在.txt文件中查找热门单词计数-while循环进行得非常慢,无法正常工作

如何解决在.txt文件中查找热门单词计数-while循环进行得非常慢,无法正常工作

我试图从本质上遍历.txt文件中的每个单词,当我发现一个单词(从我的单词映射中)的单词超过maxwordcount变量时,我将其添加到topwords向量的前面

int main(int argc,char** argv) {
    fstream txtfile;
    string filename = argv[1];
    string word,tempword;
    int maxwordcount = 0;
    int wordcount = 0;
    int uniquewordcount = 0;
    vector<pair <string,int> > topwords;
    map<string,int> words;

if (argc != 2) {
    cout << "Incorrect number of arguments on the command line bud" << endl;
}else{
    txtfile.open(filename.c_str());
if (txtfile.is_open()) {
        while (txtfile >> word){
            //removePunctuation(word);
            //transform(word.begin(),word.end(),word.begin(),[](unsigned char c){ return::tolower(c); });     //makes string lowercase using iterator
            if (words.find(word) == words.end()) {   
                words[word] = 1;                                //adds word into the map as a pair starting with a word count of 1
                if (words[word] > maxwordcount) {            //For case if word is the first word added to the map
                    maxwordcount = words[word];              //change maxwordcount
                    topwords.insert( topwords.begin(),make_pair(word,words[word]) );    //insert word into the front of the top words vector
                    cout << "word: '" << word << "'  word-count: " << words[word] << endl;
                }
                uniquewordcount++;              
            }else{                                          //the word is found
                words[word]++;                              //increment count for word by 1
                if (words[word] > maxwordcount) {           //check if wordcount > maxwordcount
                    topwords.insert( topwords.begin(),words[word]) );      //insert word into the front of the top words vector       
                }                                           
            }
            wordcount++;
        }

在程序结束时,我想显示txt文件中的前10个字。我通过显示实时单词计数(cout)测试了while循环是否正在运行。这个数字上升了,但是上升得非常慢。另外,我正在为txt文件使用大量书籍。

Image of results when running

我也不完全了解在地图和向量中插入变量,因此那里可能出了问题。

我已经走到了尽头,所以这时任何事情都会有所帮助。

我也使用了一个较小的文本文件进行测试:

This is a small sentence to test test test
hey hey

结果:

word: 'This'  word-count: 1
1
2
3
4
5
6
7
7
7
8
8
There were 11 words in the file.
There were 8 unique words in the file.
Top 20 words in little.txt:
   hey 2
   test 3
   test 2
   This 1
Segmentation fault

我知道我做错了什么,但是我不知道下一步该做什么或要测试什么。还是C ++和C的业余爱好者。

解决方法

您应该逐行读取文件,逐行处理

按行读取文件文件:https://www.systutorials.com/how-to-process-a-file-line-by-line-in-c/

https://www.geeksforgeeks.org/split-a-sentence-into-words-in-cpp/

https://www.w3schools.com/cpp/cpp_functions.asp

https://www.w3schools.com/cpp/cpp_function_param.asp

https://www.w3schools.com/cpp/cpp_function_return.asp

https://www.w3schools.com/cpp/cpp_pointers.asp

https://www.w3schools.com/cpp/cpp_references.asp

#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <regex>
#include <iterator>

using namespace std;


vector<std::pair <string,int> > topwords;

void store(vector<pair <string,int> > &topwords,string str){
    auto pos = std::find_if(topwords.begin(),topwords.end(),[str](std::pair<string,int> const &b) {
            return b.first == str;
});
    
    //std::cout<< pos->first << endl;
    
    if(pos != topwords.end()){
        std::cout << "word: " << pos->first << " " << pos->second << " found" << endl;
        pos->second++;
    }
    else{
        std::cout << "not found" << endl;
        topwords.push_back( make_pair(str,1) );
    }
}

void removeDupWord(string str)
{
    // Used to split string around spaces.
    istringstream ss(str);
    
    // Traverse through all words
    /*
    do {
        // Read a word
        string word;
        ss >> word;
        
        // Print the read word
        cout << word << endl;
        store(topwords,word );
        
        // While there is more to read
    } while (ss);
    */
    string word;
    while (ss >> word) {
        //cout << word << endl;
        const std::regex sanitized{ R"([-[\]{}()*+?.,\^$|#\s])" };
        
        std::stringstream result;
        std::regex_replace(std::ostream_iterator<char>(result),word.begin(),word.end(),sanitized,"");
        
        //store(topwords,word );
        store(topwords,result.str() );
    }
}

void readReadFile(string &fileName){
    std::cout << "fileName" << fileName << endl;
    std::ifstream file(fileName);
    std::string str;
    while (std::getline(file,str)) {
        //std::cout << str << "\n";
        removeDupWord(str);
        //store(topwords,str);
    }
}

bool compareFunction (const std::pair<std::string,int> &a,const std::pair<std::string,int> &b) {
    
    return a.first<b.first; // sort by letter
}

bool compareFunction2 (const std::pair<std::string,int> &b) {
    
    return a.second>b.second; // sort by count
}

bool cmp(pair<string,int> &A,pair<string,int> &B) {
    return A.second < B.second;
}

void check(vector<pair <string,int> > &topwords){
    std::pair<string,int> mostUsedWord = make_pair("",0);
    for(auto ii : topwords){
        std::cout << "word: " << ii.first << " count: " << ii.second << endl;
        if(ii.second > mostUsedWord.second){
            mostUsedWord.first = ii.first;
            mostUsedWord.second = ii.second;
        }
    }
    std::cout << "most used Word: " << mostUsedWord.first << " x " << mostUsedWord.second << " Times." << endl;
           
}

void get_higestTopTenValues(vector<pair <string,int> > &topwords){
    std::sort(topwords.begin(),compareFunction2);//sort the vector by count
    int MAX = std::max_element(topwords.begin(),cmp)->second;
    std::cout << "max: " << MAX << endl;
    for(auto ii : topwords){
        //std::cout << "word: " << ii.first << " count: " << ii.second << endl;
        if(ii.second >= (MAX - 10)){
            std::cout << ii.first << " " << ii.second << endl;
            
        }
    }
}

void get_LowestTopTenValues(vector<pair <string,compareFunction2);//sort the vector by count
    int MIN = std::min_element(topwords.begin(),cmp)->second;
    std::cout << "min: " << MIN << endl;
    for(auto ii : topwords){
        //std::cout << "word: " << ii.first << " count: " << ii.second << endl;
        if(ii.second <= (MIN + 9)){
            std::cout << ii.first << " " << ii.second << endl;
            
        }
    }
}

int main ()
{
    std::string word,fileName;
    
    fileName = "input.txt";
    readReadFile(fileName);
    
    topwords.push_back( make_pair("ba",1) );
    topwords.push_back( make_pair("bu",1) );
    topwords.push_back( make_pair("hmmm",1) );
    topwords.push_back( make_pair("what",1) );
    topwords.push_back( make_pair("and",1) );
    topwords.push_back( make_pair("hello",1) );
    
    word = "hellos";
    
    store(topwords,word);
    store(topwords,word);
    
    word = "hello";
    
    store(topwords,word);
    
    std::sort(topwords.begin(),compareFunction);//sort the vector by letter
    // or
    //std::sort(topwords.begin(),compareFunction2);//sort the vector by count
    
    
    std::cout << "---------------------------------------" << endl;
    std::cout << " get all values" << endl;
    std::cout << "---------------------------------------" << endl;
    check(topwords);
    
    
    std::cout << "---------------------------------------" << endl;
    std::cout << " get the top 10 highest values" << endl;
    std::cout << "---------------------------------------" << endl;
    get_higestTopTenValues(topwords);
    
    std::cout << "---------------------------------------" << endl;
    std::cout << " get the top 10 lowest values" << endl;
    std::cout << "---------------------------------------" << endl;
    get_LowestTopTenValues(topwords);
    
}
,

这个问题很久以前就有人回答了。我偶然发现了这个问题并回答了,我发现一切都过于复杂。

因此,我想添加一个使用现有 STL 元素的更现代的 C++ 解决方案。

这使得代码更加紧凑。

请看下面:

#include <iostream>
#include <utility>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <string>
#include <fstream>

const std::string fileName{"r:\\loremipsum.txt"};

int main() {

    if (std::ifstream textFileStream{ fileName }; textFileStream) {

        // Here we store the count of all words
        std::unordered_map<std::string,size_t> counter{};

        size_t countOfOverallWords{}; // Counter for the number of all words

        // Read all words from file,remove punctuation,and count teh occurence
        for (std::string word; textFileStream >> word; counter[word]++) {
            word.erase(std::remove_if(word.begin(),ispunct),word.end());
            ++countOfOverallWords;
        }
        // For storing the top 10
        std::vector<std::pair<std::string,size_t>> top(10);

        // Get top 10
        std::partial_sort_copy(counter.begin(),counter.end(),top.begin(),top.end(),[](const std::pair<std::string,size_t >& p1,size_t>& p2) { return p1.second > p2.second; });

        // Now show result
        std::cout << "Count of overall words:\t " << countOfOverallWords << "\nCount of unique words:\t " << counter.size() << "\n\nTop 10:\n";
        for (const auto& t : top) std::cout << "Value: " << t.first << "\t Count: " << t.second << '\n';
    }
    else std::cerr << "\n\nError: Could not open source file '" << fileName << "'\n\n";

    return 0;
}

使用 Microsoft Visual Studio Community 2019 版本 16.8.2 进行开发和测试。

使用带有标志 --std=c++17 -Wall -Wextra -Wpedantic

的 clang11.0 和 gcc10.2 额外编译和测试

语言:C++17

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-