PHP-based maximum forward matching algorithm example and php-based positive algorithm example

Source: Internet
Author: User

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

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.