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

自学php做网站哈尔滨优化调整人员流动管理

自学php做网站,哈尔滨优化调整人员流动管理,程序源代码下载网站,网站找到后台了 如何破解账号 密码在现代的互联网应用中,敏感词过滤已成为一个必不可少的功能,尤其是在社交媒体、评论审核等需要保证内容健康的场景下。本文将基于开源库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://www.hkea.cn/news/511510/

相关文章:

  • 上海正规网站制作价格可口可乐软文营销案例
  • 番禺网站 建设信科网络站长之家ping
  • 建筑工程施工承包合同关键词优化报价推荐
  • 网站可以免费看企业网站系统
  • 中华人民共和国建设部网站seo怎么快速提高排名
  • 南宁做网站的有几家东莞网络营销网站建设
  • 苏州知名网站建设开发新区seo整站优化公司
  • 政府建设网站计划书品牌营销策略包括哪些内容
  • 深圳市做网站百度seo排名点击器app
  • 五莲网站建设维护推广网络营销推广及优化方案
  • 重庆网红整站多关键词优化
  • 动易网站cms一级消防工程师考试
  • wordpress更新报错想找搜索引擎优化
  • 提供网站建设费用资源网
  • wordpress怎么使用主题seo优化评论
  • 柳州做网站如何建网站详细步骤
  • 黄岛做网站哪家好四川seo关键词工具
  • dede门户网站模版写软文推广
  • 网站开发者排名开发一个app平台大概需要多少钱?
  • 做网站 博客百度推广助手客户端
  • 温州市手机网站制作哪家好爱站网长尾词挖掘
  • 党委网站建设要求凡科建站靠谱吗
  • wordpress 安卓客户端福建seo优化
  • 襄阳seo技术长沙seo网站优化
  • 做一的同志小说网站做seo要投入什么
  • 网站的文件结构百度搜索排名怎么收费
  • 全景网站app网络营销工具分析
  • 南京建设工程交易中心网站seo是什么的简称
  • 利用vps做网站关键字排名查询
  • 常熟网站制作找哪家好品牌型网站制作价格