当前位置: 首页 > news >正文

sensitive-word 敏感词 违规文字检测

1、快速开始

  • - JDK1.7+- Maven 3.x+

    2、Maven 引入

<!-- https://mvnrepository.com/artifact/com.github.houbb/sensitive-word --><dependency><groupId>com.github.houbb</groupId><artifactId>sensitive-word</artifactId><version>0.13.1</version></dependency>

3、spring接入及自定义敏感词库

定义:许的内容-返回的内容不被当做敏感词 [白名单]

import com.github.houbb.sensitive.word.api.IWordAllow;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Component;import java.util.List;@Component
public class MyWordAllow implements IWordAllow {@Overridepublic List<String> allow() {return StreamUtil.readAllLines("/backend_sensitive_word_allow.txt");}
}

定义:MyWordDeny  拒绝出现的数据-返回的内容被当做是敏感词 

import com.github.houbb.heaven.util.io.StreamUtil;
import com.github.houbb.sensitive.word.api.IWordDeny;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Component;import java.util.List;
@Component
public class MyWordDeny implements IWordDeny {@Overridepublic List<String> deny() {return StreamUtil.readAllLines("/backend_sensitive_word_deny.txt");}
}

文件位置:

白名单内容如下【backend_sensitive_word_allow.txt】:

duck
shit
chicken
fowl
sex
sexy
prostitute
gender

源码:

com.github.houbb.sensitive.word.support.deny.WordDenySystem.deny()

定义配置类:SensitiveWordConfig
import com.github.houbb.sensitive.word.api.IWordAllow;
import com.github.houbb.sensitive.word.api.IWordDeny;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.core.SensitiveWordHelper;
import com.github.houbb.sensitive.word.support.allow.WordAllows;
import com.github.houbb.sensitive.word.support.deny.WordDenys;
import com.github.houbb.sensitive.word.support.ignore.SensitiveWordCharIgnores;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author : qinjinyuan* @desc : TODO  请填写你的功能描述* @date : 2024/03/11 10:05*/
@Configuration
public class SpringSensitiveWordConfig {@Autowiredprivate MyWordAllow myDdWordAllow;@Autowiredprivate MyWordDeny myDdWordDeny;/*** 初始化引导类** @return 初始化引导类* @since 1.0.0*/@Beanpublic SensitiveWordBs sensitiveWordBs() {// 敏感词 = 系统 + 自定义IWordDeny wordDeny = WordDenys.chains(WordDenys.defaults(), myDdWordDeny);// 白名单 = 系统 + 自定义IWordAllow wordAllow = WordAllows.chains(WordAllows.defaults(), myDdWordAllow);return SensitiveWordBs.newInstance().wordAllow(wordAllow).wordDeny(wordDeny).charIgnore(SensitiveWordCharIgnores.specialChars())// 各种其他配置.numCheckLen(8).init();}
}

4、测试:

# 根据敏感词库,进行数据处理 
final String text2 = "F#U%C^K fuck gender the fuck bad fuck words.fuck";SensitiveWordBs sensitiveWordBs =  SpringUtils.getBean(SensitiveWordBs.class);String result = sensitiveWordBs.replace(text2);System.out.println(result);# 输出如下
******* **** gender the **** bad **** words*****

5、进阶:与jackson注解配合使用

有了这么好的工具,如何优雅的用在我们的系统中?

定义:反序列化类 (spring默认用jackson)

SensitiveDeserializer
import cn.hutool.core.text.CharSequenceUtil;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.sikaryofficial.common.core.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.jackson.JsonComponent;import java.io.IOException;/*** @author : qinjinyuan* @desc : 自定义反序列化器,敏感词处理* @date : 2023/12/14 9:53*/
@Slf4j
@JsonComponent
public class SensitiveDeserializer extends JsonDeserializer<String> {/*** 反序列化字符串 ,进行敏感词处理** @param jsonParser* @param deserializationContext* @return* @throws IOException* @throws JacksonException*/@Overridepublic String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {if(CharSequenceUtil.isBlank(jsonParser.getText())){return null;}SensitiveWordBs sensitiveWordBs =  SpringUtils.getBean(SensitiveWordBs.class);return sensitiveWordBs.replace(jsonParser.getText());}
}

使用jackson注解  在需要的request dto属性中添加即可:

@JsonDeserialize(using = SensitiveDeserializer.class)

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;public class XXXXReq{@JsonDeserialize(using = SensitiveDeserializer.class)private String remark;}

6、小结

敏感词工具,脱敏等,其实有hutool,但是对于效率及灵活度来说,这个开源敏感词工具更实用:

可自定义敏感词库;

可定义白名单;

支持分词查找;

支持跳过特殊字符;

基于 DFA 算法,性能为 7W+ QPS,应用无感;

。。。

作者 老马啸西风 github  star 1.9k

各大平台连敏感词库都没有的吗?sensitive-word java 开源敏感词工具入门使用 | Echo Blog

https://github.com/houbb/sensitive-word/blob/master/CHANGE_LOG.md

GitHub - houbb/sensitive-word: 👮‍♂️The sensitive word tool for java.(敏感词/违禁词/违法词/脏词。基于 DFA 算法实现的高性能 java 敏感词过滤工具框架。请勿发布涉及政治、广告、营销、翻墙、违反国家法律法规等内容。高性能敏感词检测过滤组件,附带繁体简体互换,支持全角半角互换,汉字转拼音,模糊搜索等功能。)

相关文章:

