sensitive-word v0.13 特性版本发布 支持英文单词全词匹配

拓展阅读

sensitive-word-admin v1.3.0 发布 如何支持分布式部署?
sensitive-word-admin 敏感词控台 v1.2.0 版本开源
sensitive-word 基于 DFA 算法实现的高性能敏感词工具介绍
更多技术交流

业务背景

对于英文单词 Disburse 之类的,其中的 sb 字母会被替换,要怎么处理,能不能只有整个单词匹配的时候才替换。

针对匹配词进一步判断

说明

支持版本:v0.13.0

有时候我们可能希望对匹配的敏感词进一步限制,比如虽然我们定义了【av】作为敏感词,但是不希望【have】被匹配。

就可以自定义实现 wordResultCondition 接口,实现自己的策略。

系统内置的策略在 WordResultConditions#alwaysTrue() 恒为真,WordResultConditions#englishWordMatch() 则要求英文必须全词匹配。

入门例子

原始的默认情况:

java 复制代码
final String text = "I have a nice day。";

List<String> wordList = SensitiveWordBs.newInstance()
        .wordDeny(new IWordDeny() {
            @Override
            public List<String> deny() {
                return Collections.singletonList("av");
            }
        })
        .wordResultCondition(WordResultConditions.alwaysTrue())
        .init()
        .findAll(text);
Assert.assertEquals("[av]", wordList.toString());

我们可以指定为英文必须全词匹配。

java 复制代码
final String text = "I have a nice day。";

List<String> wordList = SensitiveWordBs.newInstance()
        .wordDeny(new IWordDeny() {
            @Override
            public List<String> deny() {
                return Collections.singletonList("av");
            }
        })
        .wordResultCondition(WordResultConditions.englishWordMatch())
        .init()
        .findAll(text);
Assert.assertEquals("[]", wordList.toString());

当然可以根据需要实现更加复杂的策略。

如何自定义自己的策略

可以参考 WordResultConditions#englishWordMatch() 实现类,只需要继承 AbstractWordResultCondition 实现对应的方法即可。

策略的定义

以 englishWordMatch 实现类为例:

java 复制代码
package com.github.houbb.sensitive.word.support.resultcondition;

import com.github.houbb.heaven.util.lang.CharUtil;
import com.github.houbb.heaven.util.util.CharsetUtil;
import com.github.houbb.sensitive.word.api.IWordContext;
import com.github.houbb.sensitive.word.api.IWordResult;
import com.github.houbb.sensitive.word.constant.enums.WordValidModeEnum;

/**
 * 英文单词必须要全词匹配
 *
 * https://github.com/houbb/sensitive-word/issues/45
 *
 * @since 0.13.0
 */
public class WordResultConditionEnglishWordMatch extends AbstractWordResultCondition {

    @Override
    protected boolean doMatch(IWordResult wordResult, String text, WordValidModeEnum modeEnum, IWordContext context) {
        final int startIndex = wordResult.startIndex();
        final int endIndex = wordResult.endIndex();
        // 判断当前是否为英文单词
        for(int i = startIndex; i < endIndex; i++) {
            char c = text.charAt(i);
            if(!CharUtil.isEnglish(c)) {
                return true;
            }
        }

        // 判断处理,判断前一个字符是否为英文。如果是,则不满足
        if(startIndex > 0) {
            char preC = text.charAt(startIndex-1);
            if(CharUtil.isEnglish(preC)) {
                return false;
            }
        }

        // 判断后一个字符是否为英文
        if(endIndex < text.length() - 1) {
            char afterC = text.charAt(endIndex+1);
            if(CharUtil.isEnglish(afterC)) {
                return false;
            }
        }

        return true;
    }

}

策略的指定

然后用引导类指定我们的策略即可:

java 复制代码
List<String> wordList = SensitiveWordBs.newInstance()
        .wordResultCondition(new WordResultConditionEnglishWordMatch())
        .init()
        .findAll(text);

小结

实际应用的场景会被预想的复杂,所以此处设计为接口,内置一些常见的实现策略。

同时支持用户自定义拓展。

开源代码

github.com/houbb/sensi... 本文由博客一文多发平台 OpenWrite 发布!

相关推荐
啾啾Fun1 分钟前
Java反射操作百倍性能优化
java·性能优化·反射·缓存思想
20岁30年经验的码农8 分钟前
若依微服务Openfeign接口调用超时问题
java·微服务·架构
曲莫终16 分钟前
SpEl表达式之强大的集合选择(Collection Selection)和集合投影(Collection Projection)
java·spring boot·spring
ajassi200034 分钟前
开源 java android app 开发(十二)封库.aar
android·java·linux·开源
q5673152340 分钟前
Java使用Selenium反爬虫优化方案
java·开发语言·分布式·爬虫·selenium
kaikaile199544 分钟前
解密Spring Boot:深入理解条件装配与条件注解
java·spring boot·spring
守护者1701 小时前
JAVA学习-练习试用Java实现“一个词频统计工具 :读取文本文件,统计并输出每个单词的频率”
java·学习
bing_1581 小时前
Spring Boot 中ConditionalOnClass、ConditionalOnMissingBean 注解详解
java·spring boot·后端
ergdfhgerty1 小时前
斐讯N1部署Armbian与CasaOS实现远程存储管理
java·docker
勤奋的知更鸟1 小时前
Java性能测试工具列举
java·开发语言·测试工具