An example of the maximum forward matching algorithm implemented in PHP

jacklove
Release: 2023-04-02 15:42:02
Original
1588 people have browsed it

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编程'
);
Copy after login

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,'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;
}
Copy after login

Usage:

$query='欢迎访问脚本之家!脚本之家是国内专业的网站,提供各种脚本下载及JS、Python、php等编程资料';
extractWords($query,$dict);
Copy after login

Run result:

脚本之家,脚本之家,脚本下载
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!