拓展閱讀
sensitive-word-admin v1.3.0 發(fā)布 如何支持分布式部署?
sensitive-word-admin 敏感詞控臺(tái) v1.2.0 版本開(kāi)源
sensitive-word 基于 DFA 算法實(shí)現(xiàn)的高性能敏感詞工具介紹
更多技術(shù)交流
業(yè)務(wù)背景
對(duì)于英文單詞 Disburse 之類(lèi)的,其中的 sb 字母會(huì)被替換,要怎么處理,能不能只有整個(gè)單詞匹配的時(shí)候才替換。
針對(duì)匹配詞進(jìn)一步判斷
說(shuō)明
支持版本:v0.13.0
有時(shí)候我們可能希望對(duì)匹配的敏感詞進(jìn)一步限制,比如雖然我們定義了【av】作為敏感詞,但是不希望【have】被匹配。
就可以自定義實(shí)現(xiàn) wordResultCondition 接口,實(shí)現(xiàn)自己的策略。
系統(tǒng)內(nèi)置的策略在 WordResultConditions#alwaysTrue()
恒為真,WordResultConditions#englishWordMatch()
則要求英文必須全詞匹配。
入門(mén)例子
原始的默認(rèn)情況:
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());
當(dāng)然可以根據(jù)需要實(shí)現(xiàn)更加復(fù)雜的策略。
如何自定義自己的策略
可以參考 WordResultConditions#englishWordMatch()
實(shí)現(xiàn)類(lèi),只需要繼承 AbstractWordResultCondition 實(shí)現(xiàn)對(duì)應(yīng)的方法即可。
策略的定義
以 englishWordMatch 實(shí)現(xiàn)類(lèi)為例:
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();
// 判斷當(dāng)前是否為英文單詞
for(int i = startIndex; i < endIndex; i++) {
char c = text.charAt(i);
if(!CharUtil.isEnglish(c)) {
return true;
}
}
// 判斷處理,判斷前一個(gè)字符是否為英文。如果是,則不滿足
if(startIndex > 0) {
char preC = text.charAt(startIndex-1);
if(CharUtil.isEnglish(preC)) {
return false;
}
}
// 判斷后一個(gè)字符是否為英文
if(endIndex < text.length() - 1) {
char afterC = text.charAt(endIndex+1);
if(CharUtil.isEnglish(afterC)) {
return false;
}
}
return true;
}
}
策略的指定
然后用引導(dǎo)類(lèi)指定我們的策略即可:
List<String> wordList = SensitiveWordBs.newInstance()
.wordResultCondition(new WordResultConditionEnglishWordMatch())
.init()
.findAll(text);
小結(jié)
實(shí)際應(yīng)用的場(chǎng)景會(huì)被預(yù)想的復(fù)雜,所以此處設(shè)計(jì)為接口,內(nèi)置一些常見(jiàn)的實(shí)現(xiàn)策略。
同時(shí)支持用戶自定義拓展。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-828833.html
開(kāi)源代碼
https://github.com/houbb/sensitive-word 本文由博客一文多發(fā)平臺(tái) OpenWrite 發(fā)布!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-828833.html
到了這里,關(guān)于sensitive-word v0.13 特性版本發(fā)布 支持英文單詞全詞匹配的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!