求助,关于一个c++词频统计编程代码

hyesungxss 2012-03-07 10:55:01
课程设计任务书
课程名称:C++课程设计
设计题目:词频统计
问题描述
给定一篇英文文本,要求提取其中所有的单词并计算其一元词频(某个单词出现的次数)和二元词频(两个连续单词出现的次数)
举例:I have an apple . And you have an apple , too .
输出: i--1,have--2,an--2,apple--2,and--2,you--1,too--1
an apple--2
基本要求
(1)去除标点(给定文本中只含英文逗号和句号,不分段分行)
(2)全部单词转化为小写
(3)统计词频并输出结果至文件,同一单词的不同形式(比如have和had)视为不同单词
(4)程序可读性好,有简单交互界面,代码可读性好
实现提示
(1) 学习字符串和文件相关操作
(2) 使用getline函数读取单词(包含符号),筛选和处理后进行存储和计数
(3) 可以使用STL
选作内容
手动建立单词词形词典,使软件可参考词典将同一单词的不同形式转换为同一单词的原型(例如将have、has、had都转换为have),然后进行词频统计。词形词典要求从文件中读取以实现可扩充性。
...全文
675 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
pyh1991611117 2013-03-03
  • 打赏
  • 举报
回复
亲,还有人在么??问题解决了么??
thybob 2012-10-13
  • 打赏
  • 举报
回复
有中文词频统计的代码吗?
Furney 2012-03-07
  • 打赏
  • 举报
回复
这个主要是字符串处理,词频用map来存。
hyesungxss 2012-03-07
  • 打赏
  • 举报
回复
我有一个代码,但是需要改改,那位能帮我改改,结果已经实现了~但是需要进一步修改,谢谢啦
#include<iostream>
#include<fstream>
#include<string>
#include<map>
using namespace std;
string bd;
struct ll
{
string str;
int times;
};
ll aa[500];
int cishu=0;//列表里的不同词的数量
void save_file()//打开文件,读取文件
{
ifstream outfile("词频统计文件.txt",ios::in);
if(!outfile)
{
cerr<<"打开文件错误"<<endl;
exit(1);
}
while(!outfile.eof())
{
getline(outfile,bd);

}
outfile.close();
}
void delete_fh()//去除标点符号
{
int i=0;
while(i<bd.size())
{
if(bd[i]==',' || bd[i]=='.')
if(i<bd.size()-2)
bd=bd.substr(0,i)

+wz.substr(i+2,bd.size()-i-2);//前面的是遇到符号的句子+后

面的句子连接起来。逗号去除,把句子连接起来
else
bd=bd.substr(0,i);
i++;
}
}
void change()//变成小写字母
{
int i=0;
while(i<bd.size())
{
if(isupper(bd[i]))
bd[i]=bd[i]+32;
i++;
}
}
//把数据放在临时字符串与结构列表中判断,如果在结构列表
//中有就加1,如果没有就添在结构表里。
void add_a_word(string tmp)//向列表里加入词
{
int c;
for(c=0;c<cishu;c++)//遍历词表
{
if(aa[c].str==tmp)//找到了
{
aa[c].times++;//次数+1
break;
}
}
if(c==cishu)//列表里没有该词
{
aa[c].str=tmp;//把词加入列表
aa[c].times=1;//出现次数=1
cishu++;//总次数+1
}
}
void count1()//查找一个空格的单词
{

int i=0;
int a=0;
int j=0;
int n=0;
string tmp;
while(i<bd.size())
{
if(bd[i]==' ')
{
tmp=bd.substr(a,i-a);
add_a_word(tmp);
a=i+1;
}
i++;
}
}
void count2()//查找俩个空格的单词
{
string tmp1,tmp2,tmp3;
int i=0;
int a=0;
int c=0;
int s=0;
while(i<bd.size())
{
if(bd[i]==' ')
{
tmp1=bd.substr(a,i-a);
a=i+1;
c=i+1;
while(c<bd.size())
{
if(wz[c]==' ')
{
tmp2=wz.substr

(i+1,c-i-1);
break;
}
c++;
}
if(c==bd.size())
// tmp2=bd.substr

(i+1,bd.size()-i-1);
break;
tmp3=tmp1+' '+tmp2;
add_a_word(tmp3);
i=c;
continue;
}
i++;
}
//for( s=0;s<cishu;s++)
// {
// cout<<aa[s].str<<" "<<aa

[s].times<<endl;
// }
}
void over_file()//写文件
{
int s;
fstream outfile("output.txt",ios::out);
if(!outfile)
{
cerr<<"open error"<<endl;
exit(1);
}
for(s=0;s<cishu;s++)
{
outfile<<aa[s].str<<" "<<aa

[s].times<<endl;
//cout<<aa[s].str<<" "<<aa[s].times<<endl;
}
outfile.close();
}
int main(){
save_file();
delete_fh();
change();
count1();
count2();
over_file();
return 0;
hyesungxss 2012-03-07
  • 打赏
  • 举报
回复
最后显示的结果,不单单是一个单词,还要连着的单词出现的次数,例如an apple的次数
xiyoulaoyuanjia 2012-03-07
  • 打赏
  • 举报
回复
代码如下:使用stl做的,应该可以用的!

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cctype>


#include <map>
using namespace std;




void main(void)
{



;

ifstream input("d:\\1.txt");
string str;
map<string,int> string_count ;


while(input>>str){
if(str!="."&&str!=","){ // 去掉 标点符号 暂定为只有.与,
transform(str.begin(), str.end(), str.begin(), tolower);
++string_count[str];
}
}
map<string,int>::iterator it=string_count.begin();

cout<<"字符串"<<" "<<"出现次数"<<endl;
while(it!=string_count.end()){
cout<<it->first<<" "<<it->second<<endl;
it++;
}

input.close();
}



64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