Use Discuz keyword server to implement PHP Chinese word segmentation _ PHP Tutorial

Source: Internet
Author: User
Tags encode string
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...

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.