网站支付宝支付接口申请,建设好网站如何上传百度,做网站前期构架图,模具机械设备东莞网站建设在现代的互联网应用中#xff0c;敏感词过滤已成为一个必不可少的功能#xff0c;尤其是在社交媒体、评论审核等需要保证内容健康的场景下。本文将基于开源库https://github.com/houbb/sensitive-word#xff0c;详细讲解如何通过自定义敏感词库和工具类实现高效的敏感词过滤…在现代的互联网应用中敏感词过滤已成为一个必不可少的功能尤其是在社交媒体、评论审核等需要保证内容健康的场景下。本文将基于开源库https://github.com/houbb/sensitive-word详细讲解如何通过自定义敏感词库和工具类实现高效的敏感词过滤功能。
1. 项目依赖
首先需要引入 sensitive-word 相关的 Maven 依赖
dependencygroupIdcom.github.houbb/groupIdartifactIdsensitive-word/artifactIdversion1.4.1/version
/dependency2. 配置敏感词过滤组件
下面是核心的敏感词过滤配置代码通过 SensitiveWordBs 构建过滤器并加载自定义敏感词和允许词。 配置类代码
package cn.yujky.study.sensitive.config;import cn.yujky.study.sensitive.service.impl.MyWordAllowImpl;
import cn.yujky.study.sensitive.service.impl.MyWordDenyImpl;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.support.allow.WordAllows;
import com.github.houbb.sensitive.word.support.deny.WordDenys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;gframework.context.annotation.Configuration;/*** 敏感词配置*/
Slf4j
Configuration
public class SensitiveWordConfig {Autowiredprivate MyWordDenyImpl myWordDeny;Autowiredprivate MyWordAllowImpl myWordAllow;/*** 初始化敏感词过滤器** return 配置好的敏感词过滤引导类*/Beanpublic SensitiveWordBs sensitiveWordBs() {log.info(本地敏感词库初始化中...);SensitiveWordBs init SensitiveWordBs.newInstance().wordDeny(WordDenys.chains(WordDenys.defaults(), myWordDeny)).wordAllow(WordAllows.chains(WordAllows.defaults(), myWordAllow)).init();log.info(本地敏感词库初始化完成);return init;}
}3 自定义敏感词库
通过实现 WordDeny 和 WordAllow 接口可以分别配置屏蔽词和允许词。以下是示例代码
3.1 自定义屏蔽词MyWordDenyImpl
package cn.yujky.study.sensitive.service.impl;import com.github.houbb.sensitive.word.api.IWordDeny;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;/*** name: MyWordDeny* description: p/p* author: yujky* date: 2024/12/27 11:18*/
Slf4j
Service
AllArgsConstructor
public class MyWordDenyImpl implements IWordDeny {private final ResourceLoader resourceLoader;Overridepublic ListString deny() {// 加载resource目录下的sensiticeWord.txt文本中的敏感词Resource resource resourceLoader.getResource(classpath:sensiticeWord.txt);// 将文件内容读取为字符串try {String content null;content new String(Files.readAllBytes(Paths.get(resource.getURI())));log.info(敏感词库加载完成敏感词数量为{}, content.split(\\n).length);log.info(敏感词库加载完成敏感词\\n {}, content);// 按换行分割return Arrays.stream(content.split(\\n)).distinct().toList();} catch (IOException e) {throw new RuntimeException(e);}}
}这里的敏感词库我是直接放在resource目录下的sensiticeWord.txt文本中你也可以改为从数据库或者其他存储工具中读取
3.2 自定义允许词MyWordAllowImpl
package cn.yujky.study.sensitive.service.impl;import com.github.houbb.sensitive.word.api.IWordAllow;
import org.springframework.stereotype.Service;import java.util.Arrays;
import java.util.List;/*** name: MyWordAllowImpl* description: p/p* author: yujky* date: 2024/12/27 11:20*/
Service
public class MyWordAllowImpl implements IWordAllow {Overridepublic ListString allow() {return Arrays.asList(五星红旗);}
}4. 清洗文本工具类
在敏感词检测前通常需要对文本进行预处理例如移除特殊字符、表情符号等。以下是清洗文本的工具类示例代码
package cn.yujky.study.sensitive;Slf4j
public class SensitiveTextCleaner {/*** 移除 Emoji 表情** param text 输入文本* return 清洗后的文本*/public static String removeEmojis(String text) {String emojiRegex [\\x{1F600}-\\x{1F64F}\\x{1F300}-\\x{1F5FF}\\x{1F680}-\\x{1F6FF}\\x{1F700}-\\x{1F77F}\\x{1F780}-\\x{1F7FF}\\x{1F800}-\\x{1F8FF}\\x{1F900}-\\x{1F9FF}\\x{1FA00}-\\x{1FA6F}\\x{1FA70}-\\x{1FAFF}\\x{2600}-\\x{26FF}\\x{2700}-\\x{27BF}];return text.replaceAll(emojiRegex, );}/*** 移除特殊字符** param text 输入文本* return 清洗后的文本*/public static String removeSpecialCharacters(String text) {return text.replaceAll([^a-zA-Z0-9\u4e00-\u9fa5], );}/*** 综合清洗文本移除表情与特殊字符** param text 输入文本* return 清洗后的文本*/public static String cleanText(String text) {text removeEmojis(text); // 移除 Emojitext removeSpecialCharacters(text); // 移除特殊字符return text.trim().toLowerCase(); // 转小写并去除多余空格}
}5. 敏感词过滤测试
在 Spring Boot 项目中通过单元测试验证过滤功能以下为完整的测试代码
package cn.yujky.study.sensitive;import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;Slf4j
SpringBootTest
class YujkySensitiveApplicationTests {Autowiredprivate SensitiveWordBs sensitiveWordBs;Testvoid contextLoads() {String text 操他;String cleanText SensitiveTextCleaner.cleanText(text);log.info(原文本: {}, 清洗后文本: {}, text, cleanText);// 检查是否包含敏感词boolean containsOriginal sensitiveWordBs.contains(text);boolean containsCleaned sensitiveWordBs.contains(cleanText);log.info(是否包含敏感词原文本: {}, containsOriginal);log.info(是否包含敏感词清洗后文本: {}, containsCleaned);// 控制台输出System.out.println(原文本检测结果: containsOriginal);System.out.println(清洗后文本检测结果: containsCleaned);}
}5.1 测试结果示例
假设敏感词库中包含 “操” 和 “他”
原文本: 操他, 清洗后文本: 操他
是否包含敏感词原文本: false
是否包含敏感词清洗后文本: true这里建议对原文本以及清洗后的文本都进行一次检测增加敏感词的检测力度
如果你在开发过程中有其他需求或问题欢迎交流 https://web.yujky.cn/ 用户名cxks 密码 cxks123