im聊天敏感字屏蔽

public string checkKey(string text)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder(text.Length);
            Dictionary<object, List<string>> dicList = new Dictionary<object, List<string>>();
            HashSet<string> filterData = new HashSet<string>();
            for (int i = 2; i >= 0; i--)
            {
                filterData.Add("微信" + (i + 9).ToString());
                filterData.Add("wx" + (i + 9).ToString());
                filterData.Add("qq" + (i + 9).ToString());
            }
            filterData.Add("大大");
            filterData.Add("大人物");
            foreach (var item in filterData)
            {
                char value = item[0];
                if (dicList.ContainsKey(value))
                    dicList[value].Add(item);
                else
                    dicList.Add(value, new List<string>() { item });
            }
            //以上部分创建好dicList后可以直接放入到缓存,下次使用直接读取缓存即可

            int count = text.Length;
            for (int i = 0; i < count; i++)
            {
                char word = text[i];
                if (dicList.ContainsKey(word))//如果在字典表中存在这个key
                {
                    int num = 0;//是否找到匹配的关键字 1找到0未找到
                    var data = dicList[word].OrderBy(g => g.Length);

                    string firstItem = data.ToList()[0];

                    string surplusText = text.Substring(i);

                    //检测每个字是否是特殊符号
                    string result = "";//要对比的结果
                    int takeNum = 0;
                    int w = 0;
                    //此处将内部的while循环提出来相同长度关键词只循环一次即可,是为了防止同样开头的关键词长度也相同,内部每一个关键词凑循环一遍
                    while (true)
                    {
                        if (IsContainSpecial(surplusText[w].ToString()))
                        {
                            takeNum += 1;
                        }
                        else
                        {
                            result += surplusText[w];
                        }

                        w += 1;
                        if (firstItem.Length == result.Length)
                        {
                            break;
                        }
                        if (w == surplusText.Length)
                            break;
                    }

                    //把该key的字典集合按 字符数排序(方便下面从少往多截取字符串查找)
                    foreach (var wordbook in data)
                    {
                        if (wordbook.Length > result.Length && w < surplusText.Length)
                        {
                            while (true)
                            {
                                if (IsContainSpecial(surplusText[w].ToString()))
                                {
                                    takeNum += 1;
                                }
                                else
                                {
                                    result += surplusText[w];
                                }

                                if (wordbook.Length == result.Length)
                                {
                                    break;
                                }
                                w += 1;
                                if (w == surplusText.Length)
                                    break;
                            }
                        }

                        
                        //根据关键字长度往后截取相同的字符数进行比较
                        if (result == wordbook)
                        {
                            num = 1;
                            sb.Append(GetString(result));
                            i = i + wordbook.Length - 1 + takeNum;
                            //比较成功 同时改变i的索引
                            break;
                        }

                    }
                    if (num == 0)
                        sb.Append(word);
                }
                else
                    sb.Append(word);
            }
            return sb.ToString();
        }
        /// <summary>
        /// 替换星号
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private static string GetString(string value)
        {
            string starNum = string.Empty;
            for (int i = 0; i < value.Length; i++)
            {
                starNum += "*";
            }
            return starNum;
        }
        public static bool IsContainSpecial(string s)
        {
            s = s.Replace(@"\", "&");
            Regex r = new Regex("[ \\[ \\] \\^ \\-_*×――(^)$%~!/@#$…&%¥—+=<>《》|!!???::•`·、。,;,.;\"‘’“”-]");
            if (r.Match(s).Success)
            {
                return true;
            }
            return false;
        }

 

posted @ 2020-10-31 14:08  木子zzgxl  阅读(340)  评论(0编辑  收藏  举报