Class Segmentation {
Var $ options = array ('lowercase' => TRUE,
'Segment _ enic' => 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;