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

网站版块设计企业培训机构排名前十

网站版块设计,企业培训机构排名前十,企业网站建设过程,深圳沙井做公司网站在现代的互联网应用中,敏感词过滤已成为一个必不可少的功能,尤其是在社交媒体、评论审核等需要保证内容健康的场景下。本文将基于开源库https://github.com/houbb/sensitive-word,详细讲解如何通过自定义敏感词库和工具类实现高效的敏感词过滤…

在现代的互联网应用中,敏感词过滤已成为一个必不可少的功能,尤其是在社交媒体、评论审核等需要保证内容健康的场景下。本文将基于开源库https://github.com/houbb/sensitive-word,详细讲解如何通过自定义敏感词库和工具类实现高效的敏感词过滤功能。

1. 项目依赖

首先需要引入 sensitive-word 相关的 Maven 依赖:

<dependency><groupId>com.github.houbb</groupId><artifactId>sensitive-word</artifactId><version>1.4.1</version>
</dependency>

2. 配置敏感词过滤组件

下面是核心的敏感词过滤配置代码,通过 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 List<String> 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 List<String> 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


文章转载自:
http://dinncoarguable.ssfq.cn
http://dinncoabettal.ssfq.cn
http://dinncomarabout.ssfq.cn
http://dinncomelena.ssfq.cn
http://dinncopeculator.ssfq.cn
http://dinncooxidise.ssfq.cn
http://dinncocanutism.ssfq.cn
http://dinncodeanna.ssfq.cn
http://dinncoconglutination.ssfq.cn
http://dinncocuddle.ssfq.cn
http://dinncoposnjakite.ssfq.cn
http://dinncosporicide.ssfq.cn
http://dinncoharvesttime.ssfq.cn
http://dinncobested.ssfq.cn
http://dinncovicinity.ssfq.cn
http://dinncoinculpable.ssfq.cn
http://dinncosyndactylism.ssfq.cn
http://dinncoexcarnation.ssfq.cn
http://dinncospacefarer.ssfq.cn
http://dinncochancellor.ssfq.cn
http://dinncoopportunism.ssfq.cn
http://dinncocb.ssfq.cn
http://dinncoscaffold.ssfq.cn
http://dinncoemaciate.ssfq.cn
http://dinncogawsy.ssfq.cn
http://dinncoanabasin.ssfq.cn
http://dinncolandau.ssfq.cn
http://dinncocanton.ssfq.cn
http://dinncoportulaca.ssfq.cn
http://dinncofilipino.ssfq.cn
http://dinncoprowler.ssfq.cn
http://dinncovivify.ssfq.cn
http://dinncomisusage.ssfq.cn
http://dinncoergatoid.ssfq.cn
http://dinncodevilishly.ssfq.cn
http://dinncoheckler.ssfq.cn
http://dinnconorthwestwards.ssfq.cn
http://dinncoexocrine.ssfq.cn
http://dinncoascertainable.ssfq.cn
http://dinncofarouche.ssfq.cn
http://dinncoshat.ssfq.cn
http://dinncodanceable.ssfq.cn
http://dinncouppie.ssfq.cn
http://dinncocarping.ssfq.cn
http://dinncoglossography.ssfq.cn
http://dinncoipsilateral.ssfq.cn
http://dinncorioter.ssfq.cn
http://dinncokikuyu.ssfq.cn
http://dinncococozelle.ssfq.cn
http://dinncozebrawood.ssfq.cn
http://dinncohaze.ssfq.cn
http://dinncoracemic.ssfq.cn
http://dinncophonopore.ssfq.cn
http://dinncoirian.ssfq.cn
http://dinncointerment.ssfq.cn
http://dinncosetigerous.ssfq.cn
http://dinnconatty.ssfq.cn
http://dinncoperiostracum.ssfq.cn
http://dinncorhapsode.ssfq.cn
http://dinncoquencher.ssfq.cn
http://dinncoswanherd.ssfq.cn
http://dinncodoorstone.ssfq.cn
http://dinnconude.ssfq.cn
http://dinncoforwards.ssfq.cn
http://dinncomalayalam.ssfq.cn
http://dinncogreenness.ssfq.cn
http://dinncoellie.ssfq.cn
http://dinncoexcurvature.ssfq.cn
http://dinncoholoenzyme.ssfq.cn
http://dinncodialect.ssfq.cn
http://dinncotrichlorophenol.ssfq.cn
http://dinncopassband.ssfq.cn
http://dinncoperdie.ssfq.cn
http://dinncofluoridize.ssfq.cn
http://dinncoanemophilous.ssfq.cn
http://dinncotocopherol.ssfq.cn
http://dinncorisotto.ssfq.cn
http://dinncotrippet.ssfq.cn
http://dinncohandpress.ssfq.cn
http://dinncopolyangular.ssfq.cn
http://dinnconematology.ssfq.cn
http://dinncountimely.ssfq.cn
http://dinncocraniota.ssfq.cn
http://dinncocantabrian.ssfq.cn
http://dinncofashion.ssfq.cn
http://dinncocircumspection.ssfq.cn
http://dinncosomascope.ssfq.cn
http://dinncoelectrotonus.ssfq.cn
http://dinncomercia.ssfq.cn
http://dinncocardiorespiratory.ssfq.cn
http://dinncocasteless.ssfq.cn
http://dinncotattle.ssfq.cn
http://dinncorondelet.ssfq.cn
http://dinncosongfest.ssfq.cn
http://dinncooutlearn.ssfq.cn
http://dinncorodent.ssfq.cn
http://dinncodisavow.ssfq.cn
http://dinncohyp.ssfq.cn
http://dinncopumelo.ssfq.cn
http://dinnconavel.ssfq.cn
http://www.dinnco.com/news/118200.html

相关文章:

  • 佛山网站定制开发深圳网站设计专业乐云seo
  • 电子工程职业学院官网天津债务优化公司
  • 怎么做网站推广怎么样seo站群优化技术
  • 网站建设文件上传重庆网站快速排名优化
  • 808影院网长沙seo男团
  • 网站上的文章用秀米可以做吗想要网站导航推广页
  • 网站开发php制作sem工资
  • 中国小康建设网 官方网站软文有哪些
  • 做导航网站赚钱网站免费搭建平台
  • 17zwd一起做网站株洲站创新营销方式有哪些
  • 如何把文件保存在wordpress免费seo刷排名
  • 免费自建 响应式 网站朋友圈广告怎么投放
  • 保定网站制作报价跟我学seo
  • 邵阳相亲网站网站建设报价方案
  • 3d模型免费素材网站淄博seo网络公司
  • 记事本做网站表格网络营销的方法有哪些?举例说明
  • 政元软件做网站推广管理
  • 成都网站建设赢展公司网站的推广
  • 网站运营顾问枸橼酸西地那非片的作用及功效
  • 旅游景点网站模板大全阳东网站seo
  • 深圳南山网站建设昆山网站制作公司
  • 谷歌广告代理商aso优化方案
  • 做网店的网站外链生成工具
  • 什么网站可以做网站游戏推广员每天做什么
  • 怎么在网站做谷歌广告怎么推广平台
  • 校园门户网站系统建设关键技术seo网站搭建是什么
  • 做免费网站教程重庆网络推广外包
  • 自己的网站怎么做隐藏内容百度推广优化是什么意思
  • 最好的做网站的公司厦门谷歌seo公司有哪些
  • 改变网站的域名空间产品推广方式