houbb / sensitive-word

👮‍♂️The sensitive word tool for java.(敏感词/违禁词/违法词/脏词。基于 DFA 算法实现的高性能 java 敏感词过滤工具框架。请勿发布涉及政治、广告、营销、翻墙、违反国家法律法规等内容。高性能敏感词检测过滤组件,附带繁体简体互换,支持全角半角互换,汉字转拼音,模糊搜索等功能。)
https://houbb.github.io/opensource/sensitive-word
Apache License 2.0
4.36k stars 589 forks source link

英文全匹配配置未生效 #69

Closed hangker1997 closed 2 months ago

hangker1997 commented 3 months ago

我的配置类是, 采用黑白名单的方式控制敏感词, 只用到了黑名单, 黑名单里 全是英文的, 开启了这个全匹配配置 后发现还是不生效, 比如 黑名单里有cp, 现在cpm是合法的,但还是被校验住了,不知道咋回事, 我用的版本是 0.14.0
@Bean public SensitiveWordBs sensitiveWordBs() { return SensitiveWordBs.newInstance() .wordAllow(WordAllows.chains(WordAllows.defaults(), myDdWordAllow)) .wordDeny(myDdWordDeny) //英文全匹配 .wordResultCondition(WordResultConditions.englishWordMatch()) // 各种其他配置 //不忽略全角和半角 中英文括号 .ignoreWidth(false) .init(); }

hangker1997 commented 3 months ago

已解决,问题原因是全匹配1位的话不好用 就像cp是敏感词,cpm还是会校验为敏感词,但是cp后面跟两位以上就不会被校验敏感词 我重写AbstractWordResultCondition的doMatch方法就解决了 import com.github.houbb.heaven.util.lang.CharUtil; 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; import com.github.houbb.sensitive.word.support.resultcondition.AbstractWordResultCondition;

public class EnglishWordMatch extends AbstractWordResultCondition {

@Override
protected boolean doMatch(IWordResult wordResult, String text, WordValidModeEnum modeEnum, IWordContext context) {
    final int startIndex = wordResult.startIndex();
    final int endIndex = wordResult.endIndex();

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

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

    // 判断当前是否为英文单词
    for(int i = startIndex; i < endIndex; i++) {
        char c = text.charAt(i);
        if(!CharUtil.isEnglish(c)) {
            return true;
        }
    }

    return true;
}

}

houbb commented 2 months ago

感谢提醒,v0.19.1 版本已修正。后续这种优化可以提 PR,我来统一合并。