基于C#檢測(cè)敏感詞功能
今天策劃給我一個(gè)任務(wù) —— 檢測(cè)昵稱中是否含有敏感詞功能,然后丟給我兩個(gè)壓縮包,我解壓一看:
有的txt文件是一行一個(gè)詞:
有的txt文件是按逗號(hào)分隔開(kāi):
不管是什么格式的總之量非常多,把我這輩子臟話都囊括了
讀取TXT文件數(shù)據(jù)
然后我得先對(duì)這些txt文件進(jìn)行處理轉(zhuǎn)換成我們能用的格式:一開(kāi)始我直接for循環(huán)查找是否含有敏感詞,后邊找資料看到一個(gè)DFA算法。
using System; using System.Text; using System.Collections.Generic; using System.IO; public class Program { static void Main() { //換行的txt文件 List<string> list = LineFeed(); //帶有逗號(hào)的txt文件 Comma(); string name = "假如這是敏感詞"; //檢測(cè)昵稱中是否含有敏感詞 CensorText(name, list); Console.Read(); } static void CensorText(string text, List<string> list) { foreach (string line in list) { if (text.Contains(line)) { Console.WriteLine("昵稱中存在無(wú)法使用的字符,請(qǐng)修改后再次確認(rèn)"); } } } //用換行分割的txt文件 static List<string> LineFeed() { string filePath = "E:\\C#Project\\PBZ\\反動(dòng)詞庫(kù).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("讀取文件時(shí)出現(xiàn)錯(cuò)誤: " + e.Message); } return lines; } //用逗號(hào)分隔的txt文件 static void Comma() { string filePath = "E:\\C#Project\\PBZ\\GFW補(bǔ)充詞庫(kù).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("讀取文件時(shí)出現(xiàn)錯(cuò)誤: " + e.Message); } return elements; } }
這樣處理過(guò)后的數(shù)據(jù)就是List<string>,或者可以處理成數(shù)組、集合都可以
我把處理出來(lái)的數(shù)據(jù)放在HashSet中
/// <summary> /// 敏感詞詞庫(kù) /// </summary> public static HashSet<string> MaskWord = new HashSet<string> { "敏感詞1","敏感詞2","敏感詞3","..." }
C#版DFA算法
然后通過(guò)C#版的DFA算法判斷昵稱中是否含有敏感詞返回bool型放在工具類中使用:
java實(shí)現(xiàn)敏感詞過(guò)濾(DFA算法)
/// <summary> /// 檢測(cè)敏感詞 /// </summary> /// <param name="text">要檢測(cè)的詞</param> /// <param name="MaskWord">敏感詞詞庫(kù)</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#檢測(cè)敏感詞功能的文章就介紹到這了,更多相關(guān)C#檢測(cè)敏感詞內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中的ICustomFormatter及IFormatProvider接口用法揭秘
這篇文章主要介紹了C#中的ICustomFormatter及IFormatProvider接口用法揭秘,本文能過(guò)分析一段代碼得出一些研究結(jié)果,需要的朋友可以參考下2015-06-06DevExpress之ChartControl的SeriesTemplate實(shí)例
這篇文章主要介紹了DevExpress之ChartControl的SeriesTemplate用法實(shí)例,實(shí)現(xiàn)了餅狀Series百分比顯示的效果,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-10-10C#開(kāi)發(fā)webService接口的流程步驟
在C#中,Web Service 接口是一種用于遠(yuǎn)程程序間的通信機(jī)制,它允許客戶端通過(guò)HTTP協(xié)議訪問(wèn)服務(wù)器端提供的功能和服務(wù),本文給大家詳細(xì)介紹了C#開(kāi)發(fā)webService接口的流程步驟,需要的朋友可以參考下2024-11-11C#實(shí)現(xiàn)chart控件動(dòng)態(tài)曲線繪制
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)chart控件動(dòng)態(tài)曲線繪制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02C#?wpf?無(wú)邊框窗口添加陰影效果的實(shí)現(xiàn)
在本篇內(nèi)容中小編給大家整理了一篇關(guān)于C#?wpf?無(wú)邊框窗口添加陰影效果的具體方法內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下2022-11-11C#實(shí)現(xiàn)SMTP服務(wù)發(fā)送郵件的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)SMTP服務(wù)發(fā)送郵件的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12C# Winform實(shí)現(xiàn)石頭剪刀布游戲
這篇文章主要為大家詳細(xì)介紹了Winform實(shí)現(xiàn)石頭剪刀布游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01