日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#敏感词过滤算法实现

發(fā)布時間:2023/12/20 C# 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#敏感词过滤算法实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

敏感詞、文字過濾是一個網(wǎng)站必不可少的功能,如何設(shè)計(jì)一個好的、高效的過濾算法是非常有必要的。

在實(shí)現(xiàn)文字過濾的算法中,DFA是唯一比較好的實(shí)現(xiàn)算法。DFA即Deterministic Finite Automaton,也就是確定有窮自動機(jī),它是是通過event和當(dāng)前的state得到下一個state,即event+state=nextstate。在實(shí)現(xiàn)敏感詞過濾的算法中,我們必須要減少運(yùn)算,而DFA在DFA算法中幾乎沒有什么計(jì)算,有的只是狀態(tài)的轉(zhuǎn)換。

下面看下在c#方法下實(shí)現(xiàn)方式

1、構(gòu)建敏感詞庫類

private?bool?LoadDictionary(){var?wordList?=?new?List<string>();if?(_memoryLexicon?==?null){_memoryLexicon?=?new?WordGroup[char.MaxValue];var?words?=?new?SensitiveWordBll().GetAllWords();if?(words?==?null)return?false;foreach?(string?word?in?words){wordList.Add(word);var?chineseWord?=?Microsoft.VisualBasic.Strings.StrConv(word,Microsoft.VisualBasic.VbStrConv.TraditionalChinese,?0);if?(word?!=?chineseWord)wordList.Add(chineseWord);}foreach?(var?word?in?wordList){if?(word.Length?>?0){var?group?=?_memoryLexicon[word[0]];if?(group?==?null){group?=?new?WordGroup();_memoryLexicon[word[0]]?=?group;}group.Add(word.Substring(1));}}}return?true;}

2、構(gòu)建敏感詞檢測類

private?bool?Check(string?blackWord){_wordlenght?=?0;//檢測源下一位游標(biāo)_nextCursor?=?_cursor?+?1;var?found?=?false;var?continueCheck?=?0;//遍歷詞的每一位做匹配for?(var?i?=?0;?i?<?blackWord.Length;?i++){//特殊字符偏移游標(biāo)var?offset?=?0;if?(_nextCursor?>=?_sourceText.Length){if?(i?-?1?<?blackWord.Length?-?1)found?=?false;break;}else{//檢測下位字符如果不是漢字?數(shù)字?字符?偏移量加1for?(var?y?=?_nextCursor;?y?<?_sourceText.Length;?y++){if?(!IsChs(_sourceText[y])?&&?!IsNum(_sourceText[y])?&&?!IsAlphabet(_sourceText[y])){offset++;//避讓特殊字符,下位游標(biāo)如果>=字符串長度?跳出if?(_nextCursor?+?offset?>=?_sourceText.Length)break;_wordlenght++;}else?break;}if?(_nextCursor?+?offset?>=?_sourceText.Length){found?=?false;break;}if?(blackWord[i]?==?_sourceText[_nextCursor?+?offset]){found?=?true;continueCheck?=?0;}else{//?匹配不到時嘗試?yán)^續(xù)匹配4個字符if?(continueCheck?<?4?&&?_nextCursor?<?_sourceText.Length?-?1){continueCheck++;i--;}else{found?=?false;break;}}}_nextCursor?=?_nextCursor?+?1?+?offset;_wordlenght++;}return?found;}}

3、測試與使用方法

_illegalWords?=?new?List<string>();if?(string.IsNullOrEmpty(sourceText)?&&?string.IsNullOrEmpty(_sourceText)){return?sourceText;}if?(!string.IsNullOrEmpty(sourceText))_sourceText?=?sourceText;_cursor?=?0;if?(!LoadDictionary()){return?_sourceText;}var?tempString?=?_sourceText.ToCharArray();var?sourceTextDbc?=?ToDBC(SourceText);for?(var?i?=?0;?i?<?SourceText.Length;?i++){//查詢以該字為首字符的詞組var?group?=?_memoryLexicon[sourceTextDbc[i]];if?(group?!=?null){for?(var?z?=?0;?z?<?group.Count();?z++){string?word?=?group.GetWord(z);if?(word.Length?==?0?||?Check(word)){if?(isFirstCheckedReturn){return?null;}var?blackword?=?string.Empty;for?(var?pos?=?0;?pos?<?_wordlenght?+?1;?pos++){blackword?+=?tempString[pos?+?_cursor].ToString();tempString[pos?+?_cursor]?=?ReplaceChar;}_illegalWords.Add(blackword);_cursor?=?_cursor?+?_wordlenght;i?=?i?+?_wordlenght;break;}}}_cursor++;}return?new?string(tempString);var?filter?=?new?SensitiveWordFilter();filter.SourceText?=?"dddddd";var?sourctText?=?filter.SourceText;filter.ResetMemoryLexicon();var?datetime?=?DateTime.Now;var?ss?=?filter.Filter();var?datetime2?=?DateTime.Now;var?millisecond?=?(datetime2?-?datetime).TotalMilliseconds;Console.WriteLine(millisecond);Console.WriteLine(ss);var?words?=?System.IO.File.ReadAllLines(@"D:\Recv\敏感詞庫大全.txt",?System.Text.Encoding.UTF8);var?ssx?=?sourctText;var?datetimex?=?DateTime.Now;foreach?(var?word?in?words){if?(word.Length?>?0)ssx?=?ssx.Replace(word,?"*".PadLeft(word.Length,?'*'));}var?datetime2x?=?DateTime.Now;var?millisecondx?=?(datetime2x?-?datetimex).TotalMilliseconds;Console.WriteLine(millisecondx);Console.WriteLine(ssx);

技術(shù)群:?需要進(jìn)技術(shù)群學(xué)習(xí)交流的請?zhí)砑有【幬⑿?#xff0c;切記備注:加群,對以上內(nèi)容有什么疑問也可以直接和小編直接溝通交流!? ???

小編微信:mm1552923 ??

公眾號:dotNet編程大全? ? ??

總結(jié)

以上是生活随笔為你收集整理的C#敏感词过滤算法实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。