Excel的if可以嵌套多少層 if函數(shù)嵌套多層舉例說明
1117
2023-11-12
本文實(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ì)有所幫助。
#免責(zé)聲明#
本站[綠夏技術(shù)導(dǎo)航]提供的一切軟件、教程和內(nèi)容信息僅限用于學(xué)習(xí)和研究目的;不得將上述內(nèi)容用于商業(yè)或者非法用途,否則,一切后果請用戶自負(fù)。本站信息來自網(wǎng)絡(luò)收集整理,版權(quán)爭議與本站無關(guān)。您必須在下載后的24個(gè)小時(shí)之內(nèi),從您的電腦或手機(jī)中徹底刪除上述內(nèi)容。如果您喜歡該程序或內(nèi)容,請支持正版,購買注冊,得到更好的正版服務(wù)。我們非常重視版權(quán)問題,如有侵權(quán)請郵件[admin@lxwl520.com]與我們聯(lián)系進(jìn)行刪除處理。敬請諒解!