This article describes the maximum forward matching algorithm implemented by PHP. Share it for your reference, as follows:
Forward maximum matching algorithm:Match several consecutive characters in the text to be participled from left to right to the vocabulary list, and if it matches, split a word. But there is a problem here: to achieve the maximum match, it is not possible to split it by the first match.
The function contains three parameters:
$queryQuery word
$dict Dictionary
$max_lenMaximum length (the default value is set to 15 here)
Dictionary example:
$dict = array( 'I'=>'I', 'Script Download'=>'Script Download', 'JS Programming'=>'JS Programming' );
Function definition:
/* * $query query term * $dict Dictionary * $max_len Maximum length */ function extractWords($query,$dict,$max_len=15){ $feature = ""; $slen=mb_strlen($query,'UTF8'); $c_bg = 0; while($c_bg<$slen){ $matched = false; $c_len =(($slen-$c_bg)>$max_len)?$max_len:($slen-$c_bg); $t_str = mb_substr($query, $c_bg,$c_len,'UTF8'); for($i=$c_len;$i>1;$i--){ $ttts = mb_substr($t_str, 0,$i,'UTF8'); if(!empty($dict[$ttts])){ // echo 'matched = '.$ttts.PHP_EOL; $matched = true; $c_bg += $i; if(!empty($feature)){ $feature.=","; } $feature.=$ttts; break; } } if(!$matched){ $c_bg++; } } echo $feature.PHP_EOL; }
How to use:
$query='Welcome to me! I am a professional website in China, providing various script downloads and programming materials such as JS, Python, php, etc.'; extractWords($query,$dict);
Running results:
I,I,Script download
For more information about PHP related content, please check out the topic of this site:PHP data structure and algorithm tutorial》、《Summary of PHP Programming Algorithm》、《Summary of usage of php strings》、《Complete collection of PHP array (Array) operation techniques》、《Summary of common traversal algorithms and techniques for PHP"and"Summary of PHP mathematical operation skills》
I hope this article will be helpful to everyone's PHP programming.