最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于C#檢測敏感詞功能

 更新時(shí)間:2024年11月08日 15:18:11   作者:Lazy龍  
這篇文章主要為大家詳細(xì)介紹了如何基于C#實(shí)現(xiàn)檢測敏感詞功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

今天策劃給我一個(gè)任務(wù)  ——  檢測昵稱中是否含有敏感詞功能,然后丟給我兩個(gè)壓縮包,我解壓一看:

有的txt文件是一行一個(gè)詞:

有的txt文件是按逗號分隔開:

不管是什么格式的總之量非常多,把我這輩子臟話都囊括了

讀取TXT文件數(shù)據(jù)

然后我得先對這些txt文件進(jìn)行處理轉(zhuǎn)換成我們能用的格式:一開始我直接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();
        //帶有逗號的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("讀取文件時(shí)出現(xiàn)錯(cuò)誤: " + e.Message);
        }
 
        return lines;
    }
 
    //用逗號分隔的txt文件
    static void Comma() 
    {
        string filePath = "E:\\C#Project\\PBZ\\GFW補(bǔ)充詞庫.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;
    }
}

這樣處理過后的數(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型放在工具類中使用:

java實(shí)現(xiàn)敏感詞過濾(DFA算法)

敏感詞管理(DFA算法實(shí)現(xiàn))

/// <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)文章

最新評論

镇宁| 平泉县| 汕头市| 嫩江县| 绵阳市| 台南县| 临清市| 南召县| 获嘉县| 湖北省| 胶南市| 稻城县| 芦山县| 冕宁县| 江陵县| 周宁县| 襄樊市| 永泰县| 曲阜市| 德保县| 德州市| 桦南县| 安陆市| 建宁县| 宜良县| 鹤壁市| 余干县| 扬州市| 墨江| 千阳县| 通许县| 太和县| 大城县| 潞西市| 望江县| 曲沃县| 德钦县| 宜兴市| 西吉县| 许昌县| 石河子市|