拓展浏览

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() 则要求英文必须全词匹配。

入门例子

原始的默认状况:

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());

咱们能够指定为英文必须全词匹配。

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 实现类为例:

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;    }}

策略的指定

而后用疏导类指定咱们的策略即可:

List<String> wordList = SensitiveWordBs.newInstance()        .wordResultCondition(new WordResultConditionEnglishWordMatch())        .init()        .findAll(text);

小结

理论利用的场景会被料想的简单,所以此处设计为接口,内置一些常见的实现策略。

同时反对用户自定义拓展。

开源代码

https://github.com/houbb/sensitive-word
本文由博客一文多发平台 OpenWrite 公布!