Php Chinese word segmentation function code example

Source: Internet
Author: User
Php Chinese word segmentation function code example
I would like to introduce you to a function for Chinese word segmentation in php, which may be used in some cases. if you have any need, please refer to it.

The code is as follows:

     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;?>

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.