我写了RMM分词技术的一个类 我在测试的时候 资源管理器里的 httpd.exe的cpu能占到50%以上的资源,哪位高手能帮我解决一下这个问题吗?谢谢了

解决方案 »

  1.   

    <?php
    class SplitWord{
    var $TagDic = Array();
    var $RankDic = Array();
    var $SourceStr = '';
    var $ResultStr = '';
    var $SplitChar = ' ';
    var $SplitLen = 4;
    var $MaxLen = 7; 
    var $MinLen = 3;   function SplitWord(){
       $this->__construct();
      }
      
      function __construct(){     
       $dicfile = dirname(__FILE__)."/ppldic.csv"; 
       $fp = fopen($dicfile,'r');
       while($line = fgets($fp,256)){
         $ws = explode(' ',$line);
         $this->TagDic[$ws[0]] = $ws[1];
         $this->RankDic[strlen($ws[0])][$ws[0]] = $ws[2];
       }
       fclose($fp);
      }
     function Clear(){
       @fclose($this->QuickDic);
      }
      function SetSource($str){
       $this->SourceStr = $this->UpdateStr($str);
       $this->ResultStr = "";
      }
      function NotGBK($str)
      {
        if($str=="") return "";
       if( ord($str[0])>0x80 ) return false;
       else return true;
      }
      function SplitRMM($str=""){
       if($str!="") $this->SetSource($str);
       if($this->SourceStr=="") return "";
       $this->SourceStr = $this->UpdateStr($this->SourceStr);
       $spwords = explode(" ",$this->SourceStr);
       $spLen = count($spwords);
       $spc = $this->SplitChar;
       for($i=($spLen-1);$i>=0;$i--){
       if($spwords[$i]=="") continue;
       if($this->NotGBK($spwords[$i])){
       if(ereg("[^0-9\.\+\-]",$spwords[$i]))
       { $this->ResultStr = $spwords[$i].$spc.$this->ResultStr; }
       else
       {
       $nextword = "";
       @$nextword = substr($this->ResultStr,0,strpos($this->ResultStr,""));
       }
       }
       else
       {
         $c = $spwords[$i][0].$spwords[$i][1];
         $n = hexdec(bin2hex($c));
          if(strlen($spwords[$i]) <= $this->SplitLen)
          {
          }
          else
          { 
          $this->ResultStr = $this->RunRMM($spwords[$i]).$spc.$this->ResultStr;
          }
         }
       }
       return $this->ResultStr;
      }
      function RunRMM($str){
       $spc = $this->SplitChar;
       $spLen = strlen($str);
       $rsStr = "";
       $okWord = "";
       $tmpWord = "";
       $WordArray = Array();
       for($i=($spLen-1);$i>=0;){
       if($i<=$this->MinLen){
       if($i==1){
         $WordArray[] = substr($str,0,2);
         }else
       {
          $w = substr($str,0,$this->MinLen+1);
          if($this->IsWord($w)){
           $WordArray[] = $w;                                                                                  
          }else{
          $WordArray[] = substr($str,2,2);
          $WordArray[] = substr($str,0,2);
          }
         }
       $i = -1; break;
       }
       if($i>=$this->MaxLen) $maxPos = $this->MaxLen;
       else $maxPos = $i;
       $isMatch = false;
       for($j=$maxPos;$j>=0;$j=$j-2){
        $w = substr($str,$i-$j,$j+1);
        if($this->IsWord($w)){
         $WordArray[] = $w;
         $i = $i-$j-1;
         $isMatch = true;
         break;
        }
       }
       }
       $rsStr = $this->otherword($WordArray);
        $rsStr;
      }
      
    function otherword($WordArray){
       $wlen = count($WordArray)-1;
       $rsStr = "";
       $spc = $this->SplitChar;
       for($i=$wlen;$i>=0;$i--)
       {
    $rsStr .= $spc.$WordArray[$i]."、";
       }
    $rsStr = preg_replace("/^".$spc."/","、",$rsStr);
       return $rsStr;
      }
      
      function IsWord($okWord){
       $slen = strlen($okWord);
       if($slen > $this->MaxLen) return false;
       else return isset($this->RankDic[$slen][$okWord]);
      }
      
      function UpdateStr($str){
       $spc = $this->SplitChar;
        $slen = strlen($str);
        if($slen==0) return '';
        $okstr = '';
        $prechar = 0;
        for($i=0;$i<$slen;$i++){
          if(ord($str[$i]) < 0x81){
            if(ord($str[$i]) < 33){
              if($prechar!=0&&$str[$i]!="\r"&&$str[$i]!="\n") $okstr .= $spc;
              $prechar=0;
              continue; 
            }else if(ereg("[^0-9a-zA-Z@\.%#:/\\&_-]",$str[$i])){
              if($prechar==0){ $okstr .= $str[$i]; $prechar=3;}
              else{ $okstr .= $spc.$str[$i]; $prechar=3;}
            }else{
             if($prechar==2||$prechar==3)
             { $okstr .= $spc.$str[$i]; $prechar=1;}
             else
             { 
               if(ereg("@#%:",$str[$i])){ $okstr .= $str[$i]; $prechar=3; }
               else { $okstr .= $str[$i]; $prechar=1; }
             }
            }
          }
          else{
            if($prechar!=0 && $prechar!=2) $okstr .= $spc;
            if(isset($str[$i+1])){
              $c = $str[$i].$str[$i+1];
              
              $n = hexdec(bin2hex($c));
              if($n<0xA13F && $n > 0xAA40){
                 if($prechar!=0) $okstr .= $spc.$c;
                 else $okstr .= $c;
                 $prechar = 3; 
                }
              else{
                $okstr .= $c;
                $prechar = 2;
              }
              $i++;
            }
          }
        }
        return $okstr;
      }
    }
    $sp=new SplitWord();
    $sp->RunRMM('sfdsf');
    ?>
    这就是那个类
      

  2.   

    给你一个类,你自己看看吧。http://code.google.com/p/httpcws/
    HTTPCWS 是一款基于HTTP协议的开源中文分词系统,目前仅支持Linux系统。HTTPCWS 使用“ICTCLAS 3.0 2009共享版中文分词算法”的API进行分词处理,得出分词结果。
    ICTCLAS(Institute of Computing Technology, Chinese Lexical Analysis System)是中国科学院计算技术研究所在多年研究工作积累的基础上,基于多层隐马模型研制出的汉语词法分析系统,主要功能包括中文分词;词性标注;命名实体识别;新词识别;同时支持用户词典。ICTCLAS经过五年精心打造,内核升级6次,目前已经升级到了ICTCLAS3.0,分词精度98.45%,各种词典数据压缩后不到3M。ICTCLAS在国内973专家组组织的评测中活动获得了第一名,在第一届国际中文处理研究机构SigHan组织的评测中都获得了多项第一名,是当前世界上最好的汉语词法分析器。
    ICTCLAS 3.0 商业版是收费的,而免费提供的 ICTCLAS 3.0 共享版不开源,词库是根据人民日报一个月的语料得出的,很多词语不存在。所以本人补充的一个19万条词语的自定义词库,对ICTCLAS分词结果进行合并处理,输出最终分词结果。
    由于 ICTCLAS 3.0 2009 共享版只支持GBK编码,因此,如果是UTF-8编码的字符串,可以先用iconv函数转换成GBK编码,再用httpcws进行分词处理,最后转换回UTF-8编码。
    HTTPCWS 软件自身(包括httpcws.cpp源文件、dict/httpcws_dict.txt自定义词库)采用NewBSD开源协议,可以自由修改。HTTPCWS 使用的 ICTCLAS 共享版 API 及 dict/Data/ 目录内的语料库,版权及著作权归中国科学院计算技术研究所、ictclas.org所有,使用需遵循其相关协议。
    演示网址:http://blog.s135.com/demo/httpcws/