c++ - 计算文本中单词的频率

标签 c++ string counting

<分区>

我写了一个函数来计算文本中特定单词的频率。这个程序每次都返回零。我该如何改进它?

while (fgets(sentence, sizeof sentence, cfPtr))
{
for(j=0;j<total4;j++)
        {
            frequency[j] = comparision(sentence,&w);
            all_frequency+=frequency[j];
}}
.
.
.
int comparision(const char sentence[ ],char *w)
{  
    int length=0,count=0,l=0,i;
    length= strlen(sentence);
    l= strlen(w);
    while(sentence[i]!= '\n')
    if(strncmp(sentence,w,l))
        count++;
    i++;
    return count;
    }

最佳答案

我已经校对了您的代码,并对编码风格和变量名称进行了评论。那里 仍然是我在条件语句中留下的一个缺陷,这是由于没有遍历 句子。

这是您标记的代码:

while(fgets(sentence, sizeof sentence, cfPtr)) {
    for(j=0;j<total4;j++){
        frequency[j] = comparision(sentence,&w);
        all_frequency+=frequency[j];
    }

}

// int comparision(const char sentence[ ],char *w)  w is a poor variable name in this case.

int comparison(const char sentence[ ], char *word)  //word is a better name.
{

    //int length=0,count=0,l=0,i;   

    //Each variable should get its own line.
    //Also, i should be initialized and l is redundant.
    //Here are properly initialized variables:

    int length = 0;
    int count = 0;
    int i = 0;

    //length= strlen(sentence);   This is redundant, as you know that the line ends at '\n'

    length = strlen(word);  //l is replaced with length.

    //while(sentence[i]!= '\n') 

    //The incrementor and the if statement should be stored inside of a block 
    //(Formal name for curley braces).

    while(sentence[i] != '\n'){
        if(strncmp(sentence, word, length) == 0)  //strncmp returns 0 if equal, so you       
            count++;                              //should compare to 0 for equality
        i++;
    }
    return count;
}

关于c++ - 计算文本中单词的频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17247194/

相关文章:

计数排序算法在 C 中不起作用

php - 如何计算php中包含零的位数

c++ - 使用 OpenCL 加速 Tesseract

c++ - Swift 中的神经网络

Python:倒序单词

javascript - 在 JavaScript 字符串中将 '-' 替换为 '--'

algorithm - 统计0到N之间的K个数

c++ - 错误 : expected primary-expression before ‘)’ token cast issue

c++ - 负序转换为负数?

string - SAS为宏变量中的所有单词添加前缀