An example of the maximum forward matching algorithm implemented by PHP

Source: Internet
Author: User
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

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.