[转]一个用php写的中文分词类

本文介绍了一个使用PHP编写的中文分词类,该类支持加载词典文件、设置是否转换为小写及是否切分英文字符串等功能。通过实例化此类并调用相应的方法可以实现对中文文本的有效分词。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一个用php写的中文分词类

  1. <?php
  2. classSegmentation{
  3. var$options=array('lowercase'=>TRUE,
  4. 'segment_english'=>FALSE);
  5. var$dict_name='Unknown';
  6. var$dict_words=array();
  7. functionsetLowercase($value){
  8. if($value){
  9. $this->options['lowercase']=TRUE;
  10. }else{
  11. $this->options['lowercase']=FALSE;
  12. }
  13. returnTRUE;
  14. }
  15. functionsetSegmentEnglish($value){
  16. if($value){
  17. $this->options['segment_english']=TRUE;
  18. }else{
  19. $this->options['segment_english']=FALSE;
  20. }
  21. returnTRUE;
  22. }
  23. functionload($dict_file){
  24. if(!file_exists($dict_file)){
  25. returnFALSE;
  26. }
  27. $fp=fopen($dict_file,'r');
  28. $temp=fgets($fp,1024);
  29. if($temp===FALSE){
  30. returnFALSE;
  31. }else{
  32. if(strpos($temp,"\t")!==FALSE){
  33. list($dict_type,$dict_name)=explode("\t",trim($temp));
  34. }else{
  35. $dict_type=trim($temp);
  36. $dict_name='Unknown';
  37. }
  38. $this->dict_name=$dict_name;
  39. if($dict_type!=='DICT_WORD_W'){
  40. returnFALSE;
  41. }
  42. }
  43. while(!feof($fp)){
  44. $this->dict_words[rtrim(fgets($fp,32))]=1;
  45. }
  46. fclose($fp);
  47. returnTRUE;
  48. }
  49. functiongetDictName(){
  50. return$this->dict_name;
  51. }
  52. functionsegmentString($str){
  53. if(count($this->dict_words)===0){
  54. returnFALSE;
  55. }
  56. $lines=explode("\n",$str);
  57. return$this->_segmentLines($lines);
  58. }
  59. functionsegmentFile($filename){
  60. if(count($this->dict_words)===0){
  61. returnFALSE;
  62. }
  63. $lines=file($filename);
  64. return$this->_segmentLines($lines);
  65. }
  66. function_segmentLines($lines){
  67. $contents_segmented='';
  68. foreach($linesas$line){
  69. $contents_segmented.=$this->_segmentLine(rtrim($line))."\n";
  70. }
  71. do{
  72. $contents_segmented=str_replace('','',$contents_segmented);
  73. }while(strpos($contents_segmented,'')!==FALSE);
  74. return$contents_segmented;
  75. }
  76. function_segmentLine($str){
  77. $str_final='';
  78. $str_array=array();
  79. $str_length=strlen($str);
  80. if($str_length>0){
  81. if(ord($str{$str_length-1})>=129){
  82. $str.='';
  83. }
  84. }
  85. for($i=0;$i<$str_length;$i++){
  86. if(ord($str{$i})>=129){
  87. $str_array[]=$str{$i}.$str{$i+1};
  88. $i++;
  89. }else{
  90. $str_tmp=$str{$i};
  91. for($j=$i+1;$j<$str_length;$j++){
  92. if(ord($str{$j})<129){
  93. $str_tmp.=$str{$j};
  94. }else{
  95. break;
  96. }
  97. }
  98. $str_array[]=array($str_tmp);
  99. $i=$j-1;
  100. }
  101. }
  102. $pos=count($str_array);
  103. while($pos>0){
  104. $char=$str_array[$pos-1];
  105. if(is_array($char)){
  106. $str_final_tmp=$char[0];
  107. if($this->options['segment_english']){
  108. $str_final_tmp=preg_replace("/([\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\\\]\^\_\`\{\|\}\~\t\f]+)/","$1",$str_final_tmp);
  109. $str_final_tmp=preg_replace("/([\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\\\]\^\_\`\{\|\}\~\t\f])([\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\\\]\^\_\`\{\|\}\~\t\f])/","$1$2",$str_final_tmp);
  110. }
  111. if($this->options['lowercase']){
  112. $str_final_tmp=strtolower($str_final_tmp);
  113. }
  114. $str_final="$str_final_tmp$str_final";
  115. $pos--;
  116. }else{
  117. $word_found=0;
  118. $word_array=array(0=>'');
  119. if($pos<4){
  120. $word_temp=$pos+1;
  121. }else{
  122. $word_temp=5;
  123. }
  124. for($i=1;$i<$word_temp;$i++){
  125. $word_array[$i]=$str_array[$pos-$i].$word_array[$i-1];
  126. }
  127. for($i=($word_temp-1);$i>1;$i--){
  128. if(array_key_exists($word_array[$i],$this->dict_words)){
  129. $word_found=$i;
  130. break;
  131. }
  132. }
  133. if($word_found){
  134. $str_final="$word_array[$word_found]$str_final";
  135. $pos=$pos-$word_found;
  136. }else{
  137. $str_final="$char$str_final";
  138. $pos--;
  139. }
  140. }
  141. }
  142. return$str_final;
  143. }
  144. }
  145. ?>

来源参考:
http://www.phpchina.cn/code/2006/0607/381.html
http://www.xuchao.cn/?play=reply&id=851

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值