PHP-based maximum forward matching algorithm example and php-based positive algorithm example
This example describes the maximum forward matching algorithm implemented by PHP. We will share this with you for your reference. The details are as follows:
Forward maximum matching algorithm:Match several consecutive characters in the text to be segmented with the word table from left to right. If the Word Table matches, the word is segmented into one word. But there is a problem: to achieve the maximum match, it is not the first match that can be split.
The function contains three parameters:
$ Query term
$ Dict dictionary
$ Max_len maximum length (15 by default)
Dictionary example:
$ Dict = array ('customer home' => 'customer home', 'script download' => 'script download', 'js programming '=> 'js programming ');
Function Definition:
/** $ 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 ;}
Usage:
$ Query = 'Welcome to the house of friends! The Helper house is a professional website in China that provides various script downloads and programming materials such as JS, Python, and php '; extractWords ($ query, $ dict );
Running result:
Helper house, helper house, script download