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("  违禁词(" . count($res_banned) . "):" . implode('|', $res_banned));
        }
        echo "
"
; }

你可能感兴趣的:(php,php)