成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導(dǎo)航首頁 ? 技術(shù)教程 ? php實(shí)現(xiàn)的中文分詞類完整實(shí)例
全站頭部文字 我要出現(xiàn)在這里
php實(shí)現(xiàn)的中文分詞類完整實(shí)例 750 2024-01-10   

本文實(shí)例講述了php實(shí)現(xiàn)的中文分詞類。分享給大家供大家參考,具體如下:

該中文分詞類源碼使用http://tools.gimoo.net/code/gimoo_php_format進(jìn)行了格式化處理,便于閱讀。具體代碼如下:

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, "t") !== FALSE) {
        list ($dict_type, $dict_name) = explode("t", 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("n", $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)) . " n";
    }
    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("/([!"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~tf]+)/", " $1 ", $str_final_tmp); 
$str_final_tmp = preg_replace("/([!"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~tf])([!"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~tf])/", " $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[$pos-$i] . $word_array[$i-1];
        }
        for ($i=($word_temp-1); $i>1; $i--) {
          if (array_key_exists($word_array[$i], $this->dict_words)) {
            $word_found = $i;
            break;
          }
        }
        if ($word_found) {
          $str_final = " $word_array[$word_found]$str_final";
          $pos = $pos - $word_found;
        } else {
          $str_final = " $char$str_final";
          $pos--;
        }
      }
    }
    return $str_final;
  }
}
?>

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php常用函數(shù)與技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。



主站蜘蛛池模板: 456电影网络重口味456| 双妻艳| 奇妙的植物世界阅读短文答案| 基础设施建设产业市场 | 河东狮吼 电视剧| 水牛城66| 《感恩的心》儿童朗诵| 林青霞离婚| 张志忠演员| 新贵妃醉酒简谱| 在线播放啄木乌丝袜秘书| 井冈山必去的三个景点 | 《世说新语》二则原文及注释 | 高达剧场版| 广州打折网| 红灯区| 洪金宝电影| 老牛家的战争电视剧全集免费观看| 庆余年2演员表全部员表| 青春派电视剧免费完整版在线观看| 在爱的名义下| 山东生活频道| 吴薇| 亚洲第一区se| 熊乃瑾个人资料| 青春没有遗憾才完美辩论资料| 李乃文电影| squirt cytherea video| 《白蛇传说》| 丛林之王| 奇奇颗颗说恐龙| 巴霍巴利王2国语版在线观看免费 惊弦电视剧完整版免费观看高清 梦的衣裳 | 拇指姑娘故事完整版| 美少女战士cosmos| 玉匣记全文免费| 寡妇电影完整版免费观看| 秀人网 官网门户免费| 山楂树之恋电影剧情简介| 3片| 美国派7| 感恩节英语祝福|

!!!站長長期在線接!!!

網(wǎng)站、小程序:定制開發(fā)/二次開發(fā)/仿制開發(fā)等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103