This article mainly introduces PHP implementation of the largest forward matching algorithm, simple description of the maximum forward matching algorithm concept, the principle and combined with the example of the implementation of PHP and the use of the maximum forward matching algorithm related operation skills, the need for friends can refer to the next
This paper describes the maximum forward matching algorithm for PHP implementation. Share to everyone for your reference, as follows:
forward maximum matching algorithm: from left to right, several consecutive characters in the text of the word will be matched to the thesaurus, and if so, a word is cut. But here's the problem: to do the best match, it's not the first match to be able to slice it.
The function contains three parameters:
$query Query terms
$dict Dictionaries
$max _len Maximum Length (default value is set to 15)
Dictionary Example:
$dict = Array (' home of script ' = ' script home ', ' script download ' = ' script download ', ' js programming ' = ' JS programming ');
function definition:
/* * $query query word * $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 the home of the script! Script Home is a domestic professional website, providing a variety of script download and JS, Python, PHP and other programming materials '; Extractwords ($query, $dict);
Operation Result:
Script House, script House, script download
Articles you may be interested in:
PHP Performance analysis tool xhprof installation use and related considerations
PHP Package DB Class Connection Sqlite3 Database Method Example Explanation
PHP implementation of analog HTTP request method analysis explained