Exemple #1
0
 private void button1_Click(object sender, EventArgs e)
 {
     string[] words = new[] { "敏感词1", "敏感词2", "含有", "垃圾", "3" }; //敏感词组 可自行在网上 搜索下载
     //敏感词库 类可被继承,如果想实现自定义 敏感词导入方法 可以 对 LoadWords 方法进行 重写
     var          library = new WordsLibrary(words);             //实例化 敏感词库
     string       text    = "在任意一个文本中都可能包含敏感词1、2、3等等,只要含有敏感词都会被找出来,比如:垃圾";
     ContentCheck check   = new ContentCheck(library, text);     //实例化 内容检测类
     var          list    = check.FindSensitiveWords();          //调用 查找敏感词方法 返回敏感词列表
     var          str     = check.SensitiveWordsReplace();       //调用 敏感词替换方法 返回处理过的字符串
 }
Exemple #2
0
        /// <summary>
        /// 替换敏感词
        /// </summary>
        /// <param name="library">敏感词库</param>
        /// <param name="text">检测文本</param>
        /// <param name="newChar">替换字符</param>
        /// <returns></returns>
        public static string SensitiveWordsReplace(WordsLibrary library, string text, char newChar = '*')
        {
            Dictionary <int, char> dic = new ContentCheck(library).WordsCheck(text);

            if (dic != null && dic.Keys.Count > 0)
            {
                char[] chars = text.ToCharArray();
                foreach (var item in dic)
                {
                    chars[item.Key] = newChar;
                }

                text = new string(chars);
            }

            return(text);
        }
Exemple #3
0
        /// <summary>
        /// 查找敏感词
        /// </summary>
        /// <param name="library">敏感词库</param>
        /// <param name="text">检测文本</param>
        /// <returns></returns>
        public static List <string> FindSensitiveWords(WordsLibrary library, string text)
        {
            ContentCheck check = new ContentCheck(library, text);

            return(check.FindSensitiveWords());
        }