简体   繁体   English

如何计算Javascript中以“The”开头的句子

[英]How to count sentences next to each other that starts with the word "The" in Javascript

I a beginner and I am trying to write a code that first splits a pasted text into sentences and then checks if three (or more) sentences next to each other starts with the word "The".我是初学者,我正在尝试编写一个代码,首先将粘贴的文本拆分为句子,然后检查三个(或更多)句子是否以单词“The”开头。 I also would like the program to work regardless of how many sentences that pasted text consist of, now I have only 5 sentences.无论粘贴的文本包含多少个句子,我也希望程序能够正常工作,现在我只有 5 个句子。 Is there anyone that can help me?有没有人可以帮助我?

<!DOCTYPE html>
<html>
<body>

See if three (or more) sentences next to each other starts with "The".





<form id = "quiz" name = "quiz">

<input id = "text1" type = "text" name = "question1"> <!here you are supposed to paste the text you want to check>
<input id = "button" type = "button" value = "Split into sentences" onclick = "Split();">




<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>





<script>
function Split() {
 question1 = document.quiz.question1.value; 
let text = question1;
const myArray = text.split(/[\\.!?]/); //this splits the text into sentences
var x=0
var y=x

document.getElementById("demo1").innerHTML = myArray; //here I write the sentences

let result1 = myArray [0].startsWith("The"); //here I don't have a space before the word "The" as a text normally don't start with a space.
let result2 = myArray [1].startsWith(" The");
let result3 = myArray [2].startsWith(" The");
let result4 = myArray [3].startsWith(" The");
let result5 = myArray [4].startsWith(" The"); //now I only have five sentences but I would like the program to check the text regardless of how many sentences the pasted text includes. How do I achieve that?




{document.getElementById("demo3").innerHTML = 'You have '+(result1 + result2 + result3 + result4 + result5) + ' sentences that starts with "The".';} // Here I count the sentences that starts with "The". But I would like to only count if three (or more) sentences next to each other starts with "The" and to let the program write something like "You have three sentences (or more) next to each other that starts with 'The'" and to inform the user of the program which the first sentence of these three (or more) consecutive sentences that starts with "The" is.





}
</script>

</body>
</html>

Try this at the end of your Split function!在您的拆分功能结束时试试这个!

let results=[];
for (var i=0;i<myArray.length;i++)
{
   if (myArray[i].toLowerCase().startsWith("the") || myArray[i].toLowerCase().startsWith(" the")) results.push(myArray[i]);
}
document.getElementById("demo3").innerHTML = 'You have '+(results.length) + ' sentences that starts with "The".';

Basically we loop through the results and check each one, adding it to a new array if it matches.基本上我们遍历结果并检查每个结果,如果匹配则将其添加到新数组中。 The length of the array is how many matches there are.数组的长度是有多少匹配。 You could also avoid the startsWith(" the") if you tweak your regex to also ignore leading spaces.如果您调整正则表达式以忽略前导空格,您也可以避免使用startsWith(" the")

If you notice you are using the same sequence of code across multiple lines.如果您注意到您在多行中使用相同的代码序列。 Since your new to coding a good rule of thumb is: If you see yourself typing the same thing over and over or copy and pasting;由于您是编码新手,一个好的经验法则是:如果您看到自己一遍又一遍地输入相同的内容或复制和粘贴; a loop is needed.需要一个循环。 So try this.所以试试这个。

Since we want to count how many sentence there are we can safely assume that every sentence ends in a period, so we can iterate through and count periods.由于我们想计算有多少个句子,我们可以安全地假设每个句子都以句号结尾,因此我们可以遍历并计算句号。 That's as simple as using regex which is pain of its own but can be very useful like now.这就像使用正则表达式一样简单,这本身就是一种痛苦,但像现在这样非常有用。

let sentenceLength = (text.match(/[.]/).length ; //regex

By the way regex can be learned here with very good explanations: Regexr Next now that we found how many sentences there we can simply throw your lines in a for loop.顺便说一句,在这里可以通过很好的解释来学习正则表达式: Regexr接下来,现在我们发现那里有多少个句子,我们可以简单地将您的行放入 for 循环中。

let results = 0;
for(let i=0; i < sentenceLength; i++){
   if(myArray [i].startsWith("The")){
      result++ ;
}

Now this code will guarantee doesn't matter how many lines it will iterate through.现在,此代码将保证它将迭代多少行都无关紧要。 Again I suggest you really look into the concept looping saves a lot of typing and time: looping article .我再次建议您真正研究一下循环的概念,可以节省大量的打字和时间: 循环文章 Also you may have noticed that I didn't answer your last question because the thing about coding is problem solving analyze what we did above and determine how might that question fit here keep grinding and I believe in you.此外,您可能已经注意到我没有回答您的最后一个问题,因为关于编码的事情是解决问题,分析我们上面所做的并确定该问题如何适合这里,我相信您。 Happy coding: PS.快乐编码:PS。 Research is a very strong skill and implementing what your read.研究是一项非常强大的技能,可以实施您所阅读的内容。 Developers typically get paid to do research then code.开发人员通常会通过研究然后编写代码获得报酬。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM