计算每个句子中的单词数量

时间:2012-11-21 03:24:11

标签: c#

我坚持如何计算每个句子中有多少单词,例如:string sentence = "hello how are you. I am good. that's good." 并让它像:

//sentence1: 4 words
//sentence2: 3 words
//sentence3: 2 words

我可以得到句子数

    public int GetNoOfWords(string s)
    {
        return s.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Length;
    }
    label2.Text = (GetNoOfWords(sentance).ToString());

我可以获得整个字符串中的单词数

    public int CountWord (string text)
    {
        int count = 0;
        for (int i = 0; i < text.Length; i++)
        {
            if (text[i] != ' ')
            {
                if ((i + 1) == text.Length)
                {
                    count++;
                }
                else
                {
                    if(text[i + 1] == ' ')
                    {
                        count++;
                    }
                }
            } 
        }
        return count;
    }

然后按钮1

        int words = CountWord(sentance);
        label4.Text = (words.ToString());

但是我无法计算每个句子中有多少个单词。

8 个答案:

答案 0 :(得分:5)

而不是像CountWords中那样循环遍历字符串,我只会使用;

 int words = s.Split(' ').Length;

它更干净简单。你在白色空格上拆分,返回所有单词的数组,该数组的长度是字符串中单词的数量。

答案 1 :(得分:1)

为什么不使用Split呢?

        var sentences = "hello how are you. I am good. that's good.";

        foreach (var sentence in sentences.TrimEnd('.').Split('.'))
            Console.WriteLine(sentence.Trim().Split(' ').Count());

答案 2 :(得分:1)

如果你想要每个句子中的单词数量,你需要

string s = "This is a sentence. Also this counts. This one is also a thing.";
string[] sentences = s.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
foreach(string sentence in sentences)
{
    Console.WriteLine(sentence.Split(' ').Length + " words in sentence *" + sentence + "*");
}

答案 3 :(得分:1)

对s.Split返回的数组的每个元素使用CountWord:

string sentence = "hello how are you. I am good. that's good.";
string[] words = sentence.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Length;

for (string sentence in sentences)
{
    int noOfWordsInSentence = CountWord(sentence);
}

答案 4 :(得分:1)

string text = "hello how are you. I am good. that's good.";
string[] sentences = s.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
IEnumerable<int> wordsPerSentence = sentences.Select(s => s.Trim().Split(' ').Length);

答案 5 :(得分:1)

如此处的几个答案中所述,请查看String函数,如Split,Trim,Replace等,以便让您前进。这里的所有答案都将解决您的简单示例,但这里有一些句子可能无法正确分析;

"Hello, how are you?" (no '.' to parse on)
"That apple costs $1.50."  (a '.' used as a decimal)
"I   like     whitespace    .    "   
"Word"  

答案 6 :(得分:0)

如果只需要计数,我会避免使用Split() -它会占用不必要的空间。也许:

static int WordCount(string s)
{
    int wordCount = 0;
    for(int i = 0; i < s.Length - 1; i++)
        if (Char.IsWhiteSpace(s[i]) && !Char.IsWhiteSpace(s[i + 1]) && i > 0)
            wordCount++;
    return ++wordCount;
}

public static void Main()
{
    Console.WriteLine(WordCount(" H elloWor    ld g  ")); // prints "4"
}

它基于空格数(1个空格= 2个单词)进行计数。连续空格将被忽略。

答案 7 :(得分:-1)

你的句子拼写是:

int words = CountWord(sentance);

与它有什么关系?