A complete example of Chinese partial speech in PHP implementation

Source: Internet
Author: User
Tags explode ord rtrim
This article mainly introduces the implementation of PHP in Chinese parts of speech, combined with a complete example of PHP based on string traversal, conversion, arithmetic and other techniques to achieve Chinese word segmentation function of the specific method, the need for friends can refer to the next

Specific as follows:

The Chinese division of Speech source code using Http://tools.jb51.net/code/jb51_php_format for the format of processing, easy to read. The specific code is as follows:

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 ("/([\!\" \#\$\%\&\ ' \ (\) \*\+\,\-\.\/\ : \;\<\=\>\?\@\[\\\\\]\^\_\ ' \{\|\}\~\t\f]+)/"," $ ", $str _final_tmp); $str _final_tmp = Preg_replace ("/([\!\" \#\$\%\&\ ' \ (\) \*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\\\]\^\_\ ' \{\|\}\~\t\f]) ([\!\ "\#\$\%\&\ ' \ (\) \*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\\\]\^\_\ ' \{\|\}\~\t        \f])/"," $ $ ", $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; }}?>

The above is the whole content of this article, I hope that everyone's study has helped.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.