PHP中文关键词匹配

   关键词匹配是比较常见的需求,如留言、弹幕及游戏聊天中的敏感词过滤,都需要对一段文字进行关键词匹配。提取到关键词后,再做进一步处理。

本类借助PHP高效的数组和mbstring扩展,来实现对中文关键词的匹配。主要思想是以关键词为key,构建字典数组,这样便可以对每个关键词可实现常数级别的查找。

具体代码如下:

 

 1 class WordMatcher {
 2     public $dict = [];
 3     public $wordMaxLen = 0;
 4 
 5     function __construct(){
 6         if(! extension_loaded('mbstring')) {
 7             exit('extension mbstring is not loaded');
 8         }
 9     }
10 
11     function addWord($word) {
12         $len = mb_strlen($word, 'utf-8');
13         $this->wordMaxLen = $len > $this->wordMaxLen ? $len : $this->wordMaxLen;
14         $this->dict[$word] = 1;
15     }
16 
17     function removeWord($word) {
18         unset($this->dict[$word]);
19     }
20 
21     function match($str, &$matched, $matchAll=false) {
22         if(mb_strlen($str) < 1) {
23             return;
24         }
25 
26         $matchLen = 0;
27         $len = $this->wordMaxLen;
28         while($len>0) {
29             $substr = mb_substr($str, 0, $len, 'utf-8');
30             if(isset($this->dict[$substr])) {
31                 $matchLen = $len;
32                 $matched[] = $substr;
33                 break;
34             } else {
35                 $len--;
36             }
37         }
38 
39         if(!$matchAll && $matchLen) {
40             $str = mb_substr($str, $matchLen, null, 'utf-8');            
41         } else {
42             $str = mb_substr($str, 1, null, 'utf-8');
43         }
44   
45         $this->match($str, $matched, $matchAll);
46     }
47 }
48 
49 $matcher = new WordMatcher;
50 $matcher->addWord('PHP');
51 $matcher->addWord('语言');
52 $matcher->addWord('H');
53 
54 
55 $matcher->match('PHP是最好的语言', $matched);

 

转载于:https://www.cnblogs.com/cnsr/p/8297123.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值