当前位置: 代码网 > it编程>编程语言>Asp.net > 基于C#检测敏感词功能

基于C#检测敏感词功能

2024年11月09日 Asp.net 我要评论
今天策划给我一个任务 —— 检测昵称中是否含有敏感词功能,然后丢给我两个压缩包,我解压一看:有的txt文件是一行一个词:有的txt文件是按逗号分隔开:不管是什么格式的总之量非常

今天策划给我一个任务  ——  检测昵称中是否含有敏感词功能,然后丢给我两个压缩包,我解压一看:

有的txt文件是一行一个词:

有的txt文件是按逗号分隔开:

不管是什么格式的总之量非常多,把我这辈子脏话都囊括了

读取txt文件数据

然后我得先对这些txt文件进行处理转换成我们能用的格式:一开始我直接for循环查找是否含有敏感词,后边找资料看到一个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("昵称中存在无法使用的字符,请修改后再次确认");
            }
        }
    }
 
    //用换行分割的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("读取文件时出现错误: " + 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("读取文件时出现错误: " + e.message);
        }
 
        return elements;
    }
}

这样处理过后的数据就是list<string>,或者可以处理成数组、集合都可以 

我把处理出来的数据放在hashset中

/// <summary>
/// 敏感词词库
/// </summary>
public static hashset<string> maskword = new hashset<string>
{
   "敏感词1","敏感词2","敏感词3","..."
}

c#版dfa算法

然后通过c#版的dfa算法判断昵称中是否含有敏感词返回bool型放在工具类中使用:

java实现敏感词过滤(dfa算法)

敏感词管理(dfa算法实现)

/// <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; // 未匹配到敏感词
}

到此这篇关于基于c#检测敏感词功能的文章就介绍到这了,更多相关c#检测敏感词内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com