当前位置:Gxlcms > PHP教程 > RMM分词算法类

RMM分词算法类

时间:2021-07-01 10:21:17 帮助过:17人阅读

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]) <= $this->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($i<=$this->MinLen){
  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<$slen;$i++){
  144. if(ord($str[$i]) < 0x81){
  145. //英文的空白符号
  146. if(ord($str[$i]) < 33){
  147. if($prechar!=0&&$str[$i]!="\r"&&$str[$i]!="\n") $okstr .= $spc;
  148. $prechar=0;
  149. continue;
  150. }else if(preg_match("/[^0-9a-zA-Z@\.%#:\\&_-]/",$str[$i])){
  151. if($prechar==0){ $okstr .= $str[$i]; $prechar=3;}
  152. else{ $okstr .= $spc.$str[$i]; $prechar=3;}
  153. }else{
  154. if($prechar==2||$prechar==3)
  155. { $okstr .= $spc.$str[$i]; $prechar=1;}
  156. else
  157. {
  158. if(preg_match("/@#%:/",$str[$i])){ $okstr .= $str[$i]; $prechar=3; }
  159. else { $okstr .= $str[$i]; $prechar=1; }
  160. }
  161. }
  162. }
  163. else{
  164. //如果上一个字符为非中文和非空格,则加一个空格
  165. if($prechar!=0 && $prechar!=2) $okstr .= $spc;
  166. //如果中文字符
  167. if(isset($str[$i+1])){
  168. $c = $str[$i].$str[$i+1];
  169. $n = hexdec(bin2hex($c));
  170. if($n<0xA13F && $n > 0xAA40){
  171. if($prechar!=0) $okstr .= $spc.$c;
  172. else $okstr .= $c;
  173. $prechar = 3;
  174. }
  175. else{
  176. $okstr .= $c;
  177. $prechar = 2;
  178. }
  179. $i++;
  180. }
  181. }
  182. }
  183. return $okstr;
  184. }
  185. }
  186. // 调用
  187. $split=new SplitWord();
  188. echo $split->SplitRMM("php搜索技术");
  189. // 注意 ppldic.csv 词典的格式是 词语+空格+编号+n

人气教程排行