亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于C#檢測敏感詞功能

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

今天策劃給我一個任務(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型放在工具類中使用:

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

敏感詞管理(DFA算法實現(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)文章

  • c# 實現(xiàn)控件(ocx)中的事件詳解

    c# 實現(xiàn)控件(ocx)中的事件詳解

    這篇文章主要介紹了c# 實現(xiàn)控件(ocx)中的事件詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • C#中的ICustomFormatter及IFormatProvider接口用法揭秘

    C#中的ICustomFormatter及IFormatProvider接口用法揭秘

    這篇文章主要介紹了C#中的ICustomFormatter及IFormatProvider接口用法揭秘,本文能過分析一段代碼得出一些研究結(jié)果,需要的朋友可以參考下
    2015-06-06
  • DevExpress之ChartControl的SeriesTemplate實例

    DevExpress之ChartControl的SeriesTemplate實例

    這篇文章主要介紹了DevExpress之ChartControl的SeriesTemplate用法實例,實現(xiàn)了餅狀Series百分比顯示的效果,具有一定的參考借鑒價值,需要的朋友可以參考下
    2014-10-10
  • c# rsa加密解密詳解

    c# rsa加密解密詳解

    這篇文章主要介紹了c# rsa加密解密的的相關(guān)資料,文中代碼非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • C#開發(fā)webService接口的流程步驟

    C#開發(fā)webService接口的流程步驟

    在C#中,Web Service 接口是一種用于遠(yuǎn)程程序間的通信機制,它允許客戶端通過HTTP協(xié)議訪問服務(wù)器端提供的功能和服務(wù),本文給大家詳細(xì)介紹了C#開發(fā)webService接口的流程步驟,需要的朋友可以參考下
    2024-11-11
  • C#實現(xiàn)chart控件動態(tài)曲線繪制

    C#實現(xiàn)chart控件動態(tài)曲線繪制

    這篇文章主要為大家詳細(xì)介紹了C#實現(xiàn)chart控件動態(tài)曲線繪制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C#?wpf?無邊框窗口添加陰影效果的實現(xiàn)

    C#?wpf?無邊框窗口添加陰影效果的實現(xiàn)

    在本篇內(nèi)容中小編給大家整理了一篇關(guān)于C#?wpf?無邊框窗口添加陰影效果的具體方法內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下
    2022-11-11
  • C#實現(xiàn)SMTP服務(wù)發(fā)送郵件的示例代碼

    C#實現(xiàn)SMTP服務(wù)發(fā)送郵件的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用C#實現(xiàn)SMTP服務(wù)發(fā)送郵件的功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12
  • C# Winform實現(xiàn)石頭剪刀布游戲

    C# Winform實現(xiàn)石頭剪刀布游戲

    這篇文章主要為大家詳細(xì)介紹了Winform實現(xiàn)石頭剪刀布游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • C# Unicode編碼解碼的實現(xiàn)

    C# Unicode編碼解碼的實現(xiàn)

    本文主要介紹了C# Unicode編碼解碼的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06

最新評論