.NET 6 实现敏感词过滤
创始人
2025-05-29 14:19:12

一、什么是敏感词过滤?

敏感词过滤是一种处理网络内容的技术,可以检测和过滤出网络中的敏感/违禁词汇。它通过给定的关键字或字符串,判断网络内容是否包含某些敏感信息,从而防止违反法律法规的信息流通。
通常,可以使用两种方法来过滤敏感词:

  1. 黑名单过滤:即定义一个黑名单,将所有敏感词择记录在其中,然后对输入的文本进行对比,如果发现有敏感词,就将其过滤掉。
  2. 白名单过滤:即定义一个白名单,将所有不敏感的词汇记录在其中,然后对输入的文本进行对比,如果发现有不在白名单中的词汇,就将其过滤掉。

二、ToolGood.Words是什么?

ToolGood.Words是一款高性能非法词(敏感词)检测组件,附带繁体简体互换,支持全角半角互换,获取拼音首字母,获取拼音字母,拼音模糊搜索等功能。
ToolGood.Words的源码网站:ToolGood.Words源码网站

三、在Visual Studio中安装ToolGood.Words

3.1、右键项目解决方案,选择“管理NuGet程序包”,如下图所示:

选择“管理NuGet程序包”

3.2、切换到“浏览”选项卡,搜索“ToolGood.Words”并安装:

安装ToolGood.Words
安装完之后最好重新编译生成项目

四、创建“subContentCheck”类

敏感/违禁词汇因特殊内容不便上传,可自行在网站上查找

using Microsoft.AspNetCore.Http;
using Microsoft.CodeAnalysis.Text;
using System.Collections;
using ToolGood.Words;
using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;namespace WebApplication1
{/// /// 提交的内容敏感违禁词检查类/// public class subContentCheck{/// /// 本地静态文件地址路径/// private IHostingEnvironment _hostingEnv;/// /// 敏感词库/// private string dictionaryPath = "/sensitiveWords/sensitiveWords.txt";/// /// 敏感链接、网站、网址库/// private string urlsPath = "/sensitiveWords/IllegalUrls.txt";/// /// 保存敏感词组/// public string[] Words { get; set; }/// /// 一个参数的构造函数/// /// 本地静态文件地址路径public subContentCheck(IHostingEnvironment hostingEnv){_hostingEnv = hostingEnv;InitDictionary();}/// /// 初始化内存敏感词库/// public void InitDictionary(){Words = new string[] { };string wordsPath = _hostingEnv.WebRootPath + dictionaryPath;string urlPath = _hostingEnv.WebRootPath + urlsPath;string[] readAllWords = System.IO.File.ReadAllLines(wordsPath, System.Text.Encoding.UTF8);string[] readAllurl = System.IO.File.ReadAllLines(urlPath, System.Text.Encoding.UTF8);//由于数组是非动态的,不能进行动态的添加,所有先将它转成list,操作ArrayList arrayList = new ArrayList(Words.ToList());if (readAllWords.Length > 0 || readAllurl.Length > 0){if (readAllWords.Length > 1){foreach (string itemWords in readAllWords){string[] allSplitWords = itemWords.Split('|');foreach (string itemSplitWords in allSplitWords){if (!string.IsNullOrEmpty(itemSplitWords)){arrayList.Add(itemSplitWords);}}}}else{if (readAllWords.Length == 1){string[] allSplitWords = readAllWords[0].Split('|');foreach (string itemSplitWords in allSplitWords){if (!string.IsNullOrEmpty(itemSplitWords)){arrayList.Add(itemSplitWords);}}}}if (readAllurl.Length > 1){foreach (string itemUrls in readAllurl){string[] allSplitUrls = itemUrls.Split('|');foreach (string itemSplitUrls in allSplitUrls){if (!string.IsNullOrEmpty(itemSplitUrls)){arrayList.Add(itemSplitUrls);}}}}else{if (readAllurl.Length == 1){string[] allSplitUrls = readAllurl[0].Split('|');foreach (string itemSplitUrls in allSplitUrls){if (!string.IsNullOrEmpty(itemSplitUrls)){arrayList.Add(itemSplitUrls);}}}}}//我们在将list转换成String[]数组 Words = (string[])arrayList.ToArray(typeof(string));}/// /// 过滤替换敏感词/// /// 需要过滤替换的原内容/// 敏感词替换的字符;默认替换为‘*’/// 返回状态码;为空则表示传入的内容为空;“0”:设置违禁词时发生错误;“1”:敏感内容替换时发生错误;“2”:需要替换的文本内容为空;其余则返回替换成功的字符串内容public string FilterWithChar(string sourceText, char replaceChar = '*'){if (!string.IsNullOrEmpty(sourceText)){string result = "";WordsSearch wordsSearch = new WordsSearch();try{wordsSearch.SetKeywords(Words);}catch (Exception ex){result = "0";return result;}try{result = wordsSearch.Replace(sourceText, replaceChar);return result;}catch (Exception ex){return result = "1";}}else{return "2";}}}
}

五、写API接口

/// 
/// 进行敏感词脱敏
/// 
/// 需要脱敏的文本内容
/// 
[HttpPost]
public IActionResult sensitive_words_replace2(string sourctText)
{string resultStr = "";//实例化敏感词库subContentCheck strCheck = new subContentCheck(_hostingEnv);if (string.IsNullOrEmpty(sourctText)){return Json(new { code = 230, msg = "需要替换的文本内容为空!", resultStr = resultStr });}try{resultStr = strCheck.FilterWithChar(sourctText);string resMsg = "";int resCode = 200;if (resultStr=="0"){resCode = 210;resultStr = "";resMsg = "设置违禁词时发生错误,请联系管理员!";}else if (resultStr=="1"){resCode = 240;resultStr = "";resMsg = "敏感内容替换时发生错误!";}else if (resultStr == "2"){resCode = 260;resultStr = "";resMsg = "需要替换的文本内容为空!";}else{resCode = 200;resMsg = "敏感词替换请求成功!";}return Json(new { code = resCode, msg = resMsg, resultStr = resultStr });}catch (Exception ex){return Json(new { code = 220, msg = "敏感内容替换时发生错误!", resultStr = "" });}
}

六、前端封装JS方法

/*** 敏感词/违禁词替换* @param {string} requestUrl 请求后端接口的路径* @param {string} sourctText 需要进行替换的内容* @param {string} boxid 将替换成功之后的内容赋值的元素容器id属性名* @param {object} layui Layui实例* @returns 替换之后的文本内容*/
function sensitive_words_replace(sourctText, boxid, layui) {let resultStr = "";//let url = ["/Home/sensitive_words_replace", "/Home/sensitive_words_replace1", "/Home/sensitive_words_replace2"];$.ajax({url: "/Home/sensitive_words_replace2",//请求后端接口的路径dataType: "JSON",type: "POST",data: {"sourctText": sourctText},success: function (res) {let resCode = res.code;let resMsg = res.msg;if ((resCode == "210" || resCode == 210) || (resCode == 220 || resCode == "220") || (resCode == 230 || resCode == "230") || (resCode == 240 || resCode == "240") || (resCode == 260 || resCode == "260")) {//返回数据后关闭loadinglayer.closeAll();resultStr = res.resultStr;layui.layer.alert(resMsg, { icon: 5, title: "温馨提示", closeBtn: 0 });} else if (resCode == 200 || resCode == "200") {resultStr = res.resultStr;$("#" + boxid).val(resultStr);//返回数据后关闭loadinglayer.closeAll();}},error: function (error) {//返回数据后关闭loadinglayer.closeAll();layui.layer.alert(error, { icon: 5, title: "温馨提示", closeBtn: 0 });}});return resultStr;
}

相关内容

热门资讯

首款AR眼镜曝光,Vidda正... 在AWE 2025上,Vidda首次展示了其AR眼镜新品,这不禁让人想起,2月25日,海信刚刚与AR...
布鲁可积木人在“长大” 文 | 玩世代一个月前,大家还在复盘「布鲁可如何靠9.9积木人弯道超车」「奥特曼撑起一个IPO」。如...
京东搅动外卖业,二级市场为何还... 文 | 思辨财经在“内卷化”的话语体系中,“零和博弈”便开始成为市场分析的主要框架。这也就使得舆论的...
阿里在画一张什么样的AI蓝图? 文 | 字母榜最近聊AI离不开阿里。继宣布未来3年投资3800亿元,用以AI和云计算的基础设施建设后...
从年报看,招行到底在押注什么?... “2024年是充满挑战的一年,面对国际形势复杂多变、国内有效信贷需求不足、利率下行利差收窄、金融风险...