Example #1
0
        /// <summary>
        /// 查找并替换
        /// </summary>
        public string Filter(string sourctText)
        {
            char replaceChar = '*';

            LoadDictionary();
            if (sourctText != string.Empty)
            {
                char[] tempString = sourctText.ToCharArray();
                for (int i = 0; i < sourctText.Length; i++)
                {
                    //查询以该字为首字符的词组
                    WordGroup group = MEMORYLEXICON[(int)ToDBC(sourctText)[i]];
                    if (group != null)
                    {
                        for (int z = 0; z < group.Count(); z++)
                        {
                            string word = group.GetWord(z);
                            if (word.Length == 0 || Check(sourctText, word))
                            {
                                string blackword = string.Empty;
                                for (int pos = 0; pos < wordlenght + 1; pos++)
                                {
                                    blackword += tempString[pos + cursor].ToString();
                                    tempString[pos + cursor] = replaceChar;
                                }
                                illegalWords.Add(blackword);
                                cursor = cursor + wordlenght;
                                i      = i + wordlenght;
                            }
                        }
                    }
                    cursor++;
                }
                return(new string(tempString));
            }
            else
            {
                return(string.Empty);
            }
        }
Example #2
0
 /// <summary>
 /// 加载内存词库
 /// </summary>
 private void LoadDictionary()
 {
     if (DictionaryPath != string.Empty)
     {
         List <string> wordList = new List <string>();
         Array.Clear(MEMORYLEXICON, 0, MEMORYLEXICON.Length);
         string[] words = System.IO.File.ReadAllLines(DictionaryPath, System.Text.Encoding.Default);
         foreach (string word in words)
         {
             string key = this.ToDBC(word);
             wordList.Add(key);
             wordList.Add(new ToTraditionalHelp().ToTraditional(key));
         }
         Comparison <string> cmp = delegate(string key1, string key2)
         {
             return(key1.CompareTo(key2));
         };
         wordList.Sort(cmp);
         for (int i = wordList.Count - 1; i > 0; i--)
         {
             if (wordList[i].ToString() == wordList[i - 1].ToString())
             {
                 wordList.RemoveAt(i);
             }
         }
         foreach (var word in wordList)
         {
             if (word.Length > 0)
             {
                 WordGroup group = MEMORYLEXICON[(int)word[0]];
                 if (group == null)
                 {
                     group = new WordGroup();
                     MEMORYLEXICON[(int)word[0]] = group;
                 }
                 group.Add(word.Substring(1));
             }
         }
     }
 }