RMM分词算法类

WBOY
WBOYasal
2016-07-25 08:47:54984semak imbas
RMM分词算法类
  1. //RMM分词算法
  2. class SplitWord{
  3. var $TagDic = Array();
  4. var $RankDic = Array();
  5. var $SourceStr = '';
  6. var $ResultStr = '';
  7. var $SplitChar = ' '; //分隔符
  8. var $SplitLen = 4; //保留词长度
  9. var $MaxLen = 7; //词典最大中文字,这里的数值为字节数组的最大索引
  10. var $MinLen = 3; //最小中文字,这里的数值为字节数组的最大索引
  11. function SplitWord(){
  12. $this->__construct();
  13. }
  14. function __construct(){
  15. //高级分词,预先载入词典以提分词高速度
  16. $dicfile = dirname(__FILE__)."/ppldic.csv";
  17. $fp = fopen($dicfile,'r'); //读取词库中的词
  18. while($line = fgets($fp,256)){
  19. $ws = explode(' ',$line); //对词库中的词进行拆分
  20. $this->TagDic[$ws[0]] = $ws[1];
  21. $this->RankDic[strlen($ws[0])][$ws[0]] = $ws[2];
  22. }
  23. fclose($fp); //关闭词库文件
  24. }
  25. //析放资源
  26. function Clear(){
  27. @fclose($this->QuickDic);
  28. }
  29. //设置源字符串
  30. function SetSource($str){
  31. $this->SourceStr = $this->UpdateStr($str);
  32. $this->ResultStr = "";
  33. }
  34. //检查字符串是否不存在中文
  35. function NotGBK($str)
  36. {
  37. if($str=="") return "";
  38. if( ord($str[0])>0x80 ) return false;
  39. else return true;
  40. }
  41. //RMM分词算法
  42. function SplitRMM($str=""){
  43. if($str!="") $this->SetSource($str);
  44. if($this->SourceStr=="") return "";
  45. $this->SourceStr = $this->UpdateStr($this->SourceStr);
  46. $spwords = explode(" ",$this->SourceStr);
  47. $spLen = count($spwords);
  48. $spc = $this->SplitChar;
  49. for($i=($spLen-1);$i>=0;$i--){
  50. if($spwords[$i]=="") continue;
  51. if($this->NotGBK($spwords[$i])){
  52. if(preg_match("/[^0-9\.\+\-]/",$spwords[$i]))
  53. { $this->ResultStr = $spwords[$i].$spc.$this->ResultStr; }
  54. else
  55. {
  56. $nextword = "";
  57. @$nextword = substr($this->ResultStr,0,strpos($this->ResultStr,""));
  58. }
  59. }
  60. else
  61. {
  62. $c = $spwords[$i][0].$spwords[$i][1];
  63. $n = hexdec(bin2hex($c));
  64. if(strlen($spwords[$i]) SplitLen)
  65. {
  66. }
  67. else
  68. {
  69. $this->ResultStr = $this->RunRMM($spwords[$i]).$spc.$this->ResultStr;
  70. }
  71. }
  72. }
  73. return $this->ResultStr;
  74. }
  75. //对全中文字符串进行逆向匹配方式分解
  76. function RunRMM($str){
  77. $spc = $this->SplitChar;
  78. $spLen = strlen($str);
  79. $rsStr = "";
  80. $okWord = "";
  81. $tmpWord = "";
  82. $WordArray = Array();
  83. //逆向字典匹配
  84. for($i=($spLen-1);$i>=0;){
  85. //当i达到最小可能词的时候
  86. if($iMinLen){
  87. if($i==1){
  88. $WordArray[] = substr($str,0,2);
  89. }else
  90. {
  91. $w = substr($str,0,$this->MinLen+1);
  92. if($this->IsWord($w)){
  93. $WordArray[] = $w;
  94. }else{
  95. $WordArray[] = substr($str,2,2);
  96. $WordArray[] = substr($str,0,2);
  97. }
  98. }
  99. $i = -1; break;
  100. }
  101. //分析在最小词以上时的情况
  102. if($i>=$this->MaxLen) $maxPos = $this->MaxLen;
  103. else $maxPos = $i;
  104. $isMatch = false;
  105. for($j=$maxPos;$j>=0;$j=$j-2){
  106. $w = substr($str,$i-$j,$j+1);
  107. if($this->IsWord($w)){
  108. $WordArray[] = $w;
  109. $i = $i-$j-1;
  110. $isMatch = true;
  111. break;
  112. }
  113. }
  114. }
  115. $rsStr = $this->otherword($WordArray);
  116. return $rsStr;
  117. }
  118. function otherword($WordArray){
  119. $wlen = count($WordArray)-1; //计算数组的元素个数
  120. $rsStr = ""; //初始化变量
  121. $spc = $this->SplitChar;
  122. for($i=$wlen;$i>=0;$i--)
  123. {
  124. $rsStr .= $spc.$WordArray[$i]."、"; //将数组为顿号进行拆分
  125. }
  126. //返回本段分词结果
  127. $rsStr = preg_replace("/^".$spc."/","、",$rsStr);
  128. return $rsStr;
  129. }
  130. //判断词典里是否存在某个词
  131. function IsWord($okWord){
  132. $slen = strlen($okWord);
  133. if($slen > $this->MaxLen) return false;
  134. else return isset($this->RankDic[$slen][$okWord]);
  135. }
  136. //整理字符串(对标点符号,中英文混排等初步处理)
  137. function UpdateStr($str){
  138. $spc = $this->SplitChar;
  139. $slen = strlen($str);
  140. if($slen==0) return '';
  141. $okstr = '';
  142. $prechar = 0; // 0-空白 1-英文 2-中文 3-符号
  143. for($i=0;$i if(ord($str[$i]) //英文的空白符号
  144. if(ord($str[$i]) if($prechar!=0&&$str[$i]!="\r"&&$str[$i]!="\n") $okstr .= $spc;
  145. $prechar=0;
  146. continue;
  147. }else if(preg_match("/[^0-9a-zA-Z@\.%#:\\&_-]/",$str[$i])){
  148. if($prechar==0){ $okstr .= $str[$i]; $prechar=3;}
  149. else{ $okstr .= $spc.$str[$i]; $prechar=3;}
  150. }else{
  151. if($prechar==2||$prechar==3)
  152. { $okstr .= $spc.$str[$i]; $prechar=1;}
  153. else
  154. {
  155. if(preg_match("/@#%:/",$str[$i])){ $okstr .= $str[$i]; $prechar=3; }
  156. else { $okstr .= $str[$i]; $prechar=1; }
  157. }
  158. }
  159. }
  160. else{
  161. //如果上一个字符为非中文和非空格,则加一个空格
  162. if($prechar!=0 && $prechar!=2) $okstr .= $spc;
  163. //如果中文字符
  164. if(isset($str[$i+1])){
  165. $c = $str[$i].$str[$i+1];
  166. $n = hexdec(bin2hex($c));
  167. if($n 0xAA40){
  168. if($prechar!=0) $okstr .= $spc.$c;
  169. else $okstr .= $c;
  170. $prechar = 3;
  171. }
  172. else{
  173. $okstr .= $c;
  174. $prechar = 2;
  175. }
  176. $i++;
  177. }
  178. }
  179. }
  180. return $okstr;
  181. }
  182. }
  183. // 调用
  184. $split=new SplitWord();
  185. echo $split->SplitRMM("php搜索技术");
  186. // 注意 ppldic.csv 词典的格式是 词语+空格+编号+n
复制代码


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn