php 检测违禁词

 /***
     * 违禁词检测
     */
    public function prohibited_words()
    {
        $params = $this->request->post();
        $params['prompt'] = trim($params['prompt'], ' ');
        if ($params['prompt'] == null) {
            $this->error('请输入内容');
        }
        $ai_prohibited = Db::name('config')->where(['name' => 'ai_prohibited'])->value('value');
        $ai_prohibited_arr = explode("\r\n", $ai_prohibited);
        $words = $ai_prohibited_arr;
        $content = $params['prompt'];
        $banned = generateRegularExpression($words);
        //检查违禁词
        $res_banned = check_words($banned, $content);
        if (count($res_banned) > 0) {
            $this->error('存在违禁词');
        }
        $this->success('成功');
    }
 /**
     * @describe 数组生成正则表达式
     * @param array $words
     * @return string
     */
    function generateRegularExpression($words)
    {
        $regular = implode('|', array_map('preg_quote', $words));
        return "/$regular/i";
    }

    /**
     * @describe 字符串 生成正则表达式
     * @param array $words
     * @return string
     */
    function generateRegularExpressionString($string)
    {
        $str_arr[0] = $string;
        $str_new_arr = array_map('preg_quote', $str_arr);
        return $str_new_arr[0];
    }

    /**
     * 检查敏感词
     * @param $banned
     * @param $string
     * @return bool|string
     */
    function check_words($banned, $string)
    {
        $match_banned = array();
        //循环查出所有敏感词

        $new_banned = strtolower($banned);
        $i = 0;
        do {
            $matches = null;
            if (!empty($new_banned) && preg_match($new_banned, $string, $matches)) {
                $isempyt = empty($matches[0]);
                if (!$isempyt) {
                    $match_banned = array_merge($match_banned, $matches);
                    $matches_str = strtolower(generateRegularExpressionString($matches[0]));
                    $new_banned = str_replace("|" . $matches_str . "|", "|", $new_banned);
                    $new_banned = str_replace("/" . $matches_str . "|", "/", $new_banned);
                    $new_banned = str_replace("|" . $matches_str . "/", "/", $new_banned);
                }
            }
            $i++;
            if ($i > 20) {
                $isempyt = true;
                break;
            }
        } while (count($matches) > 0 && !$isempyt);

        //查出敏感词
        if ($match_banned) {
            return $match_banned;
        }
        //没有查出敏感词
        return array();
    }

    /**
     * 打印到页面上
     * @param $filepath
     * @param $res_mingan
     * @param $res_banned
     */
    function write_html($content, $res_banned)
    {

        print_r($content);
        if ($res_banned) {
            print_r("  <font color='red'>违禁词(" . count($res_banned) . "):</font>" . implode('|', $res_banned));
        }
        echo "<br>";

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值