Springboot 敏感词过滤

参考:网站是怎么屏蔽脏话的呢:简单学会SpringBoot项目敏感词、违规词过滤方案_springboot 项目关键词过滤-CSDN博客

【敏感词过滤】_wx60d2a462203aa的技术博客_51CTO博客

1、添加依赖

复制代码
<dependency>
            <groupId>com.github.houbb</groupId>
            <artifactId>sensitive-word</artifactId>
            <version>0.17.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>

2、SensitiveConfig

复制代码
@Configuration
public class SensitiveConfig {

    @Autowired
    private MyWordAllow myWordAllow;

    @Autowired
    private MyWordDeny myWordDeny;

    /**
     * 初始化引导类
     * @return 初始化引导类
     * @since 1.0.0
     */
    @Bean
    public SensitiveWordBs sensitiveWordBs() {
        // 配置默认敏感词 + 自定义敏感词
        IWordDeny wordDeny = WordDenys.chains(WordDenys.defaults(), myWordDeny);
        // 配置默认非敏感词 + 自定义非敏感词

        IWordAllow wordAllow = WordAllows.chains(WordAllows.defaults(), myWordAllow);

        return SensitiveWordBs.newInstance()
                // 忽略大小写
                .ignoreCase(true)
                // 忽略半角圆角
                .ignoreWidth(true)
                // 忽略数字的写法
                .ignoreNumStyle(true)
                // 忽略中文的书写格式:简繁体
                .ignoreChineseStyle(true)
                // 忽略英文的书写格式
                .ignoreEnglishStyle(true)
                // 忽略重复词
                .ignoreRepeat(false)
                // 是否启用数字检测
                .enableNumCheck(true)
                // 是否启用邮箱检测
                .enableEmailCheck(true)
                // 是否启用链接检测
                .enableUrlCheck(true)
                // 数字检测,自定义指定长度
                .numCheckLen(8)
                // 配置自定义敏感词
                .wordDeny(wordDeny)
                // 配置非自定义敏感词
                .wordAllow(wordAllow)
                .init();
    }

3、自定义 敏感词

复制代码
@Slf4j
@Component
public class MyWordDeny implements IWordDeny {

    @Override
    public List<String> deny() {
        List<String> list = new ArrayList<String>();;
        try {
            Resource mySensitiveWords = new ClassPathResource("sensitive/mySensitiveWords.txt");
            Path mySensitiveWordsPath = Paths.get(mySensitiveWords.getFile().getPath());
            list =  Files.readAllLines(mySensitiveWordsPath, StandardCharsets.UTF_8);
        } catch (IOException ioException) {
            log.error("读取敏感词文件错误!"+ ioException.getMessage());
        }
        return list;
    }

}

自定义非敏感词

复制代码
@Slf4j
@Component
public class MyWordAllow implements IWordAllow {

    @Override
    public List<String> allow() {
        List<String> list = new ArrayList<String>();;
        try {
            Resource myAllowWords = new ClassPathResource("sensitive/myAllowWords.txt");
            Path myAllowWordsPath = Paths.get(myAllowWords.getFile().getPath());
            list =  Files.readAllLines(myAllowWordsPath, StandardCharsets.UTF_8);
        } catch (IOException ioException) {
            log.error("读取非敏感词文件错误!"+ ioException.getMessage());
        }
        return list;
    }

}

3、 Service类

复制代码
@Component
public class SensitiveWordService {

    @Autowired
    private SensitiveWordBs sensitiveWordBs;

    // 刷新敏感词库与非敏感词库缓存
    public void refresh(){
        sensitiveWordBs.init();
    }
    // 判断是否含有敏感词
    public boolean contains(String text){
        return sensitiveWordBs.contains(text);
    }

    // 使用默认替换符 * 进行替换敏感词
    public String replace(String text){
        return sensitiveWordBs.replace(text);
    }

    // 返回所有敏感词
    public List<String> findAll(String text){
        return sensitiveWordBs.findAll(text);
    }
}

4、测试类

复制代码
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyGptApplication.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = MyGptApplication.class)
public class SensitiveTest {

    @Autowired
    private SensitiveWordService sensitiveWordService;

    @Test
    public void test() {
        String s = "赌博 test";
        String s1 = sensitiveWordService.replace(s);
        Boolean flag = sensitiveWordService.contains(s);
        if (flag) {
            System.out.println("请调整问题,避免受限内容,我们将更好地协助您。");
        }
        System.out.println(s1);
    }

}

5、文本目录

相关推荐
纪莫23 分钟前
A公司一面:类加载的过程是怎么样的? 双亲委派的优点和缺点? 产生fullGC的情况有哪些? spring的动态代理有哪些?区别是什么? 如何排查CPU使用率过高?
java·java面试⑧股
风一样的树懒1 小时前
死信队列:你在正确使用么?
后端
RoyLin1 小时前
TypeScript设计模式:门面模式
前端·后端·typescript
JavaGuide1 小时前
JDK 25(长期支持版) 发布,新特性解读!
java·后端
用户3721574261351 小时前
Java 轻松批量替换 Word 文档文字内容
java
白鲸开源1 小时前
教你数分钟内创建并运行一个 DolphinScheduler Workflow!
java
weiwenhao1 小时前
关于 nature 编程语言
人工智能·后端·开源
薛定谔的算法1 小时前
phoneGPT:构建专业领域的检索增强型智能问答系统
前端·数据库·后端
RoyLin2 小时前
TypeScript设计模式:责任链模式
前端·后端·typescript
RoyLin2 小时前
TypeScript设计模式:装饰器模式
前端·后端·typescript