コード例 #1
0
        /// <summary>
        /// 位置查找
        /// </summary>
        internal Dictionary <int, int> Find(string src)
        {
            if (this._root == null)
            {
                throw new InvalidOperationException("未初始化");
            }
            Dictionary <int, int> dictionary = new Dictionary <int, int>();

            if (string.IsNullOrEmpty(src))
            {
                return(dictionary);
            }
            char[] array = this.Translation(src);
            for (int i = 0; i < array.Length; i++)
            {
                int  num = 0;
                char c   = array[i + num];
                FilterKeyWords.FilterKeyWordsNode filterKeyWordsNode = this._root;
                while (this.IsSkip(c))
                {
                    if (i + num + 1 >= array.Length)
                    {
                        break;
                    }
                    i++;
                    c = array[i + num];
                }
                while (!filterKeyWordsNode.Child.ContainsKey(c))
                {
                    if (i >= array.Length - 1)
                    {
                        break;
                    }
                    i++;
                    c = array[i + num];
                }
                while (filterKeyWordsNode.Child != null && filterKeyWordsNode.Child.ContainsKey(c))
                {
                    filterKeyWordsNode = filterKeyWordsNode.Child[c];
                    num++;
                    if (i + num == array.Length)
                    {
                        break;
                    }
                    c = array[i + num];
                    while (this.IsSkip(c) && !filterKeyWordsNode.IsEnd && i + num + 1 < array.Length)
                    {
                        num++;
                        c = array[i + num];
                    }
                }
                if (filterKeyWordsNode.IsEnd)
                {
                    dictionary.Add(i, num);
                    i += num - 1;
                }
            }
            return(dictionary);
        }
コード例 #2
0
 /// <summary>
 /// 初始化 使用前必须调用一次
 /// </summary>
 /// <param name="keyWords">敏感词列表</param>
 internal FilterKeyWords(string[] keyWords)
 {
     if (this._root != null)
     {
         return;
     }
     lock (this.LockObj)
     {
         this._root = new FilterKeyWords.FilterKeyWordsNode();
         List <string> list = (from p in (from p in keyWords
                                          select new string(this.Translation(p))).Distinct <string>()
                               orderby p, p.Length
                               select p).ToList <string>();
         int num = list.Min((string p) => p.Length);
         while (true)
         {
             if (num > list.Max((string p) => p.Length))
             {
                 break;
             }
             int i1 = num;
             IEnumerable <string> enumerable = (from p in list
                                                where p.Length >= i1
                                                select p.Substring(0, i1)).Distinct <string>();
             foreach (string current in enumerable)
             {
                 FilterKeyWords.FilterKeyWordsNode filterKeyWordsNode = this._root;
                 for (int j = 0; j < current.Length; j++)
                 {
                     char key = current[j];
                     if (filterKeyWordsNode.Child == null)
                     {
                         filterKeyWordsNode.Child = new Dictionary <char, FilterKeyWords.FilterKeyWordsNode>();
                     }
                     if (!filterKeyWordsNode.Child.ContainsKey(key))
                     {
                         filterKeyWordsNode.Child.Add(key, new FilterKeyWords.FilterKeyWordsNode
                         {
                             IsEnd = list.Contains(current.Substring(0, 1 + j))
                         });
                     }
                     filterKeyWordsNode = filterKeyWordsNode.Child[key];
                 }
             }
             num++;
         }
     }
 }