一个用PHP写的中文分词函数

<?php
class Segmentation {
   
var $options = array("lowercase" => TRUE,

数据挖掘工具


                        
"segment_english" => FALSE);
   
var $dict_name = "Unknown";
   
var $dict_words = array();
   
function setLowercase($value) {
       
if ($value) { 数据挖掘交友
           
$this->options["lowercase"] = TRUE;
        }
else { 数据挖掘交友
           
$this->options["lowercase"] = FALSE;
        }
       
return TRUE;
    }
   
function setSegmentEnglish($value) {
       
if ($value) {
           
$this->options["segment_english"] = TRUE; 数据挖掘论坛
        }
else {
           
$this->options["segment_english"] = FALSE;

数据挖掘实验室


        }
       
return TRUE;
    }
   
function load($dict_file) {
       
if (!file_exists($dict_file)) { 数据挖掘论坛
           
return FALSE;
        }
       
$fp = fopen($dict_file, "r");

数据挖掘交友


       
$temp = fgets($fp, 1024);
       
if ($temp === FALSE) { 数据挖掘工具
           
return FALSE;
        }
else {
           
if (strpos($temp, " ") !== FALSE) { 数据挖掘工具
               
list ($dict_type, $dict_name) = explode(" ", trim($temp));
            }
else {
               
$dict_type = trim($temp);
               
$dict_name = "Unknown"; 数据挖掘论坛
            }
           
$this->dict_name = $dict_name;
           
if ($dict_type !== "DICT_WORD_W") { 数据挖掘实验室
               
return FALSE;
            }
        }
       
while (!feof($fp)) { 数据挖掘实验室
           
$this->dict_words[rtrim(fgets($fp, 32))] = 1; 数据挖掘交友
        }
       
fclose($fp);
       
return TRUE;
    }
   
function getDictName() {
       
return $this->dict_name; 数据挖掘交友
    }
   
function segmentString($str) {
       
if (count($this->dict_words) === 0) { 数据挖掘研究院
           
return FALSE;
        }
       
$lines = explode(" ", $str);

数据挖掘实验室


       
return $this->_segmentLines($lines);
    }
   
function segmentFile($filename) {
       
if (count($this->dict_words) === 0) {

数据挖掘工具


           
return FALSE;
        }
       
$lines = file($filename); 数据挖掘论坛
       
return $this->_segmentLines($lines);
    }
   
function _segmentLines($lines) {
       
$contents_segmented = ""; 数据挖掘交友
       
foreach ($lines as $line) {
           
$contents_segmented .= $this->_segmentLine(rtrim($line)) . " "; 数据挖掘实验室
        }
       
do {
           
$contents_segmented = str_replace("  ", " ", $contents_segmented); 数据挖掘研究院
        }
while (strpos($contents_segmented, "  ") !== FALSE);

数据挖掘论坛


       
return $contents_segmented;
    }
   
function _segmentLine($str) {
       
$str_final = ""; 数据挖掘研究院
       
$str_array = array();
       
$str_length = strlen($str); 数据挖掘实验室
       
if ($str_length > 0) {
           
if (ord($str{$str_length-1}) >= 129) {

数据挖掘交友


               
$str .= " ";
            }
        }
       
for ($i=0; $i<$str_length; $i++) {
           
if (ord($str{$i}) >= 129) {
               
$str_array[] = $str{$i} . $str{$i+1}; 数据挖掘交友
               
$i++;
            }
else {
               
$str_tmp = $str{$i}; 数据挖掘研究院
               
for ($j=$i+1; $j<$str_length; $j++) { 数据挖掘研究院
                   
if (ord($str{$j}) < 129) {
                       
$str_tmp .= $str{$j};
                    }
else {
                       
break;
                    }
                }
               
$str_array[] = array($str_tmp);
               
$i = $j - 1;
            }
        }
 
       
$pos = count($str_array);
       
while ($pos > 0) {
           
$char = $str_array[$pos-1];
           
if (is_array($char)) {
               
$str_final_tmp = $char[0]; 数据挖掘工具
               
if ($this->options["segment_english"]) {
                   
$str_final_tmp = preg_replace("/([!"#$\%&"()*+,-./:;<=>?@[\\]^\_`{|}~ f]+)/", " $1 ", $str_final_tmp);

数据挖掘交友


                   
$str_final_tmp = preg_replace("/([!"#$\%&"()*+,-./:;<=>?@[\\]^\_`{|}~ f])([!"#$\%&"()*+,-./:;<=>?@[\\]^\_`{|}~ f])/", " $1 $2 ", $str_final_tmp);
                }
               
if ($this->options["lowercase"]) {
                   
$str_final_tmp = strtolower($str_final_tmp); 数据挖掘工具
                }
               
$str_final = " $str_final_tmp$str_final";
               
$pos--; 数据挖掘研究院
            }
else {
               
$word_found = 0;
               
$word_array = array(0 => "");

数据挖掘工具


               
if ($pos < 4) {
                   
$word_temp = $pos + 1;
                }
else {
                   
$word_temp = 5;
                }
               
for ($i=1; $i<$word_temp; $i++) { 数据挖掘实验室
                   
$word_array[$i] = $str_array
[数据挖掘专家] [数据挖掘研究院] [数据挖掘论坛] [数据挖掘实验室]
上一篇:mysql方式操作文本数据库
下一篇:PHP网站漏洞的相关总结
最新评论共有 0 位网友发表了评论 , 查看所有评论
发表评论( 不能超过250字,需审核,请自觉遵守互联网相关政策法规。 )
匿名?
数据挖掘网站导航 数据挖掘论坛导航
  • 数据挖掘工具
  • 数据挖掘论坛
  • DataCruncher - Cognos
  • MineSet - MathSoft
  • Intelligent Miner - GainSmarts
  • Sqlserver - SAS - Clementine
  • CART - Weka - WizSoft
  • NeuroShell - ModelQuest
  • data mining tools - Darwin
  • 数据挖掘交友
  • 数据挖掘博客
  • 数据挖掘工具
  • 数据挖掘资源
  • 数据挖掘技术算法
  • 数据挖掘相关期刊、会议
  • 研究院联盟合作专区
  • 数据挖掘基础与相关技术
  • 数据挖掘厂商与就业
  • 数据挖掘研究者乐园
  • 知名厂商数据挖掘工具资料
  • 国内数据挖掘实验室
  • Foreign Data Mining Lab
  • 热点关注
  • P2P Search Engines-Napster
  • Peer to Peer ( P2P ) 综述
  • P2P Search Engines-Introduction
  • P2P Routing
  • P2P Search Engines-Music and P2P
  • P2P Search Engines-Historical Developmen
  • P2P Security
  • 如何从一个php文件向另一个地址post数据,
  • 基于PHP的AJAX技术实现文件异步上传
  • Copyright and P2P
  • 论坛最新话题
  • Foundations of Statistical Natural Langu
  • Game Theory meet Data Mining: A Recent P
  • System Building: How does it help or hin
  • 数据挖掘与Clementine培训
  • 新手报到
  • 求 SASEM 客户流失预测分析
  • 数据挖掘工程师/搜索研究院—北京——无线
  • 数据挖掘入门介绍(如何着手数据挖掘)
  • Information Overload Survey Results
  • The INEX 2005 Workshop on Element Retrie
  • 相关资讯
  • P2P Search Engines-Introduction
  • P2P Search Engines-Music and P2P
  • P2P Search Engines-Historical Developmen
  • Copyright and P2P
  • P2P Search Engines-Napster
  • GNUtella
  • YouServ ?An Analysis
  • Freenet
  • P2P Search Engines
  • P2P Routing
  • 数据挖掘实验室资料
  • 数据挖掘博客地址
  • 数据挖掘实验室网站地址
  • Prepare for Medicare audits by using dat
  • 注册成为SAS用户与爱好者俱乐部会员
  • 水南梅
  • 明日烟
  • 新人报道
  • 下载
  • 厦门服务器托管,450元/月—0592-5177319 高
  • 买空间送域名--0592-5177319 高静