For Chinese search engines, Chinese Word Segmentation is one of the most basic parts of the system, because the Chinese word-based Chinese search algorithm is not very good at present. of course, this article is not about researching Chinese search engines, but about using PHP as an in-site search engine. this is an article in this system.
The word segmentation tool I used is the open-source ICTCLAS of the institute of Computing Science and Technology of the Chinese Emy of sciences. In addition, the open-source bamboo tool will be used for further research.
Starting from ICTCLAS, it is a good choice, because its algorithms are widely spread, there are open academic documents, and the compilation is simple, with less library dependencies. currently, only C/C ++, Java, and C # versions are provided, and PHP version code is not available. what should we do? You may be able to learn its C/C ++ source code and academic documents, and then develop a PHP version. however, I want to use inter-process communication to call the executable files of C/C ++ in PHP code.
After downloading and decompressing the source code, make ICTCLAS directly on the machine with the c ++ Development Library and compiling environment. its makefile script has an error '. /', of course, cannot be executed successfully in windows. however, compilation results are not affected.
The PHP class for Chinese Word Segmentation is located below. The proc_open () function is used to execute the word segmentation program and interact with the program through pipelines. The text to be segmented is input to read the word segmentation result.
<? Phpclass NLP {Private Static $ export _path; // static function set_export _path ($ PATH) {self ::$ export _path = $ path;} does not end ;} private function cmd ($ Str) {$ descriptorspec = array (0 => array ("pipe", "R"), 1 => array ("pipe ", "W"),); $ cmd = self: $ cmd_path. "/ICTCLAS"; $ process = proc_open ($ cmd, $ descriptorspec, $ pipes); If (is_resource ($ process) {$ STR = iconv ('utf-8 ', 'gbk', $ Str); fwrite ($ pipes [0], $ Str ); $ Output = stream_get_contents ($ pipes [1]); fclose ($ pipes [0]); fclose ($ pipes [1]); $ return_value = proc_close ($ process );} /* $ cmd = "printf' $ input '| ". SELF: $ pai_path. "/ICTCLAS"; Exec ($ cmd, $ output, $ RET); $ output = join ("\ n", $ output ); */$ output = trim ($ output); $ output = iconv ('gbk', 'utf-8', $ output); return $ output ;} /*** to perform word segmentation and return the word list. */function tokenize ($ Str) {$ tokens = array (); $ out Put = self: cmd ($ input); if ($ output) {$ PS = preg_split ('/\ s +/', $ output ); foreach ($ PS as $ p) {list ($ seg, $ tag) = explode ('/', $ P ); $ item = array ('seg' =>$ seg, 'tag' =>$ tag,); $ tokens [] = $ item ;}}return $ tokens ;}} NLP: set_cmd_path (dirname (_ file _);?>
It is easy to use (ensure that the executable files and dictionaries after ICTCLAS compilation are in the current directory ):
<? Phprequire_once ('nlp. php'); var_dump (NLP: tokenize ('hello, world! ');?>