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);