今天给大家推荐一款高性能敏感词检测开源库。

项目简介

这是一款基于.Net开发的、高性能敏感词工具箱,支持繁简互换、全角半角互换,拼音模糊搜索等功能。功能强大、高性能,秒级检测亿级别的文章。

技术架构

1、跨平台:采用.Net Core3.1开发,支持跨平台。可以部署在Docker, Windows, Linux, Mac。

项目结构

使用方法

敏感词检测

过滤敏感词,可以设置跳字长度,默认全角转半角、忽略大小写、跳词、重复词、黑名单。返回结果包含:关键字、关键字起始位置、结束位置、关键字序号等信息。

    string s = "中国|国人|zg人";string test = "我是中国人";StringSearch iwords = new StringSearch();iwords.SetKeywords(s.Split('|'));var b = iwords.ContainsAny(test);Assert.AreEqual(true, b);var f = iwords.FindFirst(test);Assert.AreEqual("中国", f);var all = iwords.FindAll(test);Assert.AreEqual("中国", all[0]);Assert.AreEqual("国人", all[1]);Assert.AreEqual(2, all.Count);var str = iwords.Replace(test, '*');Assert.AreEqual("我是***", str);

敏感词通配符检测

支持正则表达式类型:.?[]|,通过正则表达式可以进行模糊匹配,提升检测精准度。

    string s = ".[中美]国|国人|zg人";string test = "我是中国人";WordsMatch wordsSearch = new WordsMatch();wordsSearch.SetKeywords(s.Split('|'));var b = wordsSearch.ContainsAny(test);Assert.AreEqual(true, b);var f = wordsSearch.FindFirst(test);Assert.AreEqual("是中国", f.Keyword);var alls = wordsSearch.FindAll(test);Assert.AreEqual("是中国", alls[0].Keyword);Assert.AreEqual(".[中美]国", alls[0].MatchKeyword);Assert.AreEqual(1, alls[0].Start);Assert.AreEqual(3, alls[0].End);Assert.AreEqual(0, alls[0].Index);//返回索引Index,默认从0开始Assert.AreEqual("国人", alls[1].Keyword);Assert.AreEqual(2, alls.Count);var t = wordsSearch.Replace(test, '*');Assert.AreEqual("我****", t);

拼音转换、繁简转换、数字转大小写操作

此工具箱,集成了繁体简体互转、拼音转换、首字母提取、数字转大小写,使用例子如下:

// 转成简体WordsHelper.ToSimplifiedChinese("我愛中國");WordsHelper.ToSimplifiedChinese("我愛中國",1);// 港澳繁体 转 简体WordsHelper.ToSimplifiedChinese("我愛中國",2);// 台湾正体 转 简体// 转成繁体WordsHelper.ToTraditionalChinese("我爱中国");WordsHelper.ToTraditionalChinese("我爱中国",1);// 简体 转 港澳繁体WordsHelper.ToTraditionalChinese("我爱中国",2);// 简体 转 台湾正体// 转成全角WordsHelper.ToSBC("abcABC123");// 转成半角WordsHelper.ToDBC("abcABC123");// 数字转成中文大写WordsHelper.ToChineseRMB(12345678901.12);// 中文转成数字WordsHelper.ToNumber("壹佰贰拾叁亿肆仟伍佰陆拾柒万捌仟玖佰零壹元壹角贰分");// 获取全拼WordsHelper.GetPinyin("我爱中国");//WoAiZhongGuo   WordsHelper.GetPinyin("我爱中国",",");//Wo,Ai,Zhong,Guo   WordsHelper.GetPinyin("我爱中国",true);//WǒÀiZhōngGuó// 获取首字母WordsHelper.GetFirstPinyin("我爱中国");//WAZG// 获取全部拼音WordsHelper.GetAllPinyin('传');//Chuan,Zhuan// 获取姓名WordsHelper.GetPinyinForName("单一一")//ShanYiYiWordsHelper.GetPinyinForName("单一一",",")//Shan,Yi,YiWordsHelper.GetPinyinForName("单一一",true)//ShànYīYī

**性能对比
**

