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

2024-03-12 08:04

本文主要是介绍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 敏感词过滤工具框架。请勿发布涉及政治、广告、营销、翻墙、违反国家法律法规等内容。高性能敏感词检测过滤组件,附带繁体简体互换,支持全角半角互换,汉字转拼音,模糊搜索等功能。)

这篇关于sensitive-word 敏感词 违规文字检测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/800625

相关文章

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式

《C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式》Markdown凭借简洁的语法、优良的可读性,以及对版本控制系统的高度兼容性,逐渐成为最受欢迎的文档格式... 目录为什么要将文档转换为 Markdown 格式使用工具将 Word 文档转换为 Markdown(.

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2

Python实现一键PDF转Word(附完整代码及详细步骤)

《Python实现一键PDF转Word(附完整代码及详细步骤)》pdf2docx是一个基于Python的第三方库,专门用于将PDF文件转换为可编辑的Word文档,下面我们就来看看如何通过pdf2doc... 目录引言:为什么需要PDF转Word一、pdf2docx介绍1. pdf2docx 是什么2. by

如何Python使用设置word的页边距

《如何Python使用设置word的页边距》在编写或处理Word文档的过程中,页边距是一个不可忽视的排版要素,本文将介绍如何使用Python设置Word文档中各个节的页边距,需要的可以参考下... 目录操作步骤代码示例页边距单位说明应用场景与高级用China编程途小结在编写或处理Word文档的过程中,页边距是一个

SpringBoot如何对密码等敏感信息进行脱敏处理

《SpringBoot如何对密码等敏感信息进行脱敏处理》这篇文章主要为大家详细介绍了SpringBoot对密码等敏感信息进行脱敏处理的几个常用方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录​1. 配置文件敏感信息脱敏​​2. 日志脱敏​​3. API响应脱敏​​4. 其他注意事项​​总结

Python使用python-docx实现自动化处理Word文档

《Python使用python-docx实现自动化处理Word文档》这篇文章主要为大家展示了Python如何通过代码实现段落样式复制,HTML表格转Word表格以及动态生成可定制化模板的功能,感兴趣的... 目录一、引言二、核心功能模块解析1. 段落样式与图片复制2. html表格转Word表格3. 模板生

Java如何根据word模板导出数据

《Java如何根据word模板导出数据》这篇文章主要为大家详细介绍了Java如何实现根据word模板导出数据,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... pom.XML文件导入依赖 <dependency> <groupId>cn.afterturn</groupId>

Flutter实现文字镂空效果的详细步骤

《Flutter实现文字镂空效果的详细步骤》:本文主要介绍如何使用Flutter实现文字镂空效果,包括创建基础应用结构、实现自定义绘制器、构建UI界面以及实现颜色选择按钮等步骤,并详细解析了混合模... 目录引言实现原理开始实现步骤1:创建基础应用结构步骤2:创建主屏幕步骤3:实现自定义绘制器步骤4:构建U