基于C#檢測敏感詞功能
今天策劃給我一個任務(wù) —— 檢測昵稱中是否含有敏感詞功能,然后丟給我兩個壓縮包,我解壓一看:
有的txt文件是一行一個詞:
有的txt文件是按逗號分隔開:
不管是什么格式的總之量非常多,把我這輩子臟話都囊括了
讀取TXT文件數(shù)據(jù)
然后我得先對這些txt文件進(jìn)行處理轉(zhuǎn)換成我們能用的格式:一開始我直接for循環(huán)查找是否含有敏感詞,后邊找資料看到一個DFA算法。
using System; using System.Text; using System.Collections.Generic; using System.IO; public class Program { static void Main() { //換行的txt文件 List<string> list = LineFeed(); //帶有逗號的txt文件 Comma(); string name = "假如這是敏感詞"; //檢測昵稱中是否含有敏感詞 CensorText(name, list); Console.Read(); } static void CensorText(string text, List<string> list) { foreach (string line in list) { if (text.Contains(line)) { Console.WriteLine("昵稱中存在無法使用的字符,請修改后再次確認(rèn)"); } } } //用換行分割的txt文件 static List<string> LineFeed() { string filePath = "E:\\C#Project\\PBZ\\反動詞庫.txt"; // 替換為你的 txt 文件路徑 List<string> lines = ReadTxtFile(filePath); string a = ""; foreach (string line in lines) { a += "\"" + line + "\","; } Console.WriteLine(a); return lines; } static List<string> ReadTxtFile(string filePath) { List<string> lines = new List<string>(); try { using (StreamReader sr = new StreamReader(filePath)) { string line; while ((line = sr.ReadLine()) != null) { lines.Add(line); } } } catch (Exception e) { Console.WriteLine("讀取文件時出現(xiàn)錯誤: " + e.Message); } return lines; } //用逗號分隔的txt文件 static void Comma() { string filePath = "E:\\C#Project\\PBZ\\GFW補充詞庫.txt"; // 替換為你的 txt 文件路徑 List<string> elements = ReadTxtFile1(filePath); string a = ""; foreach (string element in elements) { a += "\"" + element + "\","; } Console.WriteLine(a); } static List<string> ReadTxtFile1(string filePath) { List<string> elements = new List<string>(); try { using (StreamReader sr = new StreamReader(filePath)) { string line = sr.ReadLine(); if (line != null) { string[] splitElements = line.Split(','); foreach (string element in splitElements) { elements.Add(element); } } } } catch (Exception e) { Console.WriteLine("讀取文件時出現(xiàn)錯誤: " + e.Message); } return elements; } }
這樣處理過后的數(shù)據(jù)就是List<string>,或者可以處理成數(shù)組、集合都可以
我把處理出來的數(shù)據(jù)放在HashSet中
/// <summary> /// 敏感詞詞庫 /// </summary> public static HashSet<string> MaskWord = new HashSet<string> { "敏感詞1","敏感詞2","敏感詞3","..." }
C#版DFA算法
然后通過C#版的DFA算法判斷昵稱中是否含有敏感詞返回bool型放在工具類中使用:
/// <summary> /// 檢測敏感詞 /// </summary> /// <param name="text">要檢測的詞</param> /// <param name="MaskWord">敏感詞詞庫</param> /// <returns></returns> public static bool CheckSensitiveWords(string text) { Dictionary<string, Dictionary<string, string>> stateMap = new Dictionary<string, Dictionary<string, string>>(); Dictionary<string, string> currentState = new Dictionary<string, string>(); char[] chars; foreach (string word in MaskWord) { currentState = stateMap.ContainsKey("0") ? stateMap["0"] : new Dictionary<string, string>(); Dictionary<string, string> nextState; chars = word.ToCharArray(); for (int i = 0; i < chars.Length; i++) { string c = chars[i].ToString(); string nextStateKey = i == chars.Length - 1 ? "end" : (i + 1).ToString(); if (currentState.ContainsKey(c)) { nextState = stateMap[currentState[c]]; } else { nextState = new Dictionary<string, string>(); stateMap[currentState.Count.ToString()] = nextState; currentState[c] = currentState.Count.ToString(); } currentState = nextState; currentState["end"] = "end"; } } currentState = stateMap.ContainsKey("0") ? stateMap["0"] : new Dictionary<string, string>(); chars = text.ToCharArray(); for (int i = 0; i < chars.Length; i++) { string c = chars[i].ToString(); if (currentState.ContainsKey(c)) { currentState = stateMap[currentState[c]]; if (currentState.ContainsKey("end")) { return true; // 匹配到敏感詞 } } else { currentState = stateMap.ContainsKey("0") ? stateMap["0"] : new Dictionary<string, string>(); } } return false; // 未匹配到敏感詞 }
到此這篇關(guān)于基于C#檢測敏感詞功能的文章就介紹到這了,更多相關(guān)C#檢測敏感詞內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中的ICustomFormatter及IFormatProvider接口用法揭秘
這篇文章主要介紹了C#中的ICustomFormatter及IFormatProvider接口用法揭秘,本文能過分析一段代碼得出一些研究結(jié)果,需要的朋友可以參考下2015-06-06DevExpress之ChartControl的SeriesTemplate實例
這篇文章主要介紹了DevExpress之ChartControl的SeriesTemplate用法實例,實現(xiàn)了餅狀Series百分比顯示的效果,具有一定的參考借鑒價值,需要的朋友可以參考下2014-10-10C#實現(xiàn)SMTP服務(wù)發(fā)送郵件的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實現(xiàn)SMTP服務(wù)發(fā)送郵件的功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12