函数将返回句子中的最大单词数。 [英] Function which will returns the maximum number of words in a sentence.
本文介绍了函数将返回句子中的最大单词数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定文本可以通过将其分为点,问号和感叹号来分为句子。例如,给定S =Wetest coders。试试看?,函数应该返回4.
我尝试过:
i可以从整个文件中找到单词数
Given text can be divided into sentences by splitting it at dots, question marks and exclamation marks. For example, given S= "Wetest coders. Give us a try?", the function should return 4.
What I have tried:
i can find number of word from whole file
protected void Button1_Click(object sender, EventArgs e)
{
string s = TextBox1.Text;
Label1.Text = GetNoOfWords(s).ToString();
}
public int GetNoOfWords(string s)
{
return s.Split(new char[] {' ',',','.','?','!'}, StringSplitOptions.RemoveEmptyEntries).Length;
}
推荐答案
使用 Linq [ ^ ]
using Linq[^]
string s = "Wetest coders. Give us a try?";
var items = s.Split(new char[] { '.', '?', '!' }, StringSplitOptions.RemoveEmptyEntries);
int count = items.Select(k=>k.Trim()).Max(k => k.Split(' ').Length); // 4
您需要使用2阶段流程:
1)您将文本拆分为句子。
2)你计算每个句子中的单词,并保留最大结果。
You need to work with a 2 stages process:
1) you split text to sentences.
2) you count words in each sentence, and retain maximum result.
如果你试图排除标点符号和停用词,请查看: [ ^ ]
If you are trying to exclude punctuation and stopwords, check this out:[^]
这篇关于函数将返回句子中的最大单词数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文