敏感词检查

作者:追风剑情 发布于:2019-11-27 15:46 分类:Unity3d

下载敏感词库
百度网盘 提取码 06o6

示例

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;

public static class Badwords
{
    private static string[] words;

    //content: 一行一个单词的敏感词库文件内容
    public static void SetContent(string content)
    {
        words = content.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
        Debug.LogFormat("badword count: {0}", words.Length);
    }

    //判断input中是否包含敏感词
    public static bool IsContainBadword(string input, out string badword)
    {
        badword = string.Empty;
        string pattern;
        bool match = false;
        for (int i=0; i<words.Length; i++)
        {
            pattern = string.Format(".*{0}.*", words[i]);
            try
            {
                match = Regex.IsMatch(input, pattern);
            }
            catch(ArgumentException ex)
            {
                Debug.LogErrorFormat("参数异常 input={0}, pattern={1}", input, pattern);
            }
            
            if (match)
            {
                badword = pattern;
                return true;
            }
        }
        return false;
    }
}


标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号