Use the Discuz keyword server to implement PHP Chinese word segmentation. Discuz! The online Chinese word segmentation service returns word segmentation results based on APIs. In the project, we only need one function to perform word segmentation conveniently. Unlike using our own server for word segmentation, Discuz! The online Chinese word segmentation service returns word segmentation results based on APIs. In the project, we only need a function to facilitate word segmentation and Keyword extraction.
The following is based on Discuz! Functions written by the online word segmentation service API can run normally:
The code is as follows:
/**
* DZ online Chinese word segmentation
* @ Param $ title string the title for Word Segmentation
* @ Param $ content string content for Word Segmentation
* @ Param $ encoding of the data returned by the encode string API
* @ Return array the keyword array obtained
*/
Function dz_segment ($ title = '', $ content ='', $ encode = 'utf-8 '){
If ($ title = ''){
Return false;
}
$ Title = rawurlencode (strip_tags ($ title ));
$ Content = strip_tags ($ content );
If (strlen ($ content)> 2400) {// The length of the online word segmentation service is limited.
$ Content = mb_substr ($ content, 0,800, $ encode );
}
$ Content = rawurlencode ($ content );
$ Url = 'http: // keyword.discuz.com/related_k1_html? Title = '. $ title.' & content = '. $ content.' & ics = '. $ encode.' & ocs = '. $ encode;
$ Xml_array = simplexml_load_file ($ url); // read the data in XML to the array object.
$ Result = $ xml_array-> keyword-> result;
$ Data = array ();
Foreach ($ result-> item as $ key => $ value ){
Array_push ($ data, (string) $ value-> kw );
}
If (count ($ data)> 0 ){
Return $ data;
} Else {
Return false;
}
}
Word Segmentation example, access through url:
The code is as follows:
Http://keyword.discuz.com/related_kw.html? Title = High School history full course review excellent tutorial & content = & ics = UTF-8 & ocs = UTF-8
Returned XML data:
The code is as follows:
36000
1
0
1291287160
0
高三历史
Success! The online Chinese word segmentation service returns word segmentation results based on APIs. In the project, we only need a function to facilitate word segmentation...