您的位置:首页 > 编程语言

编程实现:分析一个文本文件(英文文章)中各个词出现的频率,并把频率最高的前十个词打印出来

2014-03-02 12:24 627 查看
一、问题描述:分析一个文本文件(英文文章)中各个词出现的频率,并把频率最高的前十个打印出来二、问题分析:1、首先要用到文件的读取,在综合分析之后确定用c++编写2、在对于一段英文文章进行单词统计之后,各个单词出现的频率并输出3、对于每个单词出现的频率进行排序然后输出前十个频率和单词三、当老师刚刚布置这个作业时,感觉这个程序的难点在于当统计完每个单词出现的频率以及所有不同的单词的个数时,如何将其出现的次数进行排序,由于课程较多并且还有一些其他的事,所以没有立即完成作业,编写并实现这个程序用了一天的时间,比预期时间要短一些。程序中还有很多不足,以后会继续改正。对于文件的操作:
class Stword
{
public:
string name;
int num;
Stword()
{
num=0;
name="";
};
};
void readfile(Stword*&inchar,int &counter) //读入文件识别并储存单词,统计次数
{
ifstream infile("input.txt");
if(!infile)
{
cout<<"文件打开错误!"<<endl;
return;
}
while(infile)
{
string temp;
infile>>temp;
int i=0;
for( ;i<counter;i++)
{
if(temp==inchar[i].name)
{
inchar[i].num++;
break;
}
}
if(i==counter&&inchar[i].name!=temp)
{
inchar[counter].name=temp;
inchar[counter].num++;
counter++;
}
};
infile.close();
}
排序操作:
if(i>=10)//当文件中单词大于十个时对出现次数进行排序
{
int a;
for(a=0;a<i;a++)//排序
{
for(int b=0;b<i-a-1;b++)
if(inchar[b].num>inchar[b+1].num)
{
int t=inchar[b].num;
inchar[b].num=inchar[b+1].num;
inchar[b+1].num=t;
}
}
for(int c=i;c>i-10;c--)
outfile<<"前十个单词次数为:"inchar[c].num<<"前十个单词为:<<inchar[c].name<<"endl;
}
else
outfile<<"单词数少于10个!"<<endl;
通过定义Stword类进行操作,源程序:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Stword
{
public:
string name;
int num;
Stword()
{
num=0;
name="";
};
};
void readfile(Stword*&inchar,int &counter) //读入文件识别并储存单词,统计次数
{
ifstream infile("input.txt");
if(!infile)
{
cout<<"文件打开错误!"<<endl;
return;
}
while(infile)
{
string temp;
infile>>temp;
int i=0;
for( ;i<counter;i++)
{
if(temp==inchar[i].name)
{
inchar[i].num++;
break;
}
}
if(i==counter&&inchar[i].name!=temp)
{
inchar[counter].name=temp;
inchar[counter].num++;
counter++;
}
};
infile.close();
}

void outfile(Stword*inchar,int counter)//结果输出到文件的函数
{
ofstream outfile("output.txt");
for(int i=0;i<counter;i++)
{
outfile<<"单词"<<inchar[i].name<<endl<<"出现次数"<<inchar[i].num<<endl;
}

outfile<<"共有单词"<<i<<"个"<<endl;
if(i>=10)//当文件中单词大于十个时对出现次数进行排序
{
int a;
for(a=0;a<i;a++)//排序
{
for(int b=0;b<i-a-1;b++)
if(inchar[b].num>inchar[b+1].num)
{
int t=inchar[b].num;
inchar[b].num=inchar[b+1].num;
inchar[b+1].num=t;
}
}
for(int c=i;c>i-10;c--)
outfile<<"前十个单词次数为:"inchar[c].num<<"前十个单词为:<<inchar[c].name<<"endl;
}
else
outfile<<"单词数少于10个!"<<endl;
}

void main()
{
Stword*inchar=new Stword[100];
int counter=0;
readfile(inchar,counter);
outfile(inchar,counter);
}
运行结果:input.txt文本文件中的内容为:I joined the United States Air Force in January of. Whenever I would come home on leave, I would ask Dad to play the mandolin. Nobody played the mandolin like my father. He could touch your soul with the tones came out of that old mandolin. He seemed to shine when he was playing. You could see his pride in his ability to play so well for his family.输出文件output.txt中的内容为:
单词I
出现次数3
单词joined
出现次数1
单词the
出现次数4
单词United
出现次数1
单词States
出现次数1
单词Air
出现次数1
单词Force
出现次数1
单词in
出现次数2
单词January
出现次数1
单词of.
出现次数1
单词Whenever
出现次数1
单词would
出现次数2
单词come
出现次数1
单词home
出现次数1
单词on
出现次数1
单词leave,
出现次数1
单词ask
出现次数1
单词Dad
出现次数1
单词to
出现次数3
单词play
出现次数2
单词mandolin.
出现次数2
单词Nobody
出现次数1
单词played
出现次数1
单词mandolin
出现次数1
单词like
出现次数1
单词my
出现次数1
单词father.
出现次数1
单词He
出现次数2
单词could
出现次数2
单词touch
出现次数1
单词your
出现次数1
单词soul
出现次数1
单词with
出现次数1
单词tones
出现次数1
单词came
出现次数1
单词out
出现次数1
单词of
出现次数1
单词that
出现次数1
单词old
出现次数1
单词seemed
出现次数1
单词shine
出现次数1
单词when
出现次数1
单词he
出现次数1
单词was
出现次数1
单词playing.
出现次数1
单词You
出现次数1
单词see
出现次数1
单词his
出现次数3
单词pride
出现次数1
单词ability
出现次数1
单词so
出现次数1
单词well
出现次数1
单词for
出现次数1
单词family.
出现次数1
共有单词54个
前十个单词次数为:the I his to in would play mandolin. He could
前十个单词为:4 3 3 2 2 2 2 2 2 2
本次试验在参考网上代码的基础上加入自己的思想,在实验过程中遇到了很多问题,究其原因就是编程能力继续提高。

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