This article mainly introduces the use of Discuz keyword server to implement the PHP Chinese Word segmentation method and code examples, the need for friends can refer to the following
Unlike the use of their own server for Word segmentation, discuz! online Chinese word segmentation service is based on the API to return word segmentation results. In the project, we only need a function to facilitate segmentation, keyword extraction. The following is based on the discuz! online Word service API written functions, testing can be normal operation: code as follows:/** * dz Online Chinese participle * @param $title string for Word segmentation title * @param $c Ontent string The content of the participle * @param $encode The data encoding returned by the string API * @return array a keyword array */ N dz_segment ($title = ', $content = ', $encode = ' utf-8 ') { if ($title = = ") { R Eturn false; } $title = Rawurlencode (Strip_tags ($title)); $content = Strip_tags ($content); if (strlen ($content) >2400) {//Online participle service has length limit $content = &NBSP;MB_SUBSTR ($ Content, 0, $encode); } $content = Rawurlencode ($content); $url = ' http://keyword.discuz.com/related_kw.html?title= '. $title. ' &content= '. $content. ' &ics= '. $encode. ' &ocs= '. $encode; $xml _array=simplexml_load_file ($url); //read data from XML to an array object $result = $xml _arra y->keyword->result; $data = array (); foreach ($result->item as $key => $value) { Array_push ($data , (String) $value->kw); } if (count ($data) > 0) { return $data; }else{&nbs P return false; } Participle example, access by URL: Code as follows: Http://keyword.discuz.com/related_kw.html?title= high-grade history of the whole review of the fine course &content=&ics=utf-8&ocs=utf-8 returned XML data: Code as follows: <?xml version= "1.0" encoding= "Utf-8"?> < total_response> <svalid>36000</svalid> <keyword> <info> < count>1</count> <errno>0</errno> <nextuptime>1291287160</nextuptime > <keep>0</keep> </info> <result> <item> <kw><! [cdata[history]]></kw> </item> </result> </keyword> </total_response >