计算单词出现次数

Count word occurances

本文关键字:单词出 计算      更新时间:2023-10-16

在我的程序中,我读取文本文件,然后小写所有,并将它们分开,并附加到字符串。

using namespace std;
int main() {
int lines=0,i=0,k=0;
FILE *pfile=fopen("myfile.txt", "r");
char line2[500];
char * tok;
string alltext[999];
string words[999];
if(!pfile){printf("Errorn");return 1;}
std::string line;
    std::ifstream myfile("eriala.txt");
    while (std::getline(myfile, line))
        ++lines;
for(i=0;i<lines;i++){
    fgets(line2,sizeof(line2),pfile);
    char * str = line2;
    for (int i =0;i<strlen(line2); ++i){
    line2[i]=tolower(line2[i]);}
    tok = strtok(str," ,.!nt():;-");
    while(tok !=NULL){
        alltext[k].append(tok);
        alltext[k].append("n");
        tok=strtok(NULL," ,.!nt():;-");
        k++;}}
for(int i=0;i<k;i++){
    int amount=0;
    for(int j=0;j<k;j++){
        if(strcmp(alltext[i].c_str(),alltext[j].c_str())==0)
            amount++;}
    } 
}

我需要做的是计算一个单词在文本文件中出现的次数。它就是这么做的。但是我需要按降序显示

下面是一个完整的c++ 11版本,它对单词进行计数并按降序打印:

#include <algorithm>
#include <cctype>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
int main ()
{
    std::ifstream file ("myfile.txt"); // Try to open the file
    if (!file)
    {
        std::cerr << "Error: Cannot open file myfile.txt" << std::endl;
        return 1;
    }
    std::map<std::string, std::size_t> word_counter;
    std::string word;
    while (file >> word) // While a word can be read
    {
        // Remove every character that is not an alphanumeric one
        word.erase(std::remove_if(std::begin(word), std::end(word), [](char c) { return !::isalnum(c); }), std::end(word));
        if (!word.empty())
        {
            // Lower case the word
            std::transform(std::begin(word), std::end(word), std::begin(word), ::tolower);
            // Increment the counter for this word
            word_counter[word]++;
        }
    }
    // Print the counted words in descending order
    for (auto it = word_counter.crbegin() ; it != word_counter.crend() ; ++it)
        std::cout << it->first << " => " << it->second << std::endl;
}