句子计数函数不返回总数

问题描述

所以我一直在尝试创建一个句子计数函数,它将循环遍历以下“故事”:

let story = 'Last weekend,I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack,New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop,though,because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse,I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later,I reached Greenbrook Nature Sanctuary,an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point,you are very close to the end.';

我意识到我遇到的问题,但我找不到解决方法。基本上我希望我的代码返回下面的总数 sCount 但看到我在循环后返回了我的 sCount,它只是添加和返回一个总数:

const sentencetotal = (word) => {
let sCount = 0;
if (word[word.length-1] === "." || word[word.length-1] === "!" || word[word.length-1] === "?") {
  sCount += 1;
};
return sCount;
};
// console.log(sentencetotal(story)) returns '1'.

我已经尝试了多种方法解决这个问题,例如返回 sentencetotal(word) 而不是 sCount 但 console.log 只会记录函数名称

如果我删除它的函数元素,我可以让它返回正确的 sCount 总数,但这不是我想要的。

解决方法

我没有看到任何循环或迭代器会通过 story 来计算 .?! 的出现次数。

最近自己处理了“计数句子”,我知道这是一个具有许多边缘情况的重要问题。

对于一个简单的用例,您可以使用 split 和正则表达式;

story.split(/[?!.]/).length

所以你可以像这样将它包装在你的函数中:

const sentenceTotal = (word) => {
  return word.split(/[?.!]/).length
};

let story = 'Last weekend,I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack,New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop,though,because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse,I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later,I reached Greenbrook Nature Sanctuary,an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point,you are very close to the end.';

sentenceTotal(story)
=> 13
,

关于你的问题有几个奇怪的地方,所以我会分 3 个步骤来做:

第一步:语法。

你写的是对匿名变量的 const 的赋值。所以它的作用是:

  • 创建一个常量名 'sentenceCount'
  • 给这个常量分配匿名函数(words)=> {...}
  • 现在你有了这个:sentenceCount(words){...}

仅此而已。因为你写的:()=>{} 不是函数的调用,而是匿名函数的声明,你应该读这个:https://www.w3schools.com/js/js_function_definition.asp

如果你想要一个全局总数,你必须有一个全局总数变量(不是常数),这样总数就不会丢失。所以:


let sCount = 0; //<-- have sCount as a global variable not a const

function isEndOfSentence(word) {
    if (word[word.length-1] === "." || word[word.length-1] === "!" || word[word.length-1] === "?") {
      sCount += 1;
    };
};

如果您被禁止使用全局变量(最好不要这样做),那么您必须将总数注册为函数的返回值,并将总数存储在调用的“CountWords(sentence)”函数中。

function isEndOfSentence(words) {...}

callingFunction(){

    //decalaration
    let total; 

    //...inside your loop 
    total += isEndOfSentence(currentWord)
}

算法

你能提供更多关于你如何使用你的功能的上下文吗? 如果您的目标是对单词进行计数,直到有一个分隔符来标记句子的结尾,那么您的函数将没有多大用处。

正如它所写的那样,您的函数将永远只能返回 0 或 1。它执行以下操作:

  • 函数被调用。
  • 它创建了一个名为 sCount 的变量并将其设置为 0
  • sCount 是否递增
  • 它返回 sCount 所以 1 或 0

它基本上是一个返回布尔值的“isEndOfSentence”函数。它的用法应该是这样的算法:

// var totalSentence = 0
// for each word
//    if(isEndOfSentence(word))
//        totalSentence + totalSentence = 1
// endfor   

这也回到了只是计算标点符号来计算句子的数量。

快速而小巧的解决方案

我还特别尝试将程序保持在算法显式形式中,因为我猜这就是您要处理的内容。

但我觉得你想写一些小的东西,尽可能少的字符,所以为了你的信息,有一种更快的方法可以用一个叫做正则表达式的工具和原生的 JS 'split(separator)' 函数字符串。

正则表达式是对它可以匹配的字符串的描述,并且在使用时可以返回这些匹配。并且可以在JS中用于分割字符串:

story.split(/[?!.]/) //<-- will return an array of the sentences of your story. 
story.split(/[?!.]/).length //<-- will return the number of element of the array of the sentences of your story,so the sentence count

只需一行代码即可完成您想要的操作。但是如果你想聪明地解决你的问题,请记住我说过

这也回到了只是计算标点符号来计算句子的数量。

所以我们就这样做吧?

story.match(/(\.\.\.)|[.?!]/g).length

在这里玩得开心;) : https://regexr.com/

希望对你有帮助!祝你好运!