Home  >  Article  >  Backend Development  >  An example of the maximum forward matching algorithm implemented in PHP

An example of the maximum forward matching algorithm implemented in PHP

jacklove
jackloveOriginal
2018-07-05 17:47:151731browse

This article mainly introduces the maximum forward matching algorithm implemented in PHP, briefly describes the concept and principle of the maximum forward matching algorithm, and analyzes the related operating techniques of implementing and using the maximum forward matching algorithm in PHP in the form of examples. Friends in need can refer to the following

The examples in this article describe the maximum forward matching algorithm implemented in PHP. Share it with everyone 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 list from left to right. If there is a match, , then a word is segmented. But there is a problem here: to achieve maximum matching, it is not possible to split the first match.

The function contains three parameters:

$query Query word
$dict Dictionary
$max_len Maximum length (the default value here is set to 15)

Dictionary example:

$dict = array(
  '脚本之家'=>'脚本之家',
  '脚本下载'=>'脚本下载',
  'JS编程'=>'JS编程'
);

Function definition:

/*
 * $query 查询词
 * $dict 词典
 * $max_len 最大长度
 */
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,&#39;UTF8&#39;);
      for($i=$c_len;$i>1;$i--){
        $ttts = mb_substr($t_str, 0,$i,&#39;UTF8&#39;);
          if(!empty($dict[$ttts])){
//          echo &#39;matched = &#39;.$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=&#39;欢迎访问脚本之家!脚本之家是国内专业的网站,提供各种脚本下载及JS、Python、php等编程资料&#39;;
extractWords($query,$dict);

Run result:

脚本之家,脚本之家,脚本下载

Articles you may be interested in:

Installation and use of the PHP performance analysis tool xhprof and related precautions

Explanation of the method of php encapsulating the db class to connect to the sqlite3 database

Analysis and explanation of the method of simulating http requests in PHP

The above is the detailed content of An example of the maximum forward matching algorithm implemented in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn