自然语言理解-实验报告._第1页
自然语言理解-实验报告._第2页
自然语言理解-实验报告._第3页
自然语言理解-实验报告._第4页
已阅读5页,还剩33页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、分词系统工程报告课程:自然语言理解姓名:王佳淼学号: 2011914班级:信息安全11-1日期: 2013-11-2实验一宋词字统计一研究背景本实验所涉及的研究背景是利用计算机来“鉴赏”宋词。主要针对宋词这种特殊的汉语诗歌体裁,开展了有关自动生成算法及其实现方法的探索性研究。通过对大量语料的学习,来自动生成宋词。由于宋词自身的特性, 能够在经过大量预料学习后,利用在宋词当中出现频率较高的词语或者单字排列组合来生成宋词。二实验所采用的开发平台及语言工具实验在 WIN7 的环境下利用 VC+ 编程。三系统设计(1)算法基本思想从文本中字符,判断是否为中文字符(全角字符),若为全角字符则根据需要继续

2、读取,即读取两个或三个字。利用map 容器来存储统计结果。(2)流程图( 3)代码字统计#include <map> #include <fstream>#include<iostream>#include<string>using namespace std;/宋词的统计void display_map(map<string, int> &wmap);ofstream ofs("c:fenciout2.txt");int main()cout<<" 开始: "<<

3、;endl;const char *szInputFileName="c:Text.txt"ifstream ifs(szInputFileName);string szTemp;string str1,str2,str3,str;char a10;/map<string, int> wmap;/统计双字的while (ifs)while(ifs.peek()='n') ifs.get();while(ifs.peek()=' ') ifs.get();while(!ifs.peek()&0x80) ifs.get();i

4、fs.get(a,3,' ');if(a0&0x80)str1=a;while(ifs.peek()='n') continue;while(ifs.peek()=' ') continue;while(!ifs.peek()&0x80) ifs.get();ifs.get(a,3,' ');if(a0&0x80) str2=a;if(str1!=" , "&&str1!="。 "&&str1!="? "&&

5、amp;str1!=" !"&&str1!=""&&str2!=", "&&str2!="。 "&&str2!="? "&&str2!="!"&&str2!="")szTemp=str1+str2;wmapszTemp+;if(ifs.peek()!='n'&&ifs.peek()!=' ') ifs.seekg(

6、-2,ios:cur);ifs.close();/三字ifstream ifs1(szInputFileName);while (ifs1)while(ifs1.peek()='n') ifs1.get();while(ifs1.peek()=' ') ifs1.get();while(!ifs1.peek()&0x80) ifs1.get();ifs1.get(a,3,' ');if(a0&0x80)str1=a;while(ifs1.peek()='n') continue;while(ifs1.peek()=

7、' ') continue;while(!ifs1.peek()&0x80) ifs1.get();ifs1.get(a,3,' ');if(a0&0x80) str2=a;else continue;while(ifs1.peek()='n') continue;while(ifs1.peek()=' ') continue;while(!ifs1.peek()&0x80) ifs1.get();ifs1.get(a,3,' ');if(a0&0x80) str3=a;else c

8、ontinue;if(str1!=","&&str1!="。"&&str1!="?"&&str1!="!"&&str1!=""&&str2!=","&&str2!="。"&&str2!="? "&&str2!="!"&&str2!=""&&

9、str3!=","&&str3!="。"&&str3!="? "&&str3!="!"&&str3!="")szTemp=str1+str2+str3;wmapszTemp+;if(ifs1.peek()!='n'&&ifs1.peek()!=' ') ifs1.seekg(-4,ios:cur);ifs1.close();/单字ifstream ifs2(szInputFileNam

10、e);while (ifs2)while(ifs2.peek()='n') ifs2.get();while(ifs2.peek()=' ') ifs2.get();while(!ifs2.peek()&0x80) ifs2.get();ifs2.get(a,3,' ');if(a0&0x80)str=a;if(str!=" ,"&&str!=" 。"&&str!=" ?"&&str!=" !"&

11、&str!="")szTemp=str;wmapszTemp+;ifs2.close();display_map(wmap);return false;void display_map(map<string, int> &wmap)int sum=0;map<string, int>:const_iterator map_it;for (map_it=wmap.begin(); map_it!=wmap.end();map_it+)/统计总数sum+=map_it->second;for (map_it=wmap.begin()

12、; map_it!=wmap.end();map_it+)/计算词频cout<<"(""<<map_it->first<<"","<<map_it->second<<")" cout<<(double)map_it->second/(sum-1)<<endl;for (map_it=wmap.begin(); map_it!=wmap.end();map_it+)/计算词频ofs<<"(&qu

13、ot;"<<map_it->first<<"","<<map_it->second<<")" ofs<<(double)map_it->second/sum<<endl;宋词生成#include<map>#include<fstream>#include<iostream>#include<string>using namespace std;void display_map(map<stri

14、ng, int> &wmap); void ci(int a);map<string, int> wmap;map<string, int> wmapp;int main()cout<<" 开始: "<<endl;const char *szInputFileName="c:Text.txt"ifstream ifs(szInputFileName);string szTemp,szTempp;string str1,str2,str;char a10;/统计双字的while (ifs)whi

15、le(ifs.peek()='n') ifs.get();while(ifs.peek()=' ') ifs.get();ifs.get(a,3,' ');if(a0&0x80)str1=a;while(ifs.peek()='n') continue;while(ifs.peek()=' ') continue;ifs.get(a,3,' ');if(a0&0x80) str2=a;if(str1!=" , "&&str1!="。 &q

16、uot;&&str1!="? "&&str1!=" !"&&str1!=""&&str2!=", "&&str2!="。 "&&str2!="? "&&str2!="!"&&str2!="")szTemp=str1+str2;wmapszTemp+;if(ifs.peek()!='n'&

17、&ifs.peek()!=' ') ifs.seekg(-2,ios:cur);ifs.close();/ display_map(wmap);/单字ifstream ifs1(szInputFileName);while (ifs1)while(ifs1.peek()='n') ifs1.get();while(ifs1.peek()=' ') ifs1.get();ifs1.get(a,3,' ');if(a0&0x80)str=a;if(str!=" ,"&&str!=&q

18、uot; 。"&&str!=" ?"&&str!=" !"&&str!="")szTemp=str;wmappszTemp+;ifs1.close();/ display_map(wmapp); int as;while(1)cout<<" 请 随意 输入一 个 数 来创 造 宋 词: ( 输 入 0 时 退出)"<<endl;cin>>as;if(as=0) return 0;ci(as);cout<<end

19、l<<endl;cout<<"-"<<endl;cout<<endl<<endl;return false;void ci(int a)cout<<"如梦令"<<endl;map<string, int>:iterator map_it;for(int i=0;i<3;i+)map_it=wmap.begin();for(int j=0;j<(rand()+a*2)%(wmap.size();j+) map_it+; cout<<map

20、_it->first;cout<<"。 "for(i=0;i<3;i+)map_it=wmap.begin();for( int j=0;j<rand()%(a);j+)map_it+;cout<<map_it->first;cout<<"。 "for(i=0;i<2;i+)map_it=wmap.begin();for( int j=0;j<rand()%(wmap.size();j+) map_it+; cout<<map_it->first;map_it=wm

21、app.begin();for( int j=0;j<(rand()+a*3)%(wmapp.size();j+) map_it+; cout<<map_it->first;cout<<","for(i=0;i<3;i+)map_it=wmap.begin();for( int j=0;j<(rand()+a)%(wmap.size();j+) map_it+; cout<<map_it->first;cout<<"。 "map_it=wmap.begin();for(j=0;

22、j<(2*rand()+a)%(wmap.size();j+)map_it+;string temp=map_it->first;cout<<temp<<"。"<<temp<<" 。"for(i=0;i<3;i+)map_it=wmap.begin();for( int j=0;j<(a*rand()%(wmap.size();j+) map_it+; cout<<map_it->first;cout<<"。 "<<end

23、l;void display_map(map<string, int> &wmap)map<string, int>:const_iterator map_it;for (map_it=wmap.begin(); map_it!=wmap.end();map_it+)cout<<"(""<<map_it->first<<"","<<map_it->second<<")"<<endl;四系统演示与分析(

24、 1)截图字统计:宋词生成(如梦令):(2)结果分析五对次实验的感想、意见和建议通过此次实验,能熟练利用MAP 容器,为接下来的两个实验打好基础。也能对汉字进行操作, 清楚的了解到汉字在计算机中的存储方式有别于字符型。利用位运算& 来与 0x80 进行比对以区分全角字符和半角字符。但因为将字作为了map 的 key 值,所以输出的列并不是按照出现频率由大到小,这是可以进行改进的方面。在宋词生成的程序中,主要是利用随机输入的数字来产生伪随机数,以便利用产生的伪随机数选择不同出现频率的词组来生成宋词。我觉得可以改进的方面是, 在统计时就能用程序自动获取不同词牌名的不同格式,在生成宋词时能选

25、择不同的词牌名以生成不同宋词,使程序更加智能。实验二人民日报词频统计一研究背景词是自然语言中能够独立运用的最小单位,是自然语言处理的基本单位。自动词法分析就是利用计算机对自然语言的形态 (morphology) 进行分析,判断词的结构和类别等。 而词频统计是分词系统的基础和关键,只有在正确、准确、快速的词频统计的情况下才能很好的进行分词算法。二实验所采用的开发平台及语言工具实验在 WIN7 的环境下利用 VC+ 编程。三系统设计( 1)算法基本思想从文本中字符,判断是否为中文字符(全角字符) ,若为全角字符则根据需要继续读取,即读取两个或三个字。利用map 容器来存储统计结果。(2)流程图(3

26、)代码#include<map>#include<fstream>#include<iostream>#include<string>#include<vector>#include<algorithm>using namespace std;/人民日报的词频统计typedef pair<string,int> PAIR;vector<PAIR> vecpair;void display_map(map<string, int> &wmap); ofstream ofs(&quo

27、t;c:fenciout.txt");int cmp(const PAIR& x, const PAIR& y)/vector 的 sort()降序排列return x.second>y.second;int main()cout<<" 开始: "<<endl;const char *szInputFileName="c:cidian.txt"ifstream ifs(szInputFileName);string szTemp;string str1,str2,str3,str;char a100

28、;/map<string, int> wmap;map<string, int> wmapok;/统计while (ifs)szTemp=""int i=0;ifs>>ai;/ 逐个字符读入if(ifs.eof()break;if(!(ai&0x80)/ 非中文continue;if(!(ai&0xB0)/ 除去空格ifs>>ai;continue;while(ai!='/')/ 读取szTemp+=ai;i+;ifs>>ai;wmapszTemp+;/对应字频加 1ifs.clos

29、e();/map 根据 value 排序map<string, int>:const_iterator curr; for(curr=wmap.begin();curr!=wmap.end();+curr) vecpair.push_back(make_pair(curr->first,curr->second);sort(vecpair.begin(),vecpair.end(),cmp);/调用排序函数,则 vecpair中的 first 是词,而且降序排列for(int i = 0 ;i<vecpair.size();i+)ofs<<"

30、 < "<<vecpairi.first<<", "<<vecpairi.second<<">"<<endl;display_map(wmap);return;void display_map(map<string, int> &wmap)int sum=0;map<string, int>:const_iterator map_it;for (map_it=wmap.begin(); map_it!=wmap.end();map_it+)/

31、统计总数sum+=map_it->second;for (map_it=wmap.begin(); map_it!=wmap.end();map_it+) / 计算词频,在界面上输出 ,此时是无序的cout<<"(""<<map_it->first<<"","<<map_it->second<<")"cout<<(double)map_it->second/sum<<endl;四系统演示与分析(1)截图排序后

32、:(2)结果分析五对次实验的感想、意见和建议在这次实验后,能含有分词字符的文件进行读取,利用语料来训练程序,并将结果保存在文件当中,以便实验三可以使用。而且能够进行排序,按照value 的值,即词语出现次数由高到低进行排序。美中不足的是,程序运行速度有待提高。由于map 是按照键值排序的,所以引入了 vector,并利用 vector 的 sort 函数进行排序,熟悉了这两种容器的使用,提高了对c+库函数的认识。实验三人民日报分词一研究背景自动词法分析就是利用计算机对自然语言的形态(morphology)进行分析,判断词的结构和类别等。 自动分词是汉语句子分析的基础,而且,词语的分析具有广泛的

33、应用(词频统计,词典编纂,文章风格研究等)二模型方法正向最大匹配算法 (Forward MM, FMM) 。这种算法使得程序简单易行,开发周期短,而且仅需要很少的语言资源(词表) ,不需要任何词法、句法、语义资源。三系统设计(1)算法基本思想FMM算法描述:(1) 令 i=0,当前指针 pi 指向输入字串的初始位置,执行下面的操作:(2) 计算当前指针 pi 到字串末端的字数 (即未被切分字串的长度) n,如果 n=1,转 (4),结束算法。否则,令 m=词典中最长单词的字数,如果 n<m, 令 m=n;(3)从当前pi 起取 m 个汉字作为词wi ,判断:(a) 如果 wi 确实是词典

34、中的词,则在wi 后添加一个切分标志,转(c);(b) 如果 wi 不是词典中的词且 wi 的长度大于 1,将 wi 从右端去掉一个字,转 (a)步;否则( wi 的长度等于 1),则在 wi 后添加一个切分标志(单字),执行 (c)步;(c) 根据 wi 的长度修改指针 pi 的位置,如果 pi 指向字串末端,转(4),否则, i=i+1 ,返回 (2);(4) 输出切分结果,结束分词程序。(2)流程图图 1.分词流程图图 2.FMM 算法流程图(3)代码#include<map>#include<fstream>#include<iostream>#in

35、clude<string>#include<vector>#include<algorithm>using namespace std;/人民日报的词频统计typedef pair<string,int> PAIR;vector<PAIR> vecpair;ofstream ofs("c:fenciout.txt");int cmp(const PAIR& x, const PAIR& y)return x.second>y.second;int main()cout<<"

36、; 开始: "<<endl;const char *szInputFileName="c:cidian.txt"ifstream ifs(szInputFileName);string szTemp;string str1,str2,str3,str;char a100;/map<string, int> wmap;map<string, int> wmapok;/统计while (ifs)szTemp=""int i=0;ifs>>ai;/ 逐个字符读入if(ifs.eof()break;if(

37、!(ai&0x80)/ 非中文continue;if(!(ai&0xB0)/ 除去空格ifs>>ai;continue;while(ai!='/')szTemp+=ai;i+;ifs>>ai;wmapszTemp+;/对应字频加 1ifs.close();/map 根据 value 排序map<string, int>:const_iterator curr; for(curr=wmap.begin();curr!=wmap.end();+curr) vecpair.push_back(make_pair(curr->f

38、irst,curr->second);sort(vecpair.begin(),vecpair.end(),cmp);/调用排序函数,则 vecpair中的 first 是词,而且降序排列for(int i = 0 ;i<vecpair.size();i+)ofs<<vecpairi.first<<endl;ofs.close();ifstream infile2;infile2.open("c:fenciout.txt",ios:in);if(!infile2)cerr<<"文件打开错误 "<<

39、;endl;exit(1);vector<string>arr1,arr2;/1 是读取词典, 2 是存储分好的句子 string word;string text=" 台湾是中国领土不可分割的一部分,这是大势所趋。"cout<<" 原句是:台湾是中国领土不可分割的一部分,这是大势所趋。 "<<endl<<endl;while(getline(infile2,word)arr1.push_back(word);string temp1=text;string temp2=temp1;int p1=0;while(strlen(temp2.c_str()!=0)for(int i=strlen(temp2.c_str();i>=0;i-=2)if(find(arr1.begin(),arr1.end(),temp1)!=arr1.end()temp1+="/"arr2.push_back(temp1);p1

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论