  • python字符串转换成字典
  • 【论文速读】| 大语言模型引导的协议模糊测试
  • 【Java探索之旅】运算符解析 算术运算符,关系运算符
  • 我把Spring Cloud的超详细资料介绍给你,面试官不会生气吧?geigei
  • 【完美实现】VITE + VUE3 + SVG图片解析+element-plus开发环境初始化(基于macos)
  • 面试宝典-【redis】
  • ECharts饼图图例消失踩的坑
  • 电玩城游戏大厅计时软件怎么用,佳易王计时计费管理系统软件定时语音提醒操作教程
  • mineadmin 快速安装部署(docker环境)
  • Go Zero微服务个人探究之路(十六)回顾api服务和rpc服务的本质
  • Avalonia之ListBox模版设置
  • SpringBoot 过滤器和拦截器的区别
  • 【C语言】文件操作篇-----程序文件和数据文件,文件的打开和关闭,二进制文件和文本文件,fopen,fclose【图文详解】
  • 修复Jenkins fossa扫描的时候报错的问题
  • PostgreSQL数据优化——死元组清理
  • 【JavaScript】通过闭包创建具有私有属性的实例对象
  • Angular数据绑定机制
  • canvas 高仿 Apple Watch 表盘
  • CentOS6 编译安装 redis-3.2.3
  • CSS盒模型深入
  • Java 网络编程(2):UDP 的使用
  • JavaScript 是如何工作的:WebRTC 和对等网络的机制!
  • Node + FFmpeg 实现Canvas动画导出视频
  • PHP那些事儿
  • SSH 免密登录
  • Zepto.js源码学习之二
  • -- 查询加强-- 使用如何where子句进行筛选,% _ like的使用
  • 关于 Cirru Editor 存储格式
  • 开源地图数据可视化库——mapnik
  • 买一台 iPhone X,还是创建一家未来的独角兽?
  • 巧用 TypeScript (一)
  • 腾讯视频格式如何转换成mp4 将下载的qlv文件转换成mp4的方法
  • ​​​​​​​Installing ROS on the Raspberry Pi
  • ​七周四次课(5月9日)iptables filter表案例、iptables nat表应用
  • #define,static,const,三种常量的区别
  • #LLM入门|Prompt#2.3_对查询任务进行分类|意图分析_Classification
  • (6)STL算法之转换
  • (floyd+补集) poj 3275
  • (java版)排序算法----【冒泡,选择,插入,希尔,快速排序,归并排序,基数排序】超详细~~
  • (层次遍历)104. 二叉树的最大深度
  • (附源码)springboot工单管理系统 毕业设计 964158
  • (十六)一篇文章学会Java的常用API
  • (四)linux文件内容查看
  • *** 2003
  • ./configure,make,make install的作用
  • ./include/caffe/util/cudnn.hpp: In function ‘const char* cudnnGetErrorString(cudnnStatus_t)’: ./incl
  • .bat批处理(四):路径相关%cd%和%~dp0的区别
  • .net core使用EPPlus设置Excel的页眉和页脚
  • .NET/C# 的字符串暂存池
  • .NET教程 - 字符串 编码 正则表达式(String Encoding Regular Express)
  • @cacheable 是否缓存成功_让我们来学习学习SpringCache分布式缓存,为什么用?
  • @serverendpoint注解_SpringBoot 使用WebSocket打造在线聊天室(基于注解)
  • @synthesize和@dynamic分别有什么作用?
  • @Transactional事务注解内含乾坤?
  • @Transient注解