mirror of
https://github.com/houbb/sensitive-word.git
synced 2026-03-22 08:27:36 +08:00
add maxFirst
This commit is contained in:
@@ -413,3 +413,10 @@
|
||||
|:---|:-----|----------------------|:-------------------|:-----|
|
||||
| 1 | A | wordCheck 策略支持用户自定义 | 2025-2-17 12:06:45 | https://github.com/houbb/sensitive-word/issues/101 |
|
||||
| 2 | A | wordCheckUrlNoPrefix | 2025-2-17 12:06:45 | https://github.com/houbb/sensitive-word/issues/101 |
|
||||
|
||||
# release_0.25.0
|
||||
|
||||
| 序号 | 变更类型 | 说明 | 时间 | 备注 |
|
||||
|:---|:-----|----------------------|:-------------------|:-----|
|
||||
| 1 | A | wordCheck 策略支持用户自定义 | 2025-2-17 12:06:45 | https://github.com/houbb/sensitive-word/issues/101 |
|
||||
| 2 | A | wordCheckUrlNoPrefix | 2025-2-17 12:06:45 | https://github.com/houbb/sensitive-word/issues/101 |
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.github.houbb.sensitive.word.support.check;
|
||||
|
||||
import com.github.houbb.heaven.annotation.ThreadSafe;
|
||||
import com.github.houbb.sensitive.word.api.ISensitiveWordCharIgnore;
|
||||
import com.github.houbb.sensitive.word.api.IWordCheck;
|
||||
import com.github.houbb.sensitive.word.api.IWordContext;
|
||||
import com.github.houbb.sensitive.word.api.IWordData;
|
||||
import com.github.houbb.sensitive.word.api.context.InnerSensitiveWordContext;
|
||||
import com.github.houbb.sensitive.word.constant.enums.WordContainsTypeEnum;
|
||||
import com.github.houbb.sensitive.word.constant.enums.WordTypeEnum;
|
||||
import com.github.houbb.sensitive.word.support.result.WordLengthResult;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 敏感词监测实现
|
||||
* @author binbin.hou
|
||||
* @since 0.26.0
|
||||
*/
|
||||
@Deprecated
|
||||
public class WordCheckWordMaxLen extends AbstractWordCheck {
|
||||
|
||||
@Override
|
||||
protected Class<? extends IWordCheck> getSensitiveCheckClass() {
|
||||
return WordCheckWordMaxLen.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WordLengthResult getActualLength(int beginIndex, InnerSensitiveWordContext innerContext) {
|
||||
final String txt = innerContext.originalText();
|
||||
final Map<Character, Character> formatCharMapping = innerContext.formatCharMapping();
|
||||
final IWordContext context = innerContext.wordContext();
|
||||
final IWordData wordData = context.wordData();
|
||||
final IWordData wordDataAllow = context.wordDataAllow();
|
||||
final ISensitiveWordCharIgnore wordCharIgnore = context.charIgnore();
|
||||
|
||||
// 前一个条件
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
char[] rawChars = txt.toCharArray();
|
||||
|
||||
int tempLen = 0;
|
||||
int maxWhite = 0;
|
||||
int maxBlack = 0;
|
||||
boolean firstCheck = true;
|
||||
|
||||
WordContainsTypeEnum wordContainsTypeEnumAllow = wordDataAllow.contains(stringBuilder, innerContext);
|
||||
WordContainsTypeEnum wordContainsTypeEnumDeny = wordData.contains(stringBuilder, innerContext);
|
||||
|
||||
for (int i = beginIndex; i < rawChars.length; i++) {
|
||||
if (wordCharIgnore.ignore(i, rawChars, innerContext) && tempLen != 0) {
|
||||
tempLen++;
|
||||
continue;
|
||||
}
|
||||
|
||||
char mappingChar = formatCharMapping.get(rawChars[i]);
|
||||
stringBuilder.append(mappingChar);
|
||||
tempLen++;
|
||||
|
||||
if (firstCheck || !WordContainsTypeEnum.NOT_FOUND.equals(wordContainsTypeEnumAllow)) {
|
||||
wordContainsTypeEnumAllow = wordDataAllow.contains(stringBuilder, innerContext);
|
||||
if (WordContainsTypeEnum.CONTAINS_END.equals(wordContainsTypeEnumAllow)) {
|
||||
maxWhite += tempLen;
|
||||
wordContainsTypeEnumAllow = WordContainsTypeEnum.NOT_FOUND;
|
||||
}
|
||||
}
|
||||
|
||||
// 黑名单命中
|
||||
if (firstCheck || !WordContainsTypeEnum.NOT_FOUND.equals(wordContainsTypeEnumDeny)) {
|
||||
wordContainsTypeEnumDeny = wordData.contains(stringBuilder, innerContext);
|
||||
if (WordContainsTypeEnum.CONTAINS_END.equals(wordContainsTypeEnumDeny)) {
|
||||
maxBlack += tempLen;
|
||||
wordContainsTypeEnumDeny = WordContainsTypeEnum.NOT_FOUND;
|
||||
}
|
||||
}
|
||||
|
||||
// 不再是第一次检测
|
||||
firstCheck = false;
|
||||
|
||||
// 黑白名单都未匹配
|
||||
if (WordContainsTypeEnum.NOT_FOUND.equals(wordContainsTypeEnumAllow) &&
|
||||
WordContainsTypeEnum.NOT_FOUND.equals(wordContainsTypeEnumDeny)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return WordLengthResult.newInstance()
|
||||
.wordAllowLen(maxWhite)
|
||||
.wordDenyLen(maxBlack);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getType() {
|
||||
return WordTypeEnum.WORD.getCode();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.github.houbb.sensitive.word.bs;
|
||||
|
||||
import com.github.houbb.sensitive.word.api.IWordDeny;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class SensitiveWordMaxFirstTest {
|
||||
|
||||
@Test
|
||||
public void maxFirstTest() {
|
||||
SensitiveWordBs bs = SensitiveWordBs.newInstance()
|
||||
.wordDeny(new IWordDeny() {
|
||||
@Override
|
||||
public List<String> deny() {
|
||||
return Arrays.asList("我的世界", "我的");
|
||||
}
|
||||
}).init();
|
||||
|
||||
String text = "我的世界我的好玩";
|
||||
|
||||
List<String> textList = bs.findAll(text);
|
||||
// Assert.assertEquals("", textList.toString());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user