简体   繁体   English

计算每个句子中有多少个单词

[英]Count how many words in each sentence

I'm stuck on how to count how many words are in each sentence, an example of this is: string sentence = "hello how are you. I am good. that's good."我被困在如何计算每个句子中有多少个单词,一个例子是: string sentence = "hello how are you. I am good. that's good." and have it come out like:并让它出来像:

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

I can get the number of sentences我可以得到句子的数量

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

and i can get the number of words in the whole string我可以得到整个字符串中的单词数

    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;
    }

then button1然后按钮 1

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

But I can't count how many words are in each sentence.但是我无法计算每个句子中有多少个单词。

Instead of looping over the string as you do in CountWords I would just use;我不会像在CountWords那样循环遍历字符串,而是使用;

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

It's much more clean and simple.它更加干净和简单。 You split on white spaces which returns an array of all the words, the length of that array is the number of words in the string.您拆分空格,返回所有单词的数组,该数组的长度是字符串中的单词数。

Why not use Split instead?为什么不使用 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());

If you want number of words in each sentence, you need to如果你想要每个句子中的单词数,你需要

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 + "*");
}

Use CountWord on each element of the array returned by s.Split:对 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);
}
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);

As noted in several answers here, look at String functions like Split, Trim, Replace, etc to get you going.正如这里的几个答案中所指出的,查看字符串函数,如 Split、Trim、Replace 等,让您继续前进。 All answers here will solve your simple example, but here are some sentences which they may fail to analyse correctly;这里的所有答案都将解决您的简单示例,但这里有一些他们可能无法正确分析的句子;

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

If you only need a count, I'd avoid Split() -- it takes up unnecessary space.如果您只需要计数,我会避免使用Split() - 它会占用不必要的空间。 Perhaps:可能:

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"
}

It counts based on the number of spaces (1 space = 2 words).它根据空格数进行计数(1 个空格 = 2 个单词)。 Consecutive spaces are ignored.连续的空格被忽略。

Does your spelling of sentence in:你的句子拼写是否为:

int words = CountWord(sentance);

have anything to do with it?有什么关系吗?

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

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