首页 > 编程语言 >【C++】统计文本词频程序

【C++】统计文本词频程序

时间:2023-04-11 23:44:51浏览次数:118  
标签:std 文本 end text C++ start 词频 word include

 1 #include <iostream>
 2 #include <fstream>
 3 #include <string>
 4 #include <iomanip>
 5 #include <vector>
 6 #include <map>
 7 #include <cctype>
 8 #include <algorithm>
 9 bool cmp(std::pair<std::string, size_t>& a,
10          std::pair<std::string, size_t>& b){
11     return a.second > b.second;
12 }//词频从大到小排序
13 
14 int main(){
15     std::ifstream ifs{};
16     ifs.open("./input.txt", std::ifstream::in);//从input.txt读入文本
17     std::string text( (std::istreambuf_iterator<char>(ifs) ),
18                        (std::istreambuf_iterator<char>()    ) );
19     //std::istreambuf_iterator<char>()表示文件结尾
20     ifs.close();
21     const std::string separators{" ():;.[],\n"};//分隔符,可自定义
22     std::map<std::string, size_t> mp;
23     std::string word{};
24     size_t start{text.find_first_not_of(separators)}, end{};
25     while(start != std::string::npos){
26         end = text.find_first_of(separators, start+1);
27         word = text.substr(start, end-start);
28         std::transform(word.begin(), word.end(), word.begin(),
29             [](unsigned char c){ return std::tolower(c); });//将单词统一转换为小写字母
30         ++mp[word];
31         start =text.find_first_not_of(separators, end+1);
32     }
33     size_t cnt{};
34     const size_t perline{6};//每行输出6个单词和词频
35     std::vector<std::pair<std::string, size_t> > v;
36     copy(mp.begin(),
37             mp.end(),
38             std::back_inserter<std::vector<std::pair<std::string, size_t> > >(v));
39     sort(v.begin(), v.end(), cmp);
40     std::stringstream ss{};
41     for(const auto& item : v){
42         ss << std::right << std::setw(15) << item.first << " : " << std::left << std::setw(5) << item.second;
43         if(++cnt % perline == 0){
44             ss << std::endl;
45         } 
46     }
47     ss << std::endl;
48     std::ofstream ofs{};
49     ofs.open("./res.txt", std::ofstream::out | std::ofstream::trunc );//结果输出到res.txt中
50     ofs << ss.str();
51     ofs.close();
52     return 0;
53 }

 

标签:std,文本,end,text,C++,start,词频,word,include
From: https://www.cnblogs.com/2020R/p/17308310.html

相关文章

  • 网络框架重构之路plain2.0(c++23 without module) 综述
    最近互联网行业一片哀叹,这是受到三年影响的后遗症,许多的公司也未能挺过寒冬,一些外资也开始撤出市场,因此许多的IT从业人员加入失业的行列,而且由于公司较少导致许多人求职进度缓慢,很不幸本人也是其中之一。自从参加工作以来,一直都是忙忙碌碌,开始总认为工作只是为了更好的生活,但是一......
  • C++中&的功能 及 用法
    参考资料:C++中&的功能及用法-konglingbin-博客园(cnblogs.com)对于习惯使用C进行开发的朋友们,在看到c++中出现的&符号,可能会犯迷糊,因为在C语言中这个符号表示了取地址符,但是在C++中它却有着不同的用途,掌握C++的&符号,是提高代码执行效率和增强代码质量的一个很好的办法。......
  • C++/ 4/11 学习内容
    空指针调用结构体中的成员函数const修饰成员函数,不能更改函数成员的值友元,让朋友可以访问本类的私有变量, *全局函数做友元*类做友元*成员函数做友元运算符重载:注意格式就ok还有<<这个输出时候的重载, 各种个样的函数重载,主要是为了方便,在主函数里面的实现......
  • Code-C++-Linux-统计一个文件夹占据空间大小
    Code-C++-Linux-统计一个文件夹占据空间大小https://my.oschina.net/Tsybius2014/blog/330628从以上链接中拷贝的代码#include<stdio.h>#include<sys/stat.h>#include<sys/types.h>#include<unistd.h>#include<stdlib.h>#include<dirent.h>#incl......
  • C++ 按照字典序实现combination
    C++按照字典序实现combination引言C++STL提供了permutation相关的函数(std::next_permutation和std::prev_permutation),但是没有提供combination相关的函数,本文将基于字典序的方法实现一个combination相关的函数。算法回顾1.permutation用法C++的permutation是基于字典序实......
  • C++复习第五天(封装)
    封装练习,设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,可以显示姓名和学号。#include<iostream>#include<string>usingnamespacestd;classStudent{public://类中的属性和行为,我们统一称为成员stringm_name;intm_Id;voidshowStudent......
  • C++-unique_lock与lock_guard区别
    C++-unique_lock与lock_guard区别https://blog.csdn.net/ccw_922/article/details/124662275https://blog.csdn.net/sinat_35945236/article/details/124505414都可以对std::mutex进行封装,实现RAII的效果。绝大多数情况下这两种锁是可以互相替代的,区别是unique_lock比lock_gu......
  • C++ Traits的笔记
    traits意思为特性,特点在C++中用于提取类型信息#include<type_traits>type_traits库中有std::is_same可以判断两个类型是否相同先看一下使用模板提取类型信息,就是多做一层封装在使用模板的过程中假设函数中有必要声明一个变量,要和迭代器所指向的对象类型相同template<class......
  • C++第二天
    计算正五边形的周长和面积#include <bits/stdc++.h>using namespace std;int main(){    double n,S,C;    double a,b;    cin>>n;    a=sqrt(5);    b=25+10*a;    S=sqrt(b)/4*n*n;    cout<<S<<endl;    C=5*n;    cout<<C<<endl;......
  • C++创建线程
    C++11中使用std::thread来创建线程。一、创建线程#include<iostream>#include<thread>#include<mutex>/*函数指针创建线程*/voidthread_func(intsize){std::cout<<"thread_func:"<<std::this_thread::get_id()<<std::en......