本站所有内容仅限用于学习和研究目的,如有侵权请邮件与我们联系处理!
世上事本无难易,为之则易!

网站首页 站长福利 福利文章 正文

用php实现一个敏感词过滤功能

曹操 2023-03-28 福利文章 454 ℃ 2 评论 9575字 free 收藏

网站内容有过多的敏感词,会导致K站。一方面是你懂的,另一方面是我们自己可能也要过滤一些人身攻击或者广告信息等,具体词库可以google下,有很多。

过滤敏感词,使用简单的循环str_replace是性能很低效的,还会随着词库的增加,性能指数下降,而且简单的替换,不能解决一些不是完全匹配的词。这时候就需要先构建一个字典树(trie),单纯的字典树占用空间较大,使用Double-Array Trie或者Ternary Search Tree可以在保证性能的同时节省一部分空间,但是敏感词基本不会很多,几千甚至上万个词基本没压力,所以就实现就选择先构建一个字典树,然后逐字做匹配。

代码不多,就贴到这里。

函数部分

<?php
class SensitiveWordFilter
{
    private $dict;
    private $dictPath;
    public function __construct($dictPath)
    {
        $this->dict = array();
        $this->dictPath = $dictPath;
        $this->initDict();
    }
    private function initDict()
    {
        $handle = fopen($this->dictPath, 'r');
        if (!$handle) {
            throw new RuntimeException('open dictionary file error.');
        }
        while (!feof($handle)) {
            $word = trim(fgets($handle, 128));
            if (empty($word)) {
                continue;
            }
            $uWord = $this->unicodeSplit($word);
            $pdict = &$this->dict;
            $count = count($uWord);
            for ($i = 0; $i < $count; $i++) {
                if (!isset($pdict[$uWord[$i]])) {
                    $pdict[$uWord[$i]] = array();
                }
                $pdict = &$pdict[$uWord[$i]];
            }
            $pdict['end'] = true;
        }
        fclose($handle);
    }
    public function filter($str, $maxDistance = 5)
    {
        if ($maxDistance < 1) {
            $maxDistance = 1;
        }
        $uStr = $this->unicodeSplit($str);
        $count = count($uStr);
        for ($i = 0; $i < $count; $i++) {
            if (isset($this->dict[$uStr[$i]])) {
                $pdict = &$this->dict[$uStr[$i]];
                $matchIndexes = array();
                for ($j = $i + 1, $d = 0; $d < $maxDistance && $j < $count; $j++, $d++) {
                    if (isset($pdict[$uStr[$j]])) {
                        $matchIndexes[] = $j;
                        $pdict = &$pdict[$uStr[$j]];
                        $d = -1;
                    }
                }
                if (isset($pdict['end'])) {
                    $uStr[$i] = '*';
                    foreach ($matchIndexes as $k) {
                        if ($k - $i == 1) {
                            $i = $k;
                        }
                        $uStr[$k] = '*';
                    }
                }
            }
        }
        return implode($uStr);
    }
    public function unicodeSplit($str)
    {
        $str = strtolower($str);
        $ret = array();
        $len = strlen($str);
        for ($i = 0; $i < $len; $i++) {
            $c = ord($str[$i]);
            if ($c & 0x80) {
if (($c & 0xf8) == 0xf0 && $len - $i >= 4) {
if ((ord($str[$i + 1]) & 0xc0) == 0x80 && (ord($str[$i + 2]) & 0xc0) == 0x80 && (ord($str[$i + 3]) & 0xc0) == 0x80) {
$uc = substr($str, $i, 4);
$ret[] = $uc;
$i += 3;
}
} else if (($c & 0xf0) == 0xe0 && $len - $i >= 3) {
if ((ord($str[$i + 1]) & 0xc0) == 0x80 && (ord($str[$i + 2]) & 0xc0) == 0x80) {
$uc = substr($str, $i, 3);
$ret[] = $uc;
$i += 2;
}
} else if (($c & 0xe0) == 0xc0 && $len - $i >= 2) {
if ((ord($str[$i + 1])  & 0xc0) == 0x80) {
$uc = substr($str, $i, 2);
$ret[] = $uc;
$i += 1;
}
}
} else {
$ret[] = $str[$i];
}
}
return $ret;
}
}

使用方法

<?php
require 'SensitiveWordFilter.php';
/*
初始化传入词库文件路径,词库文件每个词一个换行符。
如:
敏感1
敏感2
目前只支持UTF-8编码
*/
$filter = new SensitiveWordFilter(__DIR__ . '/data/minganwords.txt');
/*
第一个参数传入要过滤的字符串,第二个是匹配的字间距,
比如'枪支'是一个敏感词,想过滤'枪||||支'的时候,
就需要指定一个两个字的间距,可以根据情况设定,
超过指定间距就不会过滤。所有匹配的敏感词会被替换为'*'。
*/
$groupname = "这是一个敏感词";
$check = $filter->filter($groupname,2);
echo($check);
目录导航
  • 函数部分
  • 使用方法

  • Tags:发帖软件

    必看说明

    • 本站中所有被研究的素材与信息全部来源于互联网,版权争议与本站无关。
    • 本站文章或仅为文本内容原创,非程序原创。如有侵权、不妥之处,请联系站长第一时间删除。敬请谅解!
    • 本站所有内容严格遵守国家法律的条例,所有研究的算法技术均来源于搜索引擎公开默认允许用户研究使用的接口。
    • 阅读本文及获取资源前,请确保您已充分阅读并理解《访问曹操SEO网站需知:行为准则》。
    • 本站分享的任何工具、程序仅供学习参考编写架构,仅可在本地的虚拟机内断网测试,严禁联网运行或上传搭建!
    • 任何资源必须在下载后24个小时内,从电脑中彻底删除。不得传播或者用于其他任何用途!否则一切后果用户自负!
    • 转载请注明 : 文章转载自  曹操SEO 用php实现一个敏感词过滤功能
    • 本文标题:《用php实现一个敏感词过滤功能》
    • 本文链接:https://www.ccooc.cn/1903.html

    已有2位网友发表了看法:

    欢迎 发表评论:

    网站分类
    近期评论
    文章归档
    标签列表
    站点信息
    • 文章总数:2021
    • 页面总数:7
    • 分类总数:46
    • 标签总数:340
    • 评论总数:8895
    • 浏览总数:5175133