下面我们用户1000字字符串,进行10万次性能对比,看看对比结果,测试代码如下:

            ReadBadWord();var text = File.ReadAllText("Talk.txt");Console.Write("-------------------- FindFirst OR ContainsAny 100000次 --------------------
");Run("TrieFilter", () => { tf1.HasBadWord(text); });Run("FastFilter", () => { ff.HasBadWord(text); });Run("StringSearch(ContainsAny)", () => { stringSearch.ContainsAny(text); });Run("StringSearchEx(ContainsAny)--- WordsSearchEx(ContainsAny)代码相同", () => { stringSearchEx.ContainsAny(text); });Run("StringSearchEx2(ContainsAny)--- WordsSearchEx2(ContainsAny)代码相同", () => { stringSearchEx2.ContainsAny(text); });Run("StringSearchEx3(ContainsAny)--- WordsSearchEx3(ContainsAny)代码相同", () => { stringSearchEx3.ContainsAny(text); });Run("IllegalWordsSearch(ContainsAny)", () => { illegalWordsSearch.ContainsAny(text); });Run("StringSearch(FindFirst)", () => { stringSearch.FindFirst(text); });Run("StringSearchEx(FindFirst)", () => { stringSearchEx.FindFirst(text); });Run("StringSearchEx2(FindFirst)", () => { stringSearchEx2.FindFirst(text); });Run("StringSearchEx3(FindFirst)", () => { stringSearchEx3.FindFirst(text); });Run("WordsSearch(FindFirst)", () => { wordsSearch.FindFirst(text); });Run("WordsSearchEx(FindFirst)", () => { wordsSearchEx.FindFirst(text); });Run("WordsSearchEx2(FindFirst)", () => { wordsSearchEx2.FindFirst(text); });Run("WordsSearchEx3(FindFirst)", () => { wordsSearchEx3.FindFirst(text); });Run("IllegalWordsSearch(FindFirst)", () => { illegalWordsSearch.FindFirst(text); });Console.Write("-------------------- Find All 100000次 --------------------
");Run("TrieFilter(FindAll)", () => { tf1.FindAll(text); });Run("FastFilter(FindAll)", () => { ff.FindAll(text); });Run("StringSearch(FindAll)", () => { stringSearch.FindAll(text); });Run("StringSearchEx(FindAll)", () => { stringSearchEx.FindAll(text); });Run("StringSearchEx2(FindAll)", () => { stringSearchEx2.FindAll(text); });Run("StringSearchEx3(FindAll)", () => { stringSearchEx3.FindAll(text); });Run("WordsSearch(FindAll)", () => { wordsSearch.FindAll(text); });Run("WordsSearchEx(FindAll)", () => { wordsSearchEx.FindAll(text); });Run("WordsSearchEx2(FindAll)", () => { wordsSearchEx2.FindAll(text); });Run("WordsSearchEx3(FindAll)", () => { wordsSearchEx3.FindAll(text); });Run("IllegalWordsSearch(FindAll)", () => { illegalWordsSearch.FindAll(text); });Console.Write("-------------------- Replace  100000次 --------------------
");Run("TrieFilter(Replace)", () => { tf1.Replace(text); });Run("FastFilter(Replace)", () => { ff.Replace(text); });Run("StringSearch(Replace)", () => { stringSearch.Replace(text); });Run("WordsSearch(Replace)", () => { wordsSearch.Replace(text); });Run("StringSearchEx(Replace)--- WordsSearchEx(Replace)代码相同", () => { stringSearchEx.Replace(text); });Run("StringSearchEx2(Replace)--- WordsSearchEx2(Replace)代码相同", () => { stringSearchEx2.Replace(text); });Run("StringSearchEx3(Replace)--- WordsSearchEx3(Replace)代码相同", () => { stringSearchEx3.Replace(text); });Run("IllegalWordsSearch(Replace)", () => { illegalWordsSearch.Replace(text); });Console.Write("-------------------- Regex  100次 --------------------
");Run(100, "Regex.IsMatch", () => { re.IsMatch(text); });Run(100, "Regex.Match", () => { re.Match(text); });Run(100, "Regex.Matches", () => { re.Matches(text); });Console.Write("-------------------- Regex used Trie tree  100次 --------------------
");Run(100, "Regex.IsMatch", () => { re2.IsMatch(text); });Run(100, "Regex.Match", () => { re2.Match(text); });Run(100, "Regex.Matches", () => { re2.Matches(text); });

执行10万次性能对比,结果如下:

从测试结果看,此工具比C#自带的正则效率高8.8倍,如果数量量越大性能优势越明显。

项目地址:https://github.com/toolgood/ToolGood.Words

- End -

推荐阅读

  • 推荐一个C#操作SVG图形矢量图的开源项目

  • 一个支持DOCX、PPTX、Html等文件合并、拆分、互相转换的C#开源项目

  • 盘点10个.NetCore实用的开源框架项目

  • 盘点5个C#开发的、可用于个人博客的系统

  • 一个基于Quartz.Net开发的Windows版本的进程监控

专注分享编程知识、热门有用有趣的开源项目

推荐基于.NetCore一款高性能敏感词检测开源库相关推荐

  1. ToolGood.Words一款高性能敏感词(非法词/脏字)检测过滤组件,附带繁体简体互换,支持全角半角互换,汉字转拼音,模糊搜索等功能。

    https://github.com/toolgood/ToolGood.Words ToolGood.Words 一款高性能非法词(敏感词)检测组件,附带繁体简体互换,支持全角半角互换,获取拼音首字 ...

  2. 测试.net开源敏感词检测库ToolGood.Words

      微信公众号"DotNet"看到介绍.net开源敏感词检测库ToolGood.Words的文章<.NET Core一款高性能敏感词检测开源库>,根据参考文献2中的测试 ...

  3. 写一个高性能的敏感词检测组件

    最近写了一个高性能的敏感词检测组件[ToolGood.Words]. 一.高性能,它的效率到底有多快? 如果将正则表达式的算法效率设为1,高性能可达到正则表达式的1.5万倍. 二.选一个巧妙的算法: ...

  4. 一种基于DFA算法的敏感词检测JAVA程序片段

    本文章提供一种基于DFA算法的敏感词检测JAVA程序片段,如下: 1.构造多叉树数据结构 import org.jetbrains.annotations.NotNull;/*** 多叉树* @aut ...

  5. java 敏感词检测

    在网上看到好多的敏感词检测,发现都是在推荐某某算法,但是敏感词全是利用文本去存放.在项目中不能很好的进行维护和管理(个人看法). 本文的敏感词的检测方式还是DFA算法检测,不过敏感词存放地址放入了Re ...

  6. 【敏感词检测】用DFA构建字典树完成敏感词检测任务

    任务概述 敏感词检测是各类平台对用户发布内容(UGC)进行审核的必做任务. 对于文本内容做敏感词检测,最简单直接的方法就是规则匹配.构建一个敏感词词表,然后与文本内容进行匹配,如发现有敏感词,则提交报 ...

  7. 一次敏感词检测开发记录

    需求 用户上传execl,前端解析excel ,解析之后,将excel的json数据,传给后端,后端通过关键字检测算法,返回你之前传的数据,并且附件敏感字.然后前端渲染数据只table.并且用户以到e ...

  8. 关于java中敏感词检测的一些总结

    之前项目里客户提出一个需求,需要对系统中使用文本转化成语音发送的功能进行敏感词检测,禁止用户提交有敏感词的语音.通过查询各方面资料,整理了大概几种方案: 项目启动时对载入敏感词库作为缓存(一个大map ...

  9. Go+PHP实现敏感词检测

    概述 广告,敏感词检测一直以来都是让人头疼的话题,仅仅通过添加敏感词列表是解决不了问题的.今天封禁了这个词,明天又会有新的违禁词冒出来,比起愚公无穷尽的子孙更甚. 敏感词匹配这种治标不治本的方法,在一 ...

  10. php访问小程序内容检测接口,关于小程序接入敏感词检测接口的坑

    接入 msgSecCheck 接口47001 错误码踩坑! 这是官方文档要求,写的有些笼统,根据开发者社区提供需要进行编码后在传参,以下为 php 代码示例/** * 敏感词检测 * @param $ ...

最新文章

  1. springmvc和mybatis整合关键配置
  2. Linux中的输入输出管理
  3. linux shell rm 删除子目录下 所有.o后缀文件
  4. PAT甲级1127 ZigZagging on a Tree (30分):[C++题解]之字形层次遍历树bfs实现一层一层读入
  5. 贝叶斯统计 传统统计_统计贝叶斯如何补充常客
  6. MFC中App、Doc、MainFrame、View各指针的互相获取
  7. Request 获取网址各片段
  8. visio安装报错 1:1935 2:{XXXXXXXX...
  9. premiere软件的使用(快速入门,迅速了解常用功能、常用快捷键、常用插件)——wsdchong
  10. 第22次 CCF CSP认证一二题题解及感悟
  11. 设计专业是计算机的吗,计算机平面设计是属于计算机什么专业范畴?
  12. AI吻合度100%,某业余6段棋手吊打围甲7段,疑似AI附体
  13. 装甲逆袭-玩家移动处理
  14. Cesium 添加天地图三维地形
  15. nodeBB项目开发中遇到的错误(nodeBB系列二)
  16. 【算法训练营学习笔记-Week01】数组和链表的比较以及LeetCode的做题反思
  17. Qt制作简单标签云(上)
  18. 推荐一款小巧而强大的截屏软件 FastStone Capture
  19. SQL如何构建多条件组合查询,而且不降低效率
  20. 极客学院mysql教程_干货分享 速成必备视频 六天带你玩转MySQL视频教程 数据库......

热门文章

  1. 如何使用 VNC 远程访问树莓派
  2. js代码在调试状态执行正确,但是正常使用时没有反应
  3. 诗歌九 声律启蒙(云对雨,雪对风,晚照对晴空)
  4. 802.11电源管理模式
  5. 2021徐州市36中学高考成绩查询,关注!徐州四星级高中高考成绩公布!江苏13市高分学霸真颜曝光!...
  6. 实验室gpu服务器集群 使用方法探索
  7. Word2013自动生成中英文目录
  8. 深度剖析Java集合之BitSet
  9. 鲁迅《狂人日记》全文
  10. 解决pr导入的视频在监控器里面画面会放大的问题