最大概率法分词及性能测试

 最大概率分词是一种最基本的统计方法分词。一个待分割的字符串有多种分词结果,最大概率分词的原则是将其中概率最大的那个作为该字符串的分词结果。


第一部分 理论基础


        如对一个字符串:

        S:有意见分歧

        分词结果1: w1:有/ 意见/ 分歧/

        分词结果2: w2:有意/ 见/ 分歧/

        最大概率分词就是要求得 Max(P(w1|s),P(w2|s)) 。

        根据贝叶斯公式:

                P(w|s)=P(s|w)P(w)/P(s)                                                                      (公式1)

        在公式1中,因为P(s)和P(w|s)都基本一样,因此,就求最大的P(w)即可。根据一元语法,词之间出现的概率互相独立,因此有下面的公式成:

                P(w)=P(w1,w2,…,w3)=P(w1)P(w2)…P(w3)                                   (公式2)

        即字符串出现的概率就是构成字符串的各个词的概率之积。而一个词的概率可以按照其出现的次数除以语料中总的词数得到。

        分析下面的例子,我们可以计算得到各个词的概率为:

                有:0.018

                有意:0.0005

                意见:0.001

                见:0.0002

                分歧:0.0001

        则根据公式2有:

                P(w1)=p(有)P(意见)P(分歧)=0.018*0.001*0.0001=1.8*10^(-9)

                P(w2)=P(有意)P(见)P(分歧)=0.0005*0.0002*0.0001=1*10^(-11)

        由于P(w1)>P(w2),故w1为该字符串的分词结果。


        当然,在实际操作过程中,如果字符串比较长,分词的形式就会非常多,计算量和长度呈指数增长关系,因此需要采用一定的算法来减少运算量,我们可以看到字符串的概率是累计相乘的,因此可以采用动态规划的方法来减少运算量。

        这里记P`(w)为到达候选词wi时的累计概率,则

                P`(wi)=P`(wi-1)P(wi)                                             (公式3)

        根据公式3,有P`(意见)=P`(有)P(意见)


第二部分 算法实现


        在算法的实现思路上,基本上是先记录所有可能出现的词,以及其对应的概率,也就是分段的代价函数,同时寻找每一个词的最佳的前趋词。然后就是回溯,从字符串的尾部向前搜索最优路径即可。这也是动态规划的一般实现方法。


        1.思路说明

        (1)获取候选词

        获取句子中可能出现的所有词作为候选词,但要满足下列条件:如果是长度大于1的词,则必须在词典中出现;如果是长度等于1,即为单字,可以不在词典中出现。

        (2)构造前趋词:

        假定字符串从左到右进行扫描,可以得到w1,w2,…,wi-1,wi,….等若干候选词,如果wi-1的尾字根wi的首字邻接,就称wi-1为wi的前趋词。比如上面例中,候选词“有”就是候选词“意见”的前趋词,“意见”和“见”都是“分歧”的前趋词。字串最左边的词没有前趋词。

        (3)寻找最佳前趋词:

        如果某个候选词wi有若干个前趋词wj,wk,…..等等,其中累计概率最大的候选词称为wi的最佳前趋词。比如候选词“意见”只有一个前趋词“有”,因此“有”同时也就是“意见”的最佳前趋词;候选词“分歧”有两个前趋词“意见”和“见”,其中“意见”的累计概率大于“见”累计概率,因此“意见”是“分歧”的最佳前趋词。

        (4)确定最优路径

        回溯,从字符串的尾部按照最佳前趋词的指引,向前搜索最优路径。


        2.具体步骤

        (1)对一个待分词的字串S,按照从左到右的顺序取出全部候选词w1,w2,….,wi,…,wn;

        (2)到词典中查出每个候选词的概率值P(wi)。

        (3)按照公式3计算每个候选词的累计概率,同时比较得到每个词的最佳前趋词。

        (4)如果当前词wn是字符串S的尾词,且累计概率P’(wn)最大,则wn就是S的终点词。

        (5)从wn开始,按照从右到左的顺序,因此将每个词的最佳前趋输出,即为S的分词结果。

         例子: 

        (1)对“有意见分歧”,从左到右进行一遍扫描,得到全部候选词:“有”,“有意”,“意见”,“见”,“分歧”;

        (2)对每个候选词,记录下它的概率值,并将累计概率赋初值为0;

        (3)顺次计算各个候选词的累计概率值,同时记录每个候选词的最佳前趋词:

                P`(有)=P(有),

                P`(意见)=P(意见),

                P`(意见)=P`(有)P(意见),(“意见”的最佳前趋词为“有”)

                P`(见)=P`(有意)P(见),(“见”的最佳前趋词为“有意”)

                P`(意见) > P`(见)

        (4) “分歧”是尾词,“意见”是“分歧”的最佳前趋词,分词过程结束。




第三部分 结果展示


        对1998年1月《人民日报》进行分析,其中构造词典和测试用的语料比例为9:1。分别用三种方法进行分词:正向最大概率匹配、逆向最大概率匹配、最大概率法。对它们的分词结果进行比较,结果如下:




第四部分 源代码


        源代码分为三个文件,分别是:dictionary_2.h(词典头文件)、segmentwords.cpp(三种分词方法所在的文件)、main.cpp(结果输出、正确性比对等功能)。

        1.dictionary_2.h(词典头文件)

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <string>  
  3. #include <fstream>  
  4. #include <sstream>  
  5. #include <map>  
  6. #include <cstdlib>  
  7.   
  8. using namespace std;  
  9.   
  10. /* 
  11.  * 词典的定义,用于最大概率分词 
  12.  */  
  13. class Dictionary{  
  14.     private:  
  15.         string strline;         //保存每行内容  
  16.         string word;            //保存一个词语  
  17.         map<string, int> word_map;    //词典,用map表示  
  18.       
  19.     public:  
  20.         long size;          //词典规模  
  21.         long freq_all;  
  22.         long arr_1[20];  
  23.         double arr_2[20];  
  24.         Dictionary();           //构造函数,初始化词典  
  25.         ~Dictionary();  
  26.         int findWord(string word);  //在词典中查找特定的词语  
  27. };  
  28.   
  29. Dictionary::Dictionary(){  
  30.     freq_all = 0;  
  31.     for(int i = 0; i < 20; i++){  
  32.         arr_1[i] = 0;  
  33.         arr_2[i] = 0.0;  
  34.     }  
  35.     //读取词典文件  
  36.     fstream fin("dict_3.txt");  
  37.     if(!fin){  
  38.         cerr << "open file error !" << endl;  
  39.         exit(-1);  
  40.     }  
  41.     //将每个词语加入集合  
  42.     while(getline(fin, strline, '\n')){  
  43.         istringstream istr(strline);  
  44.         istr >> word;     //从流中读取单词  
  45.         ++word_map[word];   //  
  46.         ++arr_1[word.size()];  
  47.         ++freq_all;  
  48.     }  
  49.     fin.close();  
  50.     //初始化词典大小  
  51.     size = word_map.size();  
  52.     for(int i = 0; i < 20; i++){  
  53.         arr_2[i] = (double)arr_1[i]/freq_all;  
  54.     }  
  55. }  
  56.   
  57. Dictionary::~Dictionary(){  
  58.       
  59. }  
  60.   
  61. int Dictionary::findWord(string word){  
  62.     map<string, int>::iterator p_cur = word_map.find(word);   
  63.     if(p_cur != word_map.end()){  
  64.         return p_cur -> second;  
  65.     }else{  
  66.         return -1;  
  67.     }  
  68. }  


        2.segmentwords.cpp(三种分词方法所在的文件)

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <cmath>  
  2. #include <string>  
  3. #include <iostream>  
  4. #include "dictionary_2.h"  
  5.   
  6. const short MaxWordLength = 20; //词典中最大词的长度  
  7. const char Separator = '/';     //词界标记  
  8.   
  9. Dictionary word_dict;           //初始化一个词典  
  10.    
  11. /* 
  12.  * 类定义:候选词的结构 
  13.  */  
  14. class Candidate{  
  15.     public:  
  16.         short pos;  //候选词在输入串中的起点  
  17.         short length;   //输入串的长度  
  18.         short bestPrev; //最佳前趋词的序号  
  19.         float fee;  //候选词的费用  
  20.         float sumFee;   //候选词路径上的累计费用  
  21.         string word;    //候选词  
  22.         int freq;   //候选词的频数(不能用short,否则有可能溢出)  
  23. };  
  24.   
  25.   
  26. /* 
  27.  * 函数功能:取出字符串中的全部候选词 
  28.  * 函数输入:字符串的引用 
  29.  * 函数输出:该字符串中含有的所有的存在与词典中的词(或者单字,单字可以在词典中不存在) 
  30.  */  
  31. vector<Candidate> getTmpWords(const string &s){  
  32.     int freq = 0;           //词典中词的频率  
  33.     short n = s.length();       //字符串的长度  
  34.     string word = "";       //存放候选词  
  35.     Candidate cand;         //存放候选词属性  
  36.     vector<Candidate> vec_cd; //候选词队列  
  37.   
  38.     //以每个汉字为起点  
  39.     for(short i = 0; i < n; i += 2){  
  40.         //词的长度为 1~MaxWordLength/2 个汉字  
  41.         for(short len = 2; len <= MaxWordLength; len += 2){  
  42.             word = s.substr(i, len);  
  43.             freq = word_dict.findWord(word);//去词典中查找出现频率  
  44.             if(len > 2 && freq == -1){  
  45.                 //若不止一字且词表中找不到则不予登录  
  46.                 continue;  
  47.             }  
  48.             if(freq == -1){  
  49.                 //如果为单字词,且词表中找不到  
  50.                 freq = 0;  
  51.             }  
  52.             cand.pos = i;           //该候选词在汉字串中的起点  
  53.             cand.length = len;      //该候选词的长度  
  54.             cand.word = word;  
  55.             cand.fee = -log((double)(freq*1 + 1)/word_dict.freq_all);//该候选词的费用  
  56.             cand.sumFee = 0.0f;     //该候选词的累计费用置初值  
  57.             cand.freq = freq;  
  58.             //将获取的候选词加入队列  
  59.             vec_cd.push_back(cand);   
  60.         }  
  61.     }  
  62.   
  63.     return vec_cd;  
  64. }  
  65.   
  66. /* 
  67.  * 函数功能:获取最佳前趋词序号 
  68.  * 函数输入:候选词列表的引用 
  69.  * 函数输出:无 
  70.  */  
  71. void getPrew(vector<Candidate> &vec_cd){  
  72.     short min_id = -1;              //最佳前趋词编号  
  73.     short j = -1;  
  74.     short size = (short)vec_cd.size();      //计算队列长度  
  75.     for(short i = 0; i < size; i++){  
  76.         if(vec_cd[i].pos == 0){  
  77.             //如果候选词是汉字串中的首词  
  78.             vec_cd[i].bestPrev = -1;    //无前趋词  
  79.             vec_cd[i].sumFee = vec_cd[i].fee;   //累计费用为该词本身费用  
  80.         }else{  
  81.             //如果候选词不是汉字串中的首词  
  82.             min_id = -1;            //初始化最佳前趋词编号  
  83.             j = i - 1;          //从当前对象向左找  
  84.             while(j >= 0){  
  85.                 //向左寻找所遇到的所有前趋词  
  86.                 if(vec_cd[j].pos + vec_cd[j].length == vec_cd[i].pos){  
  87.                     if(min_id == -1 || vec_cd[j].sumFee < vec_cd[min_id].sumFee){  
  88.                         min_id = j;  
  89.                     }  
  90.                 }  
  91.                 --j;  
  92.             }  
  93.   
  94.             vec_cd[i].bestPrev = min_id;    //登记最佳前趋编号  
  95.             vec_cd[i].sumFee = vec_cd[i].fee + vec_cd[min_id].sumFee;//登记最小累计费用  
  96.         }  
  97.     }  
  98. }  
  99.   
  100.   
  101. /* 
  102.  * 函数功能:最大概率法分词 
  103.  * 函数输入:待切分的字符串 
  104.  * 函数输出:切分好的字符串 
  105.  */  
  106. string segmentSentence_MP(string s1){  
  107.     short len = s1.length();  
  108.     short min_id = -1;      //最小费用路径的终点词的序号  
  109.       
  110.     //取出s1中的全部候选词  
  111.     vector<Candidate> vec_cd = getTmpWords(s1);  
  112.   
  113.     //获得最佳前趋词序号、当前词最小累计费用  
  114.     getPrew(vec_cd);  
  115.   
  116.     //确定最小费用路径的终点词的序号  
  117.     short n = (short)vec_cd.size();  
  118.     for(short i = 0; i < n; i++){  
  119.         if(vec_cd[i].pos + vec_cd[i].length == len){  
  120.             //如果当前词是s1的尾词  
  121.             if(min_id == -1 || vec_cd[i].sumFee < vec_cd[min_id].sumFee){  
  122.                 //如果是第一个遇到的尾词,或者是当前尾词的最小累计费用小于  
  123.                 //已经遇到过的任一尾词的最小累计费用,则将其序号赋给min_id  
  124.                 min_id = i;  
  125.             }  
  126.         }  
  127.     }  
  128.   
  129.     //构造输出串  
  130.     string s2 = "";     //输出串初始化  
  131.     for(short i = min_id; i >= 0; i = vec_cd[i].bestPrev){  
  132.         //注意:是先取后面的词  
  133.         s2 = s1.substr(vec_cd[i].pos, vec_cd[i].length) + Separator + s2;  
  134.     }  
  135.           
  136.     return s2;  
  137. }  
  138.   
  139.   
  140.   
  141. /* 
  142.  * 函数功能:对字符串用最大匹配算法(正向)处理 
  143.  * 函数输入:汉字字符串 
  144.  * 函数输出:分好词的字符串 
  145.  */  
  146. string segmentSentence_1(string s1){  
  147.     string s2 = "";     //用s2存放分词结果  
  148.       
  149.     while(!s1.empty()){  
  150.         int len = s1.length();  //取输入串长度  
  151.         if(len > MaxWordLength){  
  152.             len = MaxWordLength;    //只在最大词长范围内进行处理  
  153.         }  
  154.           
  155.         string w = s1.substr(0, len);  
  156.         int n = word_dict.findWord(w);  //在词典中查找相应的词  
  157.         while(len > 2 && n == -1){  
  158.             len -= 2;   //从候选词右边减掉一个汉字,将剩下的部分作为候选词  
  159.             w = s1.substr(0, len);  
  160.             n = word_dict.findWord(w);  
  161.         }  
  162.   
  163.         s2 = s2 + w + Separator;  
  164.         s1 = s1.substr(w.length(), s1.length() - w.length());  
  165.     }  
  166.       
  167.     return s2;  
  168. }  
  169.   
  170.   
  171. /* 
  172.  * 函数功能:对字符串用最大匹配算法(逆向)处理 
  173.  * 函数输入:汉字字符串 
  174.  * 函数输出:分好词的字符串 
  175.  */  
  176. string segmentSentence_2(string s1){  
  177.     string s2 = "";     //用s2存放分词结果  
  178.       
  179.     while(!s1.empty()){  
  180.         int len = s1.length();  //取输入串长度  
  181.         if(len > MaxWordLength){  
  182.             len = MaxWordLength;    //只在最大词长范围内进行处理  
  183.         }  
  184.           
  185.         string w = s1.substr(s1.length() - len, len);  
  186.         int n = word_dict.findWord(w);  //在词典中查找相应的词  
  187.         while(len > 2 && n == -1){  
  188.             len -= 2;   //从候选词左边减掉一个汉字,将剩下的部分作为候选词  
  189.             w = s1.substr(s1.length() - len, len);  
  190.             n = word_dict.findWord(w);  
  191.         }  
  192.   
  193.         w = w + Separator;  
  194.         s2 = w + s2;  
  195.         s1 = s1.substr(0, s1.length() - len);  
  196.     }  
  197.       
  198.     return s2;  
  199. }  


        3.main.cpp(结果输出、正确性比对等功能)

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <cstdlib>  
  2. #include <vector>  
  3. #include <iomanip>  
  4. #include <map>  
  5. #include <algorithm>  
  6. #include <sys/time.h>  
  7. #include <sys/stat.h>  
  8. #include "segmentwords.cpp"  
  9.   
  10. const long MaxCount = 50000;    //需要切分的最大句子数量,若该值大于文件中  
  11.                 //实际的句子数量,以实际句子数量为准。  
  12.   
  13. //获取当前时间(ms)  
  14. long getCurrentTime(){  
  15.     struct timeval tv;  
  16.     gettimeofday(&tv, NULL);  
  17.     return tv.tv_sec*1000 + tv.tv_usec/1000;  
  18. }  
  19.   
  20. //获取文件大小  
  21. unsigned long getFileSize(string file_path){  
  22.     unsigned long filesize = -1;  
  23.     struct stat statbuff;  
  24.     if(stat(file_path.c_str(), &statbuff) < 0){  
  25.         return filesize;  
  26.     }else{  
  27.         filesize = statbuff.st_size;  
  28.     }  
  29.         return filesize;  
  30. }  
  31.   
  32.   
  33.   
  34. /* 
  35.  * 函数功能:对句子进行最大匹配法处理,包含对特殊字符的处理 
  36.  * 函数输入:1.含有汉字、英文符号的字符串 
  37.  *         2.flag=1调用正向最大匹配算法,flag=2调用逆向最大匹配算法 
  38.  * 函数输出:分好词的字符串 
  39.  */  
  40. string SegmentSentenceMM(string s1, int flag){  
  41.     string s2 = ""//用s2存放分词结果  
  42.     int i;  
  43.     int dd;  
  44.     while(!s1.empty()){  
  45.         unsigned char ch = (unsigned char)s1[0];  
  46.         if(ch < 128){  
  47.             //处理西文字符  
  48.             i = 1;  
  49.             dd = s1.length();  
  50.   
  51.             while(i < dd && ((unsigned char)s1[i] < 128) && (s1[i] != 10) && (s1[i] != 13)){  
  52.                 //s1[i]不能是换行符或回车符  
  53.                 i++;  
  54.             }//中止循环条件:出现中文字符、换行或者回车  
  55.   
  56.             if(i == 1 && (ch == 10 || ch == 13)){  
  57.                 //如果是换行或回车符,将它拷贝给s2输出  
  58.                 s2 += s1.substr(0, i);  
  59.             }else{  
  60.                 s2 += s1.substr(0, i) + Separator;  
  61.             }  
  62.               
  63.             s1 = s1.substr(i, dd);  
  64.             continue;  
  65.         }else{  
  66.             if(ch < 176){  
  67.                 //中文标点等非汉字字符  
  68.                 i = 0;  
  69.                 dd = s1.length();  
  70.               
  71.                 //获取中文双字节特殊字符(非汉字、非中文标点),中止循环条件:超过长度、出现中文标点符号、出现汉字  
  72.                 while(i < dd && ((unsigned char)s1[i] < 176) && ((unsigned char)s1[i] >= 161)  
  73.                     && (!((unsigned char)s1[i] == 161 && ((unsigned char)s1[i+1] >= 162 && (unsigned char)s1[i+1] <= 168)))  
  74.                     && (!((unsigned char)s1[i] == 161 && ((unsigned char)s1[i+1] >= 171 && (unsigned char)s1[i+1] <= 191)))  
  75.                     && (!((unsigned char)s1[i] == 163 && ((unsigned char)s1[i+1] == 161 || (unsigned char)s1[i+1] == 168  
  76.                     ||   (unsigned char)s1[i+1] == 169 || (unsigned char)s1[i+1] == 172 || (unsigned char)s1[i+1] == 186   
  77.                     ||   (unsigned char)s1[i+1] == 187 || (unsigned char)s1[i+1] == 191)))){  
  78.                     //假定没有半个汉字  
  79.                     i = i + 2;  
  80.                 }  
  81.                   
  82.                 //出现中文标点  
  83.                 if(i == 0){  
  84.                     i = i + 2;  
  85.                 }  
  86.   
  87.                 //中文标点每个加一个分词标记;其他非汉字双字节字符连续输出,只加一个分词标记  
  88.                 s2 += s1.substr(0, i) + Separator;  
  89.                   
  90.   
  91.                 s1 = s1.substr(i, dd);  
  92.                 continue;  
  93.             }  
  94.         }  
  95.           
  96.         //以下处理汉字串  
  97.         i = 2;  
  98.         dd = s1.length();  
  99.         while(i < dd && (unsigned char)s1[i] >= 176){  
  100.             i += 2;  
  101.         }  
  102.   
  103.         if(flag == 1){  
  104.             //调用正向最大匹配  
  105.             s2 += segmentSentence_1(s1.substr(0, i));  
  106.         }else if(flag == 2){  
  107.             //调用逆向最大匹配  
  108.             s2 += segmentSentence_2(s1.substr(0, i));  
  109.         }else if(flag == 3){  
  110.             //调用最大概率匹配  
  111.             s2 += segmentSentence_MP(s1.substr(0, i));  
  112.         }  
  113.   
  114.         s1 = s1.substr(i, dd);   
  115.     }  
  116.   
  117.     return s2;  
  118. }  
  119.   
  120.   
  121. /* 
  122.  * 函数功能:删除分词标记(即去掉字符串中的/) 
  123.  * 函数输入:含有分词标记的字符串 
  124.  * 函数输出:不含分词标记的字符串 
  125.  */  
  126. string removeSeparator(string str_in){  
  127.     char s[10000];  
  128.     int j = 0;  
  129.     for(int i = 0; i < str_in.length(); i++){  
  130.         if(!(str_in[i] == '/')){  
  131.             s[j] = str_in[i];  
  132.             j++;  
  133.         }  
  134.     }  
  135.     s[j] = '\0';  
  136.     string str_out = s;  
  137.     return str_out;  
  138. }  
  139.   
  140.   
  141. /* 
  142.  * 函数功能:计算切分标记的位置 
  143.  * 函数输入:1.strline_in未进行切分的汉字字符串 
  144.            2.strline_right进行切分后的汉字字符串 
  145.  * 函数输出:vecetor,其中存放了strline_in中哪些位置放置了分词标记 
  146.  *         注意:vector中不包含最后标记的位置,但是包含位置0。 
  147.  */  
  148. vector<int> getPos(string strline_right, string strline_in){  
  149.     int pos_1 = 0;  
  150.     int pos_2 = -1;  
  151.     int pos_3 = 0;  
  152.     string word = "";  
  153.     vector<int> vec;  
  154.   
  155.     int length = strline_right.length();  
  156.     while(pos_2 < length){  
  157.         //前面的分词标记  
  158.         pos_1 = pos_2;  
  159.           
  160.         //后面的分词标记  
  161.         pos_2 = strline_right.find('/', pos_1 + 1);  
  162.   
  163.         if(pos_2 > pos_1){  
  164.             //将两个分词标记之间的单词取出  
  165.             word  = strline_right.substr(pos_1 + 1, pos_2 - pos_1 - 1);  
  166.             //根据单词去输入序列中查出出现的位置  
  167.             pos_3 = strline_in.find(word, pos_3);  
  168.             //将位置存入数组  
  169.             vec.push_back(pos_3);  
  170.             pos_3 = pos_3 + word.size();  
  171.         }else{  
  172.             break;  
  173.         }  
  174.     }  
  175.       
  176.     return vec;  
  177. }  
  178.   
  179.   
  180. /* 
  181.  * 获取标准切分和程序切分的结果 
  182.  */  
  183. string getString(string word, int pos, vector<int> vec_right){  
  184.     char ss[1000];  
  185.     int i = 0;  
  186.     int k = 0;  
  187.     while(vec_right[i] < pos){  
  188.         i++;  
  189.     }  
  190.     for(int j = 0; j < word.size(); j++){  
  191.         if(j == vec_right[i] - pos){  
  192.             if(j != 0){  
  193.                 ss[k] = '/';  
  194.                 ++k;  
  195.             }  
  196.             ++i;  
  197.         }  
  198.         ss[k] = word[j];  
  199.         ++k;  
  200.     }  
  201.     ss[k] = '\0';  
  202.     string word_str = ss;  
  203.   
  204.     return word_str;  
  205. }  
  206.   
  207. /* 
  208.  * 函数功能:获取单个句子切分的结果统计 
  209.  * 函数输入:1.vec_right 正确的分词标记位置集合 
  210.  *           2.vec_out   函数切分得到的分词标记位置集合 
  211.  * 函数输出:返回一个veceor,含有4个元素,分别为: 
  212.  *          切分正确、组合型歧义、未登录词、交集型歧义的数量 
  213.  * 
  214.  */  
  215. vector<int> getCount_2(string strline, vector<int> vec_right, vector<int> vec_out, vector<string> &vec_err){  
  216.     vector<int> vec(4, 0);    //存放计算结果  
  217.     //建立map  
  218.     map<intint> map_result;  
  219.     for(int i = 0; i < vec_right.size(); i++){  
  220.         map_result[vec_right[i]] += 1;  
  221.     }  
  222.     for(int i = 0; i < vec_out.size(); i++){  
  223.         map_result[vec_out[i]] += 2;  
  224.     }  
  225.   
  226.     //统计map中的信息  
  227.     //若value=1,只在vec_right中  
  228.     //若value=2,只在vec_out中  
  229.     //若value=3,在vec_right和vec_out中都有  
  230.     map<intint>::iterator p_pre, p_cur;  
  231.     int count_value_1 = 0;  
  232.     int count_value_2 = 0;  
  233.     int count_value_3 = 0;  
  234.     p_pre = map_result.begin();  
  235.     p_cur = map_result.begin();  
  236.     while(p_cur != map_result.end()){  
  237.         while(p_cur != map_result.end() && p_cur -> second == 3){  
  238.             p_pre = p_cur;  
  239.             ++count_value_3;    //切分正确的数目  
  240.             ++p_cur;        //迭代器后移  
  241.         }  
  242.           
  243.         while(p_cur != map_result.end() && p_cur -> second != 3){  
  244.             if(p_cur -> second == 1){  
  245.                 ++count_value_1;  
  246.             }else if(p_cur -> second == 2){  
  247.                 ++count_value_2;  
  248.             }  
  249.             ++p_cur;  
  250.         }  
  251.           
  252.         //确定切分错误的字符串  
  253.         if(p_cur == map_result.end() && p_cur == (++p_pre)){  
  254.             continue;  
  255.         }  
  256.         int pos_1 = p_pre -> first;  
  257.         int pos_2 = p_cur -> first;   
  258.         string word = strline.substr(pos_1, pos_2 - pos_1); //切分错误的单词  
  259.         string word_right = getString(word, pos_1, vec_right);  //正确的切分方式  
  260.         string word_out = getString(word, pos_1, vec_out);  //得到的切分方式  
  261.    
  262.         string str_err = "";  
  263.         //不同的错误类型         
  264.         if(count_value_1 > 0 && count_value_2 == 0){  
  265.             str_err = "  组合型歧义: " + word + "    正确切分: " + word_right + "    错误切分: " + word_out;  
  266.             vec_err.push_back(str_err);  
  267.             cout << str_err << endl;  
  268.             vec[1] += count_value_1;          
  269.         }else if(count_value_1 == 0 && count_value_2 > 0){  
  270.             str_err = "  未登录词语: " + word + "    正确切分: " + word_right + "    错误切分: " + word_out;  
  271.             vec_err.push_back(str_err);  
  272.             cout << str_err << endl;  
  273.             vec[2] += count_value_2;  
  274.         }else if(count_value_1 > 0 && count_value_2 > 0){  
  275.             str_err = "  交集型歧义: " + word + "    正确切分: " + word_right + "    错误切分: " + word_out;  
  276.             vec_err.push_back(str_err);  
  277.             cout << str_err << endl;  
  278.             vec[3] += count_value_2;      
  279.         }  
  280.   
  281.         //计数器复位  
  282.         count_value_1 = 0;  
  283.         count_value_2 = 0;  
  284.     }  
  285.   
  286.     vec[0] += count_value_3;      
  287.   
  288.     return vec;  
  289. }  
  290.   
  291.   
  292. /* 
  293.  * 主函数:进行分词并统计分词结果 
  294.  * 
  295.  */  
  296. int main(int argc, char *argv[]){  
  297.     long time_1 = getCurrentTime();  
  298.       
  299.     string strline_right;   //输入语料:用作标准分词结果  
  300.     string strline_in;  //去掉分词标记的语料(用作分词的输入)  
  301.     string strline_out_1;   //正向最大匹配分词完毕的语料  
  302.     string strline_out_2;   //逆向最大匹配分词完毕的语料  
  303.     string strline_out_3;   //最大概率方法分词完毕的语料  
  304.       
  305.     ifstream fin("test.txt");   //打开输入文件  
  306.     if(!fin){  
  307.         cout << "Unable to open input file !" << argv[1] << endl;  
  308.         exit(-1);  
  309.     }  
  310.   
  311.     /* 
  312.     ofstream fout("result.txt");    //确定输出文件 
  313.     if(!fout){ 
  314.         cout << "Unable to open output file !" << endl; 
  315.         exit(-1); 
  316.     } 
  317.     */  
  318.   
  319.     long count = 0;         //句子编号  
  320.     long count_0 = 0;       //三种方法切分都正确的句子总数  
  321.     long count_1 = 0;       //正向最大匹配完全正确的句子总数  
  322.     long count_2 = 0;       //逆向最大匹配完全正确的句子总数  
  323.     long count_3 = 0;       //最大概率方法完全正确的句子总数  
  324.   
  325.     long count_right_all = 0;   //准确的切分总数  
  326.     long count_out_1_all = 0;   //正向最大匹配切分总数  
  327.     long count_out_2_all = 0;   //逆向最大匹配切分总数  
  328.     long count_out_3_all = 0;   //最大概率方法切分总数  
  329.     long count_out_1_right_all = 0; //正向最大匹配切分正确总数  
  330.     long count_out_2_right_all = 0; //逆向最大匹配切分正确总数  
  331.     long count_out_3_right_all = 0; //最大概率方法切分正确总数  
  332.     long count_out_1_fail_1_all = 0;//正向最大匹配(组合型歧义)  
  333.     long count_out_1_fail_2_all = 0;//正向最大匹配(未登录词语)  
  334.     long count_out_1_fail_3_all = 0;//正向最大匹配(交集型歧义)  
  335.     long count_out_2_fail_1_all = 0;//逆向最大匹配(组合型歧义)  
  336.     long count_out_2_fail_2_all = 0;//逆向最大匹配(未登录词语)  
  337.     long count_out_2_fail_3_all = 0;//逆向最大匹配(交集型歧义)  
  338.     long count_out_3_fail_1_all = 0;//最大概率方法(组合型歧义)  
  339.     long count_out_3_fail_2_all = 0;//最大概率方法(未登录词语)  
  340.     long count_out_3_fail_3_all = 0;//最大概率方法(交集型歧义)  
  341.   
  342.     vector<string> vec_err_1; //正向最大匹配切分错误的词  
  343.     vector<string> vec_err_2; //逆向最大匹配切分错误的词  
  344.     vector<string> vec_err_3; //最大概率方法切分错误的词  
  345.   
  346.     while(getline(fin, strline_right, '\n') && count < MaxCount){  
  347.         if(strline_right.length() > 1){  
  348.               
  349.             //去掉分词标记  
  350.             strline_in = removeSeparator(strline_right);  
  351.   
  352.             //正向最大匹配分词  
  353.             strline_out_1 = strline_right;  
  354.             strline_out_1 = SegmentSentenceMM(strline_in, 1);  
  355.               
  356.             //逆向最大匹配分词  
  357.             strline_out_2 = strline_right;  
  358.             strline_out_2 = SegmentSentenceMM(strline_in, 2);  
  359.   
  360.             //最大概率方法分词  
  361.             strline_out_3 = strline_right;  
  362.             strline_out_3 = SegmentSentenceMM(strline_in, 3);  
  363.   
  364.             //输出分词结果  
  365.             count++;  
  366.             cout << "----------------------------------------------" << endl;  
  367.             cout << "句子编号:" << count << endl;  
  368.             cout << endl;  
  369.             cout << "待分词的句子长度: " << strline_in.length() << "  句子:" << endl;  
  370.             cout << strline_in << endl;  
  371.             cout << endl;  
  372.             cout << "标准比对结果长度: " << strline_right.length() << "  句子:" << endl;  
  373.             cout << strline_right << endl;  
  374.             cout << endl;  
  375.             cout << "正向匹配分词长度: " << strline_out_1.length() << "  句子:" << endl;  
  376.             cout << strline_out_1 << endl;  
  377.             cout << endl;  
  378.             cout << "逆向匹配分词长度: " << strline_out_2.length() << "  句子:" << endl;  
  379.             cout << strline_out_2 << endl;  
  380.             cout << endl;  
  381.             cout << "最大概率分词长度: " << strline_out_3.length() << "  句子:" << endl;  
  382.             cout << strline_out_3 << endl;  
  383.             cout << endl;  
  384.   
  385.             //输出分词结果的数字序列表示  
  386.             vector<int> vec_right = getPos(strline_right, strline_in);  
  387.             vector<int> vec_out_1 = getPos(strline_out_1, strline_in);  
  388.             vector<int> vec_out_2 = getPos(strline_out_2, strline_in);  
  389.             vector<int> vec_out_3 = getPos(strline_out_3, strline_in);  
  390.   
  391.             cout << "标准结果:" << endl;  
  392.             for(int i = 0; i < vec_right.size(); i++){  
  393.                 cout << setw(4) << vec_right[i];  
  394.             }  
  395.             cout << endl;  
  396.             cout << "正向匹配结果:" << endl;  
  397.             for(int i = 0; i < vec_out_1.size(); i++){  
  398.                 cout << setw(4) << vec_out_1[i];  
  399.             }  
  400.             cout << endl;  
  401.             cout << "逆向匹配结果:" << endl;  
  402.             for(int i = 0; i < vec_out_2.size(); i++){  
  403.                 cout << setw(4) << vec_out_2[i];  
  404.             }  
  405.             cout << endl;  
  406.             cout << "最大概率结果:" << endl;  
  407.             for(int i = 0; i < vec_out_3.size(); i++){  
  408.                 cout << setw(4) << vec_out_3[i];  
  409.             }  
  410.             cout << endl;  
  411.   
  412.             //输出匹配的错误列表  
  413.             if(vec_right == vec_out_1 && vec_right == vec_out_2 && vec_right == vec_out_3){  
  414.                 count_0++;  
  415.             }  
  416.   
  417.             cout << endl;  
  418.             if(vec_right == vec_out_1){  
  419.                 cout << "正向最大匹配完全正确!" << endl;  
  420.                 count_1++;  
  421.             }else{  
  422.                 cout << "正向最大匹配错误列表:" << endl;  
  423.             }  
  424.             vector<int> vec_count_1 = getCount_2(strline_in, vec_right, vec_out_1, vec_err_1);  
  425.               
  426.             cout << endl;  
  427.             if(vec_right == vec_out_2){  
  428.                 cout << "逆向最大匹配完全正确!" << endl;  
  429.                 count_2++;  
  430.             }else{  
  431.                 cout << "逆向最大匹配错误列表:" << endl;  
  432.             }  
  433.             vector<int> vec_count_2 = getCount_2(strline_in, vec_right, vec_out_2, vec_err_2);  
  434.             cout << endl;  
  435.             if(vec_right == vec_out_3){  
  436.                 cout << "最大概率方法完全正确!" << endl;  
  437.                 count_3++;  
  438.             }else{  
  439.                 cout << "最大概率方法错误列表:" << endl;  
  440.             }  
  441.               
  442.             vector<int> vec_count_3 = getCount_2(strline_in, vec_right, vec_out_3, vec_err_3);  
  443.             cout << endl;  
  444.   
  445.             //准确的切分数量  
  446.             int count_right = vec_right.size();  
  447.             //切分得到的数量  
  448.             int count_out_1 = vec_out_1.size();  
  449.             int count_out_2 = vec_out_2.size();  
  450.             int count_out_3 = vec_out_3.size();  
  451.             //切分正确的数量  
  452.             int count_out_1_right = vec_count_1[0];  
  453.             int count_out_2_right = vec_count_2[0];  
  454.             int count_out_3_right = vec_count_3[0];  
  455.   
  456.             cout << "正向最大匹配:" << endl;    
  457.             cout << "  组合型歧义:" << vec_count_1[1] << endl;  
  458.             cout << "  未登录词语:" << vec_count_1[2] << endl;  
  459.             cout << "  交集型歧义:" << vec_count_1[3] << endl;  
  460.             cout << "逆向最大匹配:" << endl;    
  461.             cout << "  组合型歧义:" << vec_count_2[1] << endl;  
  462.             cout << "  未登录词语:" << vec_count_2[2] << endl;  
  463.             cout << "  交集型歧义:" << vec_count_2[3] << endl;  
  464.             cout << "最大概率方法:" << endl;    
  465.             cout << "  组合型歧义:" << vec_count_3[1] << endl;  
  466.             cout << "  未登录词语:" << vec_count_3[2] << endl;  
  467.             cout << "  交集型歧义:" << vec_count_3[3] << endl;  
  468.               
  469.             count_right_all += count_right;  
  470.             count_out_1_all += count_out_1;  
  471.             count_out_2_all += count_out_2;  
  472.             count_out_3_all += count_out_3;  
  473.             count_out_1_right_all += count_out_1_right;  
  474.             count_out_2_right_all += count_out_2_right;  
  475.             count_out_3_right_all += count_out_3_right;  
  476.             count_out_1_fail_1_all += vec_count_1[1];  
  477.             count_out_1_fail_2_all += vec_count_1[2];  
  478.             count_out_1_fail_3_all += vec_count_1[3];  
  479.             count_out_2_fail_1_all += vec_count_2[1];  
  480.             count_out_2_fail_2_all += vec_count_2[2];  
  481.             count_out_2_fail_3_all += vec_count_2[3];  
  482.             count_out_3_fail_1_all += vec_count_3[1];  
  483.             count_out_3_fail_2_all += vec_count_3[2];  
  484.             count_out_3_fail_3_all += vec_count_3[3];  
  485.               
  486.         }  
  487.     }  
  488.       
  489.     long time_2 = getCurrentTime();  
  490.     unsigned long file_size = getFileSize("test.txt");  
  491.   
  492.   
  493.     //打印错误的切分内容   
  494.     cout << endl;  
  495.     cout << "---------------------------------" << endl;  
  496.     cout << "错误样例(已排序):" << endl;  
  497.   
  498.     //选取样本(600个),去掉重复的  
  499.     //vector<string> vec_small(vec_err.begin(), vec_err.begin() + 600);  
  500.     //sort(vec_small.begin(), vec_small.end());  
  501.     //vector<string>::iterator end_unique = unique(vec_small.begin(), vec_small.end());  
  502.   
  503.     //对错误切分内容进行排序并掉重复的  
  504.     sort(vec_err_1.begin(), vec_err_1.end());  
  505.     sort(vec_err_2.begin(), vec_err_2.end());  
  506.     sort(vec_err_3.begin(), vec_err_3.end());  
  507.     vector<string>::iterator end_unique_1 = unique(vec_err_1.begin(), vec_err_1.end());  
  508.     vector<string>::iterator end_unique_2 = unique(vec_err_2.begin(), vec_err_2.end());  
  509.     vector<string>::iterator end_unique_3 = unique(vec_err_3.begin(), vec_err_3.end());  
  510.   
  511.     int num_1 = end_unique_1 - vec_err_1.begin();  
  512.     int num_2 = end_unique_2 - vec_err_2.begin();  
  513.     int num_3 = end_unique_3 - vec_err_3.begin();  
  514.   
  515.     cout << "----------------------------------" << endl;  
  516.     cout << "正向最大匹配切分错误数量:" << num_1 << endl;  
  517.     for(int i = 0; i < num_1; i++){  
  518.         cout << vec_err_1[i] << endl;  
  519.     }  
  520.     cout << endl;  
  521.   
  522.     cout << "----------------------------------" << endl;  
  523.     cout << "逆向最大匹配切分错误数量:" << num_2 << endl;  
  524.     for(int i = 0; i < num_2; i++){  
  525.         cout << vec_err_2[i] << endl;  
  526.     }  
  527.     cout << endl;  
  528.   
  529.     cout << "----------------------------------" << endl;  
  530.     cout << "最大概率方法切分错误数量:" << num_3 << endl;  
  531.     for(int i = 0; i < num_3; i++){  
  532.         cout << vec_err_3[i] << endl;  
  533.     }  
  534.     cout << endl;  
  535.   
  536.     //计算准确率和召回率  
  537.     double kk_1 = (double)count_out_1_right_all / count_out_1_all;  //正向最大匹配准确率  
  538.     double kk_2 = (double)count_out_1_right_all / count_right_all;  //正向最大匹配召回率  
  539.     double kk_3 = (double)count_out_2_right_all / count_out_2_all;  //逆向最大匹配准确率  
  540.     double kk_4 = (double)count_out_2_right_all / count_right_all;  //逆向最大匹配召回率  
  541.     double kk_5 = (double)count_out_3_right_all / count_out_3_all;  //最大概率方法准确率  
  542.     double kk_6 = (double)count_out_3_right_all / count_right_all;  //最大概率方法召回率  
  543.   
  544.     //集中输出结果  
  545.     cout << endl;  
  546.     cout << "---------------------------------" << endl;  
  547.     cout << "分词消耗时间:" << time_2 - time_1 << "ms" << endl;  
  548.     cout << "测试文件大小:" << file_size/1024 << " KB" << endl;  
  549.     cout << "分词速度为:  " << (double)file_size*1000/((time_2 - time_1)*1024) << " KB/s" << endl;  
  550.   
  551.     cout << endl;  
  552.     cout << "词典规模:" << word_dict.size << endl;  
  553.   
  554.     cout << endl;  
  555.     cout << "句子总数:" << count << endl;  
  556.     cout << "三种方法切分都正确的句子数目:   " << count_0 << "\t ( " << (double)count_0*100/count << " % )" << endl;  
  557.     cout << "正向最大匹配完全正确的句子数目: " << count_1 << "\t ( " << (double)count_1*100/count << " % )" << endl;  
  558.     cout << "逆向最大匹配完全正确的句子数目: " << count_2 << "\t ( " << (double)count_2*100/count << " % )" << endl;  
  559.     cout << "最大概率方法完全正确的句子数目: " << count_3 << "\t ( " << (double)count_3*100/count << " % )" << endl;  
  560.     cout << endl;  
  561.   
  562.     cout << "准确的切分总数:" << count_right_all << endl;        //准确的切分总数  
  563.     cout << "正向匹配切分总数:" << count_out_1_all << endl;       //正向匹配切分总数  
  564.     cout << "逆向匹配切分总数:" << count_out_2_all << endl;       //逆向匹配切分总数  
  565.     cout << "最大概率切分总数:" << count_out_3_all << endl;       //最大概率切分总数  
  566.     cout << "正向匹配切分正确总数:" << count_out_1_right_all << endl;   //正向匹配切分正确总数  
  567.     cout << "逆向匹配切分正确总数:" << count_out_2_right_all << endl;   //逆向匹配切分正确总数  
  568.     cout << "最大概率切分正确总数:" << count_out_3_right_all << endl;   //逆向匹配切分正确总数  
  569.   
  570.     cout << endl;  
  571.     cout << "正向最大匹配:" << endl;  
  572.     long count_out_1_fail_all = count_out_1_fail_1_all + count_out_1_fail_2_all + count_out_1_fail_3_all;     
  573.     cout << "  组合型歧义:" << count_out_1_fail_1_all << "\t ( " << (double)count_out_1_fail_1_all*100/count_out_1_fail_all << " % )" << endl;  
  574.     cout << "  未登录词语:" << count_out_1_fail_2_all << "\t ( " << (double)count_out_1_fail_2_all*100/count_out_1_fail_all << " % )" << endl;  
  575.     cout << "  交集型歧义:" << count_out_1_fail_3_all << "\t ( " << (double)count_out_1_fail_3_all*100/count_out_1_fail_all << " % )" << endl;  
  576.     cout << "逆向最大匹配:" << endl;    
  577.     long count_out_2_fail_all = count_out_2_fail_1_all + count_out_2_fail_2_all + count_out_2_fail_3_all;     
  578.     cout << "  组合型歧义:" << count_out_2_fail_1_all << "\t ( " << (double)count_out_2_fail_1_all*100/count_out_2_fail_all << " % )" << endl;  
  579.     cout << "  未登录词语:" << count_out_2_fail_2_all << "\t ( " << (double)count_out_2_fail_2_all*100/count_out_2_fail_all << " % )" << endl;  
  580.     cout << "  交集型歧义:" << count_out_2_fail_3_all << "\t ( " << (double)count_out_2_fail_3_all*100/count_out_2_fail_all << " % )" << endl;  
  581.     cout << "最大概率方法:" << endl;    
  582.     long count_out_3_fail_all = count_out_3_fail_1_all + count_out_3_fail_2_all + count_out_3_fail_3_all;     
  583.     cout << "  组合型歧义:" << count_out_3_fail_1_all << "\t ( " << (double)count_out_3_fail_1_all*100/count_out_3_fail_all << " % )" << endl;  
  584.     cout << "  未登录词语:" << count_out_3_fail_2_all << "\t ( " << (double)count_out_3_fail_2_all*100/count_out_3_fail_all << " % )" << endl;  
  585.     cout << "  交集型歧义:" << count_out_3_fail_3_all << "\t ( " << (double)count_out_3_fail_3_all*100/count_out_3_fail_all << " % )" << endl;  
  586.   
  587.     cout << endl;       
  588.     cout << "统计结果:" << endl;  
  589.     cout << "正向最大匹配    准确率:" << kk_1*100 << "%  \t召回率:" << kk_2*100 << "%" << endl;  
  590.     cout << "逆向最大匹配    准确率:" << kk_3*100 << "%  \t召回率:" << kk_4*100 << "%" << endl;  
  591.     cout << "最大概率方法    准确率:" << kk_5*100 << "%  \t召回率:" << kk_6*100 << "%" << endl;  
  592.   
  593.     return 0;  
  594. }  



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值