/// <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.UTF8); foreach (string word in words) { string key = ToDBC(word); wordList.Add(key); wordList.Add(Strings.StrConv(key, VbStrConv.TraditionalChinese)); } int Cmp(string key1, string key2) { return(String.Compare(key1, key2, StringComparison.Ordinal)); } wordList.Sort(Cmp); for (int i = wordList.Count - 1; i > 0; i--) { if (wordList[i] == wordList[i - 1]) { wordList.RemoveAt(i); } } foreach (var word in wordList) { WordGroup group = MEMORYLEXICON[word[0]]; if (group == null) { group = new WordGroup(); MEMORYLEXICON[word[0]] = group; } group.Add(word.Substring(1)); } } }
/// <summary> /// 查找并替换 ///使用方法 /// string returnstr = "";//返回已经替换的敏感词 ///string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; ///fw.DictionaryPath = str + "sensitive.txt"; ///fw.SourctText = txttest.Value.Trim(); /// returnstr = fw.Filter('*');//*为要替换成的字符 /// </summary> /// <param name="replaceChar">替换成的字符</param> public string Filter(char replaceChar) { LoadDictionary(); if (SourctText != string.Empty) { char[] tempString = SourctText.ToCharArray(); for (int i = 0; i < SourctText.Length; i++) { //查询以该字为首字符的词组 WordGroup group = MEMORYLEXICON[ToDBC(SourctText)[i]]; if (group != null) { for (int z = 0; z < group.Count(); z++) { string word = group.GetWord(z); if (word.Length == 0 || Check(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)); } return(string.Empty); }