您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > 中文分词处理源代码C++
#includefstream#includevector#includestring#includeiostreamusingnamespacestd;constintSTART1=0XB0,START2=0XA1,END1=0XF8,END2=0XFF;constintMAXWORDLEN=48;ifstreamfin(segdict.txt);ofstreamout(out1.txt);//---------------------建树部分----------------------structNode3{stringS;boolIsWord;Node3*L,*R;Node3(strings=,boolisWord=0,Node3*l=0,Node3*r=0):S(s),IsWord(isWord),L(l),R(r){}};structNode2{stringS;boolIsWord;Node3*Child;Node2(strings=,boolisWord=0,Node3*child=0):S(s),IsWord(isWord),Child(child){}};structNode{stringS;vectorNode2v;};vectorNodeDic;intHASH[END1-START1][END2-START2];voidBegin()//初始化{for(inti=0;iEND1-START1;i++)for(intj=0;jEND2-START2;j++)HASH[i][j]=-1;}voidBuildTree(strings,Node3*child){intlen=s.length();stringt=s.substr(0,2);Node3*LAST=child;if(child==0)LAST=child=newNode3(t,(len==2),0,0);else{while(LAST-L!=0)LAST=LAST-L;if(LAST-S!=t){LAST-L=newNode3(t,(len==2),0,0);LAST=LAST-L;}}if(len2)BuildTree(s.substr(2,MAXWORDLEN),LAST-R);}voidDictionary()//构造整个结构{Begin();strings;intN,k=0;while(fins){Noden;n.S=s.substr(0,2);intm1=(unsignedchar)s[0]-START1;intm2=(unsignedchar)s[1]-START2;HASH[m1][m2]=k++;outsHASH[m1][m2]endl;finN;outNendl;for(inti=0;iN;i++){fins;outsendl;stringt=s.substr(2,2);intLEN=s.length();intSIZE=n.v.size();intLen=s.length();if(SIZE==0||(SIZE0&&n.v[SIZE-1].S!=t))n.v.push_back(Node2(t,(Len==4),0));SIZE=n.v.size();if(Len4)BuildTree(s.substr(4,MAXWORDLEN),n.v[SIZE-1].Child);}Dic.push_back(n);}outENDHASHendlendl;}//------------------查询部分---------------------vectorstringDest;intBinarySearch(intx,stringSec)//二分查找第二个字{intL=0,R=Dic[x].v.size()-1;while(L=R){intmid=(L+R)1;if(Dic[x].v[mid].S==Sec)returnmid;elseif(Dic[x].v[mid].SSec)L=mid+1;elseR=mid-1;}return-1;}Node3*RemainSearch(Node3*p,stringcc)//顺序查找剩下的字{while(p!=0){if(p-S==cc)returnp;elsep=p-L;}return0;}unsignedCharToInt(charc){returnunsigned((unsignedchar)c);}boolIsCC(charc){unsignedval=CharToInt(c);returnval=START1&&valEND1;}boolIsEC(charc){unsignedval=CharToInt(c);returnval0x80;}voidFindNum(stringsrc,vectorstring&dest,int&StarPos,int&EndPos){intStrlen=src.length();while(EndPosStrlen&&!IsCC(src[EndPos])){if(!IsCC(EndPos))EndPos++;EndPos++;}if(EndPosStarPos){dest.push_back(src.substr(StarPos,EndPos-StarPos));StarPos=EndPos;}}voidSegment(stringsrc,vectorstring&dest){intStrLen=src.length();intStartPos=0,EndPos;while(StartPosStrLen){EndPos=StartPos;FindNum(src,dest,StartPos,EndPos);if(StartPos=StrLen)return;unsignedSegLen=2;stringHeadCC=src.substr(StartPos,2);coutHeadCCendlendl;intm1=(unsignedchar)HeadCC[0]-START1;intm2=(unsignedchar)HeadCC[1]-START2;intHeadIndex=HASH[m1][m2];if(HeadIndex=0);{stringSecCC=src.substr(StartPos+2,2);if(SecCC.length()0&&IsCC(SecCC[0])){intB2=BinarySearch(HeadIndex,SecCC);if(B2=0){if(Dic[HeadIndex].v[B2].IsWord)SegLen+=2;EndPos=StartPos+4;Node3*p=Dic[HeadIndex].v[B2].Child;while(EndPosStrLen&&(p=RemainSearch(p,src.substr(EndPos,2)))!=0){EndPos+=2;if(p-IsWord)SegLen=EndPos-StartPos;p=p-R;}}}}dest.push_back(src.substr(StartPos,SegLen));StartPos+=SegLen;}}intmain(){Dictionary();ofstreamout2(out2.txt);//stringSS=有时,我会抬头,看一看这喧嚣的人群,有没有我想见得身影,若是有那身影,或许我会看着她,看她慢慢的融入人群,直到不见。然后我会低下头,走着我的道。;//stringSS=中华人民万岁;stringSS=程序编码基本正确,实现了程序设计中提到的两种分词策略,分词结果就在预料之中。;//stringSS=在词典中对于特定的首字,前两字相同的词条很少,前三字相同的词条更少。当我们以这种形式组织词典后,除子表的第一层外,各个节点的兄弟数目都很小,对它们的查找采用顺序查找方法较为适宜。;//stringSS=主要分为两大模块:一个建立一棵树,一个是查询。建树有三个层次,第一层是HASH表,第二层是数组,用于二分查找使用,第三层是二叉树。查询分为直接查询第一层的HASH表,第二层用二分查找(第二层汉子相同的平均概率是26,一般第二字成词切相同),第三层直接顺序查找,以及查找句子中的数字和汉子标点。;Segment(SS,Dest);intLEN=Dest.size();for(inti=0;iLEN;i++)out2Dest[i]endl;system(Pause);return0;}//---------------------------------------------------------------------------
本文标题:中文分词处理源代码C++
链接地址:https://www.777doc.com/doc-5367538 .html