繁体   English   中英

c++ - 在文本文件中逐行查找单词的频率

[英]Finding a word's frequency in a text file line by line c++

我需要读取一个文件然后向用户询问一个单词,然后我需要逐行显示该单词的出现。 我还需要用字符数组检查这个。 您可以查看我的输出示例;

Line 2: 1 occurrence(s)
line 4: 2 occurrence(s)
Line 7: 1 occurrence(s)

如您所见,我将行长度除以 searchString 长度,这是 searchString 出现的最长时间。 因此,我需要显示出现次数,但我的代码将此划分显示为出现次数。 你能帮我解决这个问题吗?

#include <iostream>
#include <string>
#include <fstream>
#include <istream>

using namespace std;
int number_of_lines = 1;

void numberoflines();

unsigned int GetFileLength(std::string FileName)
{
    std::ifstream InFile(FileName.c_str());
    unsigned int FileLength = 0;
    while (InFile.get() != EOF) FileLength++;
    InFile.close();
    cout<<"Numbers of character in your file : "<<FileLength<<endl;
    return FileLength;
}


int main()
{
    string searchString, fileName, line;
    int a;
    string *b;
    char *c,*d;
    int wordCount = 0, count = 0,count1=0;
    cout << "Enter file name : " << endl;
    cin >> fileName;
    GetFileLength(fileName);
    cout << "Enter a word for searching procces : " << endl;
    cin >> searchString;



    ifstream in (fileName.c_str(), ios::in);
    d= new char[searchString.length()+1];

    strcpy(d,searchString.c_str());

    a=GetFileLength(fileName);
    b= new string [a];


    if(in.is_open()){
        while(!in.eof()){
            getline(in,line);
            c= new char[line.length()+1];
            count++;


            strcpy(c,line.c_str());


            count1=0;
            for (int i = 0; i < line.length()/searchString.length(); i++)
            {

                char *output = NULL;
                output = strstr (c,d);
                if(output) {
                    count1++;
                }
                else count1--;
            }
            if(count1>0){cout<<"Line "<<number_of_lines<<": "<<count1<<" occurrence(s) "<<endl;}
            number_of_lines++;
            if (count==10)
            {
                break;
            }
        }

        numberoflines();
    }


    return 0;
}

void numberoflines(){
    number_of_lines--;
    cout<<"number of lines in text file: " << number_of_lines << endl;
}

输出: 查看输出

这个循环:

        for (int i = 0; i < line.length()/searchString.length(); i++)
        {
            char *output = NULL;
            output = strstr (c,d);
            if(output) {
                count1++;
            }
            else count1--;
        }

不计算行中字符串的所有匹配项,因为每次调用strstr()cd都是相同的。 当您重复搜索时,您必须从上一次匹配之后的某个地方开始。

当您找不到匹配项时,也没有理由从count1减去。 发生这种情况时,您应该退出循环。 使用for循环没什么意义,因为你没有对i做任何事情; 只需使用一个while循环。

        char *start = c;
        size_t searchlen = searchString.length();
        while (true)
        {
            char *output = strstr (start,d);
            if(output) {
                count1++;
                start = output + searchlen;
            } else {
                break;
            }
        }

您不需要将整个文件读入数组或std::string 我建议你在优化之前保持这个程序简单。

如您的问题所述,您需要使用字符数组并逐行读取。

查找istream::getline函数,因为它非常有用。

让我们声明最大行长度为 1024。

这是阅读文件部分:

#define MAX_LINE_LENGTH (1024)
char text_buffer[MAX_LINE_LENGTH]; // Look, no "new" operator. :-)
//...
while (my_text_file.getline(text_buffer, MAX_LINE_LENGTH, '\n'))
{
 //... TBD
}

上面的代码片段将一行文本读入变量text_buffer

因为您使用的是字符数组,请通读您喜欢的文本中的“str”函数,例如strstr 或者您可能必须自己编写。

下一步是从文本行中提取一个“单词”。

为了提取一个词,我们需要知道它从哪里开始,到哪里结束。 因此,需要搜索文本行。 请参阅isalpha函数,因为它很有用。

这是一个用于查找单词开头和结尾的循环:

unsigned int word_start_position = 0; // start at beginning of the line.
unsigned int word_end_position = 0;
const unsigned int length = strlen(text_buffer); // Calculate only once.
while (word_start_position < length)
{
  // Find the start of a word.
  while (!isalpha(text_buffer[word_start_position]))
  {
    ++word_start_position;
  }

  // Find end of the word.
  word_end_position = word_start_position;
  while (isalpha(text_buffer[word_end_position]))
  {
    ++word_end_position;
  }
}

上述代码片段中存在一些逻辑问题,供 OP 解决。

下一部分是添加使用单词的开始和结束位置将单词中的字符复制到另一个变量的代码。 然后,此变量将用于包含出现次数的映射关联数组字典

换句话说,在容器中搜索该词。 如果该词存在,则增加关联的出现变量。 如果它不存在,则将出现次数为 1 的单词添加到容器中。

祝你好运!

暂无
暂无

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

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