Merge remote-tracking branch 'refs/remotes/origin/master' into dev
This commit is contained in:
@@ -1,257 +1,276 @@
|
|||||||
//package com.schisandra.share.application.sensitive;
|
package com.schisandra.share.application.sensitive;
|
||||||
//
|
|
||||||
//import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
//
|
|
||||||
//import com.schisandra.share.common.enums.IsDeletedFlagEnum;
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
//import lombok.extern.slf4j.Slf4j;
|
import com.mybatisflex.core.util.CollectionUtil;
|
||||||
//import org.springframework.util.CollectionUtils;
|
import com.schisandra.share.common.enums.IsDeletedFlagEnum;
|
||||||
//
|
import com.schisandra.share.infra.basic.dao.SchisandraShareSensitiveWordsDao;
|
||||||
//import java.util.*;
|
import com.schisandra.share.infra.basic.entity.SchisandraShareSensitiveWords;
|
||||||
//import java.util.concurrent.Executors;
|
import com.schisandra.share.infra.basic.entity.table.SchisandraShareSensitiveWordsTableDef;
|
||||||
//import java.util.concurrent.ScheduledExecutorService;
|
import com.schisandra.share.infra.basic.service.SchisandraShareSensitiveWordsService;
|
||||||
//import java.util.concurrent.TimeUnit;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
//
|
import org.springframework.util.CollectionUtils;
|
||||||
///**
|
|
||||||
// * 词库上下文环境
|
import javax.annotation.Resource;
|
||||||
// * <p>
|
import java.util.*;
|
||||||
// * 初始化敏感词库,将敏感词加入到HashMap中,构建DFA算法模型
|
import java.util.concurrent.Executors;
|
||||||
// *
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
// * @author minghu.zhang
|
import java.util.concurrent.TimeUnit;
|
||||||
// */
|
import java.util.stream.Collectors;
|
||||||
//@SuppressWarnings({"rawtypes", "unchecked"})
|
|
||||||
//@Slf4j
|
/**
|
||||||
//public class WordContext {
|
* 词库上下文环境
|
||||||
//
|
* <p>
|
||||||
// /**
|
* 初始化敏感词库,将敏感词加入到HashMap中,构建DFA算法模型
|
||||||
// * 敏感词字典
|
*
|
||||||
// */
|
* @author minghu.zhang
|
||||||
// private final Map wordMap = new HashMap(1024);
|
*/
|
||||||
//
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||||
// /**
|
@Slf4j
|
||||||
// * 是否已初始化
|
public class WordContext {
|
||||||
// */
|
|
||||||
// private boolean init;
|
/**
|
||||||
//
|
* 敏感词字典
|
||||||
// private long addLastId;
|
*/
|
||||||
//
|
private final Map wordMap = new HashMap(1024);
|
||||||
// public WordContext(boolean autoLoad, SensitiveWordsService service) {
|
|
||||||
// clearDelData(service);
|
/**
|
||||||
// Set<String> black = new HashSet<>();
|
* 是否已初始化
|
||||||
// Set<String> white = new HashSet<>();
|
*/
|
||||||
// List<SensitiveWords> list = service.list(Wrappers.<SensitiveWords>lambdaQuery().eq(SensitiveWords::getIsDeleted, IsDeletedFlagEnum.UN_DELETED.getCode()));
|
private boolean init;
|
||||||
// for (SensitiveWords words : list) {
|
|
||||||
// if (words.getType() == 1) {
|
private long addLastId;
|
||||||
// black.add(words.getWords());
|
|
||||||
// } else {
|
@Resource
|
||||||
// white.add(words.getWords());
|
private SchisandraShareSensitiveWordsDao schisandraShareSensitiveWordsDao;
|
||||||
// }
|
|
||||||
// }
|
public WordContext(boolean autoLoad) {
|
||||||
// if (CollectionUtils.isNotEmpty(list)) {
|
clearDelData();
|
||||||
// this.addLastId = list.get(list.size() - 1).getId();
|
Set<String> black = new HashSet<>();
|
||||||
// }
|
Set<String> white = new HashSet<>();
|
||||||
// initKeyWord(black, white);
|
|
||||||
// if (autoLoad) {
|
QueryWrapper query = new QueryWrapper();
|
||||||
// reloadWord(service);
|
query.where(SchisandraShareSensitiveWordsTableDef.SCHISANDRA_SHARE_SENSITIVE_WORDS.IS_DELETED.eq(IsDeletedFlagEnum.UN_DELETED.getCode()));
|
||||||
// }
|
List<SchisandraShareSensitiveWords> list = schisandraShareSensitiveWordsDao.selectListByQuery(query);
|
||||||
// }
|
|
||||||
//
|
for (SchisandraShareSensitiveWords words : list) {
|
||||||
// private void clearDelData(SensitiveWordsService service) {
|
if (words.getType() == 1) {
|
||||||
// LambdaUpdateWrapper<SensitiveWords> remove = Wrappers.<SensitiveWords>lambdaUpdate().eq(SensitiveWords::getIsDeleted, IsDeletedFlagEnum.DELETED.getCode());
|
black.add(words.getWords());
|
||||||
// service.remove(remove);
|
} else {
|
||||||
// }
|
white.add(words.getWords());
|
||||||
//
|
}
|
||||||
// private void reloadWord(SensitiveWordsService service) {
|
}
|
||||||
//
|
if (CollectionUtil.isNotEmpty(list)) {
|
||||||
// // 创建一个单线程的定时线程池
|
this.addLastId = Long.parseLong(list.get(list.size() - 1).getId());
|
||||||
// ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
}
|
||||||
// // 创建一个Runnable任务
|
initKeyWord(black, white);
|
||||||
// Runnable task = () -> {
|
if (autoLoad) {
|
||||||
// try {
|
reloadWord();
|
||||||
// addNewWords(service);
|
}
|
||||||
// removeDelWords(service);
|
}
|
||||||
// } catch (Exception e) {
|
|
||||||
// log.error("Sensitive words task error", e);
|
private void clearDelData() {
|
||||||
// }
|
QueryWrapper query = new QueryWrapper();
|
||||||
// };
|
query.where(SchisandraShareSensitiveWordsTableDef.SCHISANDRA_SHARE_SENSITIVE_WORDS.IS_DELETED.eq(IsDeletedFlagEnum.DELETED.getCode()));
|
||||||
// // 定时执行任务,初始延迟0,之后每分钟执行一次
|
List<SchisandraShareSensitiveWords> list = schisandraShareSensitiveWordsDao.selectListByQuery(query);
|
||||||
// scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.MINUTES);
|
schisandraShareSensitiveWordsDao.deleteBatchByIds(list.stream().map(SchisandraShareSensitiveWords::getId).collect(Collectors.toList()));
|
||||||
//
|
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// private void removeDelWords(SensitiveWordsService service) {
|
private void reloadWord() {
|
||||||
// LambdaUpdateWrapper<SensitiveWords> query = Wrappers.<SensitiveWords>lambdaUpdate()
|
|
||||||
// .eq(SensitiveWords::getIsDeleted, IsDeletedFlagEnum.DELETED.getCode());
|
// 创建一个单线程的定时线程池
|
||||||
// List<SensitiveWords> list = service.list(query);
|
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||||
// if (CollectionUtils.isNotEmpty(list)) {
|
// 创建一个Runnable任务
|
||||||
// log.info("removeDelWords {}", JSON.toJSON(list));
|
Runnable task = () -> {
|
||||||
// Set<String> black = new HashSet<>();
|
try {
|
||||||
// Set<String> white = new HashSet<>();
|
addNewWords();
|
||||||
// for (SensitiveWords words : list) {
|
removeDelWords();
|
||||||
// if (words.getType() == 1) {
|
} catch (Exception e) {
|
||||||
// black.add(words.getWords());
|
log.error("Sensitive words task error", e);
|
||||||
// } else {
|
}
|
||||||
// white.add(words.getWords());
|
};
|
||||||
// }
|
// 定时执行任务,初始延迟0,之后每分钟执行一次
|
||||||
// }
|
scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.MINUTES);
|
||||||
// removeWord(black, WordType.BLACK);
|
|
||||||
// removeWord(white, WordType.WHITE);
|
}
|
||||||
// }
|
|
||||||
// }
|
private void removeDelWords() {
|
||||||
//
|
QueryWrapper query = new QueryWrapper();
|
||||||
// private void addNewWords(SensitiveWordsService service) {
|
query.where(SchisandraShareSensitiveWordsTableDef.SCHISANDRA_SHARE_SENSITIVE_WORDS.IS_DELETED.eq(IsDeletedFlagEnum.DELETED.getCode()));
|
||||||
// LambdaUpdateWrapper<SensitiveWords> query = Wrappers.<SensitiveWords>lambdaUpdate()
|
List<SchisandraShareSensitiveWords> list = schisandraShareSensitiveWordsDao.selectListByQuery(query);
|
||||||
// .gt(SensitiveWords::getId, addLastId)
|
|
||||||
// .eq(SensitiveWords::getIsDeleted, IsDeletedFlagEnum.UN_DELETED.getCode());
|
if (CollectionUtil.isNotEmpty(list)) {
|
||||||
// List<SensitiveWords> list = service.list(query);
|
log.info("removeDelWords {}", JSON.toJSON(list));
|
||||||
// if (CollectionUtils.isNotEmpty(list)) {
|
Set<String> black = new HashSet<>();
|
||||||
// log.info("addNewWords {}", JSON.toJSON(list));
|
Set<String> white = new HashSet<>();
|
||||||
// this.addLastId = list.get(list.size() - 1).getId();
|
for (SchisandraShareSensitiveWords words : list) {
|
||||||
// Set<String> black = new HashSet<>();
|
if (words.getType() == 1) {
|
||||||
// Set<String> white = new HashSet<>();
|
black.add(words.getWords());
|
||||||
// for (SensitiveWords words : list) {
|
} else {
|
||||||
// if (words.getType() == 1) {
|
white.add(words.getWords());
|
||||||
// black.add(words.getWords());
|
}
|
||||||
// } else {
|
}
|
||||||
// white.add(words.getWords());
|
removeWord(black, WordType.BLACK);
|
||||||
// }
|
removeWord(white, WordType.WHITE);
|
||||||
// }
|
}
|
||||||
// addWord(black, WordType.BLACK);
|
}
|
||||||
// addWord(white, WordType.WHITE);
|
|
||||||
// }
|
private void addNewWords() {
|
||||||
// }
|
QueryWrapper query = new QueryWrapper();
|
||||||
//
|
query.where(SchisandraShareSensitiveWordsTableDef.SCHISANDRA_SHARE_SENSITIVE_WORDS.ID.gt(addLastId).and(SchisandraShareSensitiveWordsTableDef.SCHISANDRA_SHARE_SENSITIVE_WORDS.IS_DELETED.eq(IsDeletedFlagEnum.UN_DELETED.getCode())));
|
||||||
// /**
|
List<SchisandraShareSensitiveWords> list = schisandraShareSensitiveWordsDao.selectListByQuery(query);
|
||||||
// * 获取初始化的敏感词列表
|
|
||||||
// *
|
if (CollectionUtil.isNotEmpty(list)) {
|
||||||
// * @return 敏感词列表
|
log.info("addNewWords {}", JSON.toJSON(list));
|
||||||
// */
|
this.addLastId = Long.parseLong(list.get(list.size() - 1).getId());
|
||||||
// public Map getWordMap() {
|
Set<String> black = new HashSet<>();
|
||||||
// return wordMap;
|
Set<String> white = new HashSet<>();
|
||||||
// }
|
for (SchisandraShareSensitiveWords words : list) {
|
||||||
//
|
if (words.getType() == 1) {
|
||||||
// /**
|
black.add(words.getWords());
|
||||||
// * 初始化
|
} else {
|
||||||
// */
|
white.add(words.getWords());
|
||||||
// private synchronized void initKeyWord(Set<String> black, Set<String> white) {
|
}
|
||||||
// try {
|
}
|
||||||
// if (!init) {
|
addWord(black, WordType.BLACK);
|
||||||
// // 将敏感词库加入到HashMap中
|
addWord(white, WordType.WHITE);
|
||||||
// addWord(black, WordType.BLACK);
|
}
|
||||||
// // 将非敏感词库也加入到HashMap中
|
}
|
||||||
// addWord(white, WordType.WHITE);
|
|
||||||
// }
|
/**
|
||||||
// init = true;
|
* 获取初始化的敏感词列表
|
||||||
// } catch (Exception e) {
|
*
|
||||||
// throw new RuntimeException(e);
|
* @return 敏感词列表
|
||||||
// }
|
*/
|
||||||
// }
|
public Map getWordMap() {
|
||||||
//
|
return wordMap;
|
||||||
// /**
|
}
|
||||||
// * 读取敏感词库,将敏感词放入HashSet中,构建一个DFA算法模型:<br>
|
|
||||||
// * 中 = { isEnd = 0 国 = {<br>
|
/**
|
||||||
// * isEnd = 1 人 = {isEnd = 0 民 = {isEnd = 1} } 男 = { isEnd = 0 人 = { isEnd = 1 }
|
* 初始化
|
||||||
// * } } } 五 = { isEnd = 0 星 = { isEnd = 0 红 = { isEnd = 0 旗 = { isEnd = 1 } } } }
|
*/
|
||||||
// */
|
private synchronized void initKeyWord(Set<String> black, Set<String> white) {
|
||||||
// public void addWord(Collection<String> wordList, WordType wordType) {
|
try {
|
||||||
// if (CollectionUtils.isEmpty(wordList)) {
|
if (!init) {
|
||||||
// return;
|
// 将敏感词库加入到HashMap中
|
||||||
// }
|
addWord(black, WordType.BLACK);
|
||||||
// Map nowMap;
|
// 将非敏感词库也加入到HashMap中
|
||||||
// Map<String, String> newWorMap;
|
addWord(white, WordType.WHITE);
|
||||||
// // 迭代keyWordSet
|
}
|
||||||
// for (String key : wordList) {
|
init = true;
|
||||||
// nowMap = wordMap;
|
} catch (Exception e) {
|
||||||
// for (int i = 0; i < key.length(); i++) {
|
throw new RuntimeException(e);
|
||||||
// // 转换成char型
|
}
|
||||||
// char keyChar = key.charAt(i);
|
}
|
||||||
// // 获取
|
|
||||||
// Object wordMap = nowMap.get(keyChar);
|
/**
|
||||||
// // 如果存在该key,直接赋值
|
* 读取敏感词库,将敏感词放入HashSet中,构建一个DFA算法模型:<br>
|
||||||
// if (wordMap != null) {
|
* 中 = { isEnd = 0 国 = {<br>
|
||||||
// nowMap = (Map) wordMap;
|
* isEnd = 1 人 = {isEnd = 0 民 = {isEnd = 1} } 男 = { isEnd = 0 人 = { isEnd = 1 }
|
||||||
// } else {
|
* } } } 五 = { isEnd = 0 星 = { isEnd = 0 红 = { isEnd = 0 旗 = { isEnd = 1 } } } }
|
||||||
// // 不存在则构建一个map,同时将isEnd设置为0,因为他不是最后一个
|
*/
|
||||||
// newWorMap = new HashMap<>(4);
|
public void addWord(Collection<String> wordList, WordType wordType) {
|
||||||
// // 不是最后一个
|
if (CollectionUtils.isEmpty(wordList)) {
|
||||||
// newWorMap.put("isEnd", String.valueOf(EndType.HAS_NEXT.ordinal()));
|
return;
|
||||||
// nowMap.put(keyChar, newWorMap);
|
}
|
||||||
// nowMap = newWorMap;
|
Map nowMap;
|
||||||
// }
|
Map<String, String> newWorMap;
|
||||||
//
|
// 迭代keyWordSet
|
||||||
// if (i == key.length() - 1) {
|
for (String key : wordList) {
|
||||||
// // 最后一个
|
nowMap = wordMap;
|
||||||
// nowMap.put("isEnd", String.valueOf(EndType.IS_END.ordinal()));
|
for (int i = 0; i < key.length(); i++) {
|
||||||
// nowMap.put("isWhiteWord", String.valueOf(wordType.ordinal()));
|
// 转换成char型
|
||||||
// }
|
char keyChar = key.charAt(i);
|
||||||
// }
|
// 获取
|
||||||
// }
|
Object wordMap = nowMap.get(keyChar);
|
||||||
// }
|
// 如果存在该key,直接赋值
|
||||||
//
|
if (wordMap != null) {
|
||||||
// /**
|
nowMap = (Map) wordMap;
|
||||||
// * 在线删除敏感词
|
} else {
|
||||||
// *
|
// 不存在则构建一个map,同时将isEnd设置为0,因为他不是最后一个
|
||||||
// * @param wordList 敏感词列表
|
newWorMap = new HashMap<>(4);
|
||||||
// * @param wordType 黑名单 BLACk,白名单WHITE
|
// 不是最后一个
|
||||||
// */
|
newWorMap.put("isEnd", String.valueOf(EndType.HAS_NEXT.ordinal()));
|
||||||
// public void removeWord(Collection<String> wordList, WordType wordType) {
|
nowMap.put(keyChar, newWorMap);
|
||||||
// if (CollectionUtils.isEmpty(wordList)) {
|
nowMap = newWorMap;
|
||||||
// return;
|
}
|
||||||
// }
|
|
||||||
// Map nowMap;
|
if (i == key.length() - 1) {
|
||||||
// for (String key : wordList) {
|
// 最后一个
|
||||||
// List<Map> cacheList = new ArrayList<>();
|
nowMap.put("isEnd", String.valueOf(EndType.IS_END.ordinal()));
|
||||||
// nowMap = wordMap;
|
nowMap.put("isWhiteWord", String.valueOf(wordType.ordinal()));
|
||||||
// for (int i = 0; i < key.length(); i++) {
|
}
|
||||||
// char keyChar = key.charAt(i);
|
}
|
||||||
//
|
}
|
||||||
// Object map = nowMap.get(keyChar);
|
}
|
||||||
// if (map != null) {
|
|
||||||
// nowMap = (Map) map;
|
/**
|
||||||
// cacheList.add(nowMap);
|
* 在线删除敏感词
|
||||||
// } else {
|
*
|
||||||
// return;
|
* @param wordList 敏感词列表
|
||||||
// }
|
* @param wordType 黑名单 BLACk,白名单WHITE
|
||||||
//
|
*/
|
||||||
// if (i == key.length() - 1) {
|
public void removeWord(Collection<String> wordList, WordType wordType) {
|
||||||
// char[] keys = key.toCharArray();
|
if (CollectionUtils.isEmpty(wordList)) {
|
||||||
// boolean cleanable = false;
|
return;
|
||||||
// char lastChar = 0;
|
}
|
||||||
// for (int j = cacheList.size() - 1; j >= 0; j--) {
|
Map nowMap;
|
||||||
// Map cacheMap = cacheList.get(j);
|
for (String key : wordList) {
|
||||||
// if (j == cacheList.size() - 1) {
|
List<Map> cacheList = new ArrayList<>();
|
||||||
// if (String.valueOf(WordType.BLACK.ordinal()).equals(cacheMap.get("isWhiteWord"))) {
|
nowMap = wordMap;
|
||||||
// if (wordType == WordType.WHITE) {
|
for (int i = 0; i < key.length(); i++) {
|
||||||
// return;
|
char keyChar = key.charAt(i);
|
||||||
// }
|
|
||||||
// }
|
Object map = nowMap.get(keyChar);
|
||||||
// if (String.valueOf(WordType.WHITE.ordinal()).equals(cacheMap.get("isWhiteWord"))) {
|
if (map != null) {
|
||||||
// if (wordType == WordType.BLACK) {
|
nowMap = (Map) map;
|
||||||
// return;
|
cacheList.add(nowMap);
|
||||||
// }
|
} else {
|
||||||
// }
|
return;
|
||||||
// cacheMap.remove("isWhiteWord");
|
}
|
||||||
// cacheMap.remove("isEnd");
|
|
||||||
// if (cacheMap.size() == 0) {
|
if (i == key.length() - 1) {
|
||||||
// cleanable = true;
|
char[] keys = key.toCharArray();
|
||||||
// continue;
|
boolean cleanable = false;
|
||||||
// }
|
char lastChar = 0;
|
||||||
// }
|
for (int j = cacheList.size() - 1; j >= 0; j--) {
|
||||||
// if (cleanable) {
|
Map cacheMap = cacheList.get(j);
|
||||||
// Object isEnd = cacheMap.get("isEnd");
|
if (j == cacheList.size() - 1) {
|
||||||
// if (String.valueOf(EndType.IS_END.ordinal()).equals(isEnd)) {
|
if (String.valueOf(WordType.BLACK.ordinal()).equals(cacheMap.get("isWhiteWord"))) {
|
||||||
// cleanable = false;
|
if (wordType == WordType.WHITE) {
|
||||||
// }
|
return;
|
||||||
// cacheMap.remove(lastChar);
|
}
|
||||||
// }
|
}
|
||||||
// lastChar = keys[j];
|
if (String.valueOf(WordType.WHITE.ordinal()).equals(cacheMap.get("isWhiteWord"))) {
|
||||||
// }
|
if (wordType == WordType.BLACK) {
|
||||||
//
|
return;
|
||||||
// if (cleanable) {
|
}
|
||||||
// wordMap.remove(lastChar);
|
}
|
||||||
// }
|
cacheMap.remove("isWhiteWord");
|
||||||
// }
|
cacheMap.remove("isEnd");
|
||||||
// }
|
if (cacheMap.size() == 0) {
|
||||||
// }
|
cleanable = true;
|
||||||
// }
|
continue;
|
||||||
//
|
}
|
||||||
//}
|
}
|
||||||
|
if (cleanable) {
|
||||||
|
Object isEnd = cacheMap.get("isEnd");
|
||||||
|
if (String.valueOf(EndType.IS_END.ordinal()).equals(isEnd)) {
|
||||||
|
cleanable = false;
|
||||||
|
}
|
||||||
|
cacheMap.remove(lastChar);
|
||||||
|
}
|
||||||
|
lastChar = keys[j];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cleanable) {
|
||||||
|
wordMap.remove(lastChar);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@@ -1,222 +1,224 @@
|
|||||||
//package com.schisandra.share.application.sensitive;
|
package com.schisandra.share.application.sensitive;
|
||||||
//
|
|
||||||
//
|
|
||||||
//import java.util.ArrayList;
|
import com.mybatisflex.core.util.CollectionUtil;
|
||||||
//import java.util.List;
|
|
||||||
//import java.util.Map;
|
import java.util.ArrayList;
|
||||||
//import java.util.Objects;
|
import java.util.List;
|
||||||
//
|
import java.util.Map;
|
||||||
///**
|
import java.util.Objects;
|
||||||
// * 敏感词过滤器
|
|
||||||
// *
|
/**
|
||||||
// * @author minghu.zhang
|
* 敏感词过滤器
|
||||||
// */
|
*
|
||||||
//@SuppressWarnings("rawtypes")
|
* @author minghu.zhang
|
||||||
//public class WordFilter {
|
*/
|
||||||
//
|
@SuppressWarnings("rawtypes")
|
||||||
// /**
|
public class WordFilter {
|
||||||
// * 敏感词表
|
|
||||||
// */
|
/**
|
||||||
// private final Map wordMap;
|
* 敏感词表
|
||||||
//
|
*/
|
||||||
// /**
|
private final Map wordMap;
|
||||||
// * 构造函数
|
|
||||||
// */
|
/**
|
||||||
// public WordFilter(WordContext context) {
|
* 构造函数
|
||||||
// this.wordMap = context.getWordMap();
|
*/
|
||||||
// }
|
public WordFilter(WordContext context) {
|
||||||
//
|
this.wordMap = context.getWordMap();
|
||||||
// /**
|
}
|
||||||
// * 替换敏感词
|
|
||||||
// *
|
/**
|
||||||
// * @param text 输入文本
|
* 替换敏感词
|
||||||
// */
|
*
|
||||||
// public String replace(final String text) {
|
* @param text 输入文本
|
||||||
// return replace(text, 0, '*');
|
*/
|
||||||
// }
|
public String replace(final String text) {
|
||||||
//
|
return replace(text, 0, '*');
|
||||||
// /**
|
}
|
||||||
// * 替换敏感词
|
|
||||||
// *
|
/**
|
||||||
// * @param text 输入文本
|
* 替换敏感词
|
||||||
// * @param symbol 替换符号
|
*
|
||||||
// */
|
* @param text 输入文本
|
||||||
// public String replace(final String text, final char symbol) {
|
* @param symbol 替换符号
|
||||||
// return replace(text, 0, symbol);
|
*/
|
||||||
// }
|
public String replace(final String text, final char symbol) {
|
||||||
//
|
return replace(text, 0, symbol);
|
||||||
// /**
|
}
|
||||||
// * 替换敏感词
|
|
||||||
// *
|
/**
|
||||||
// * @param text 输入文本
|
* 替换敏感词
|
||||||
// * @param skip 文本距离
|
*
|
||||||
// * @param symbol 替换符号
|
* @param text 输入文本
|
||||||
// */
|
* @param skip 文本距离
|
||||||
// public String replace(final String text, final int skip, final char symbol) {
|
* @param symbol 替换符号
|
||||||
// char[] charset = text.toCharArray();
|
*/
|
||||||
// for (int i = 0; i < charset.length; i++) {
|
public String replace(final String text, final int skip, final char symbol) {
|
||||||
// FlagIndex fi = getFlagIndex(charset, i, skip);
|
char[] charset = text.toCharArray();
|
||||||
// if (fi.isFlag()) {
|
for (int i = 0; i < charset.length; i++) {
|
||||||
// if (!fi.isWhiteWord()) {
|
FlagIndex fi = getFlagIndex(charset, i, skip);
|
||||||
// for (int j : fi.getIndex()) {
|
if (fi.isFlag()) {
|
||||||
// charset[j] = symbol;
|
if (!fi.isWhiteWord()) {
|
||||||
// }
|
for (int j : fi.getIndex()) {
|
||||||
// } else {
|
charset[j] = symbol;
|
||||||
// i += fi.getIndex().size() - 1;
|
}
|
||||||
// }
|
} else {
|
||||||
// }
|
i += fi.getIndex().size() - 1;
|
||||||
// }
|
}
|
||||||
// return new String(charset);
|
}
|
||||||
// }
|
}
|
||||||
//
|
return new String(charset);
|
||||||
// /**
|
}
|
||||||
// * 是否包含敏感词
|
|
||||||
// *
|
/**
|
||||||
// * @param text 输入文本
|
* 是否包含敏感词
|
||||||
// */
|
*
|
||||||
// public boolean include(final String text) {
|
* @param text 输入文本
|
||||||
// return include(text, 0);
|
*/
|
||||||
// }
|
public boolean include(final String text) {
|
||||||
//
|
return include(text, 0);
|
||||||
// /**
|
}
|
||||||
// * 是否包含敏感词
|
|
||||||
// *
|
/**
|
||||||
// * @param text 输入文本
|
* 是否包含敏感词
|
||||||
// * @param skip 文本距离
|
*
|
||||||
// */
|
* @param text 输入文本
|
||||||
// public boolean include(final String text, final int skip) {
|
* @param skip 文本距离
|
||||||
// boolean include = false;
|
*/
|
||||||
// char[] charset = text.toCharArray();
|
public boolean include(final String text, final int skip) {
|
||||||
// for (int i = 0; i < charset.length; i++) {
|
boolean include = false;
|
||||||
// FlagIndex fi = getFlagIndex(charset, i, skip);
|
char[] charset = text.toCharArray();
|
||||||
// if (fi.isFlag()) {
|
for (int i = 0; i < charset.length; i++) {
|
||||||
// if (fi.isWhiteWord()) {
|
FlagIndex fi = getFlagIndex(charset, i, skip);
|
||||||
// i += fi.getIndex().size() - 1;
|
if (fi.isFlag()) {
|
||||||
// } else {
|
if (fi.isWhiteWord()) {
|
||||||
// include = true;
|
i += fi.getIndex().size() - 1;
|
||||||
// break;
|
} else {
|
||||||
// }
|
include = true;
|
||||||
// }
|
break;
|
||||||
// }
|
}
|
||||||
// return include;
|
}
|
||||||
// }
|
}
|
||||||
//
|
return include;
|
||||||
// /**
|
}
|
||||||
// * 获取敏感词数量
|
|
||||||
// *
|
/**
|
||||||
// * @param text 输入文本
|
* 获取敏感词数量
|
||||||
// */
|
*
|
||||||
// public int wordCount(final String text) {
|
* @param text 输入文本
|
||||||
// return wordCount(text, 0);
|
*/
|
||||||
// }
|
public int wordCount(final String text) {
|
||||||
//
|
return wordCount(text, 0);
|
||||||
// /**
|
}
|
||||||
// * 获取敏感词数量
|
|
||||||
// *
|
/**
|
||||||
// * @param text 输入文本
|
* 获取敏感词数量
|
||||||
// * @param skip 文本距离
|
*
|
||||||
// */
|
* @param text 输入文本
|
||||||
// public int wordCount(final String text, final int skip) {
|
* @param skip 文本距离
|
||||||
// int count = 0;
|
*/
|
||||||
// char[] charset = text.toCharArray();
|
public int wordCount(final String text, final int skip) {
|
||||||
// for (int i = 0; i < charset.length; i++) {
|
int count = 0;
|
||||||
// FlagIndex fi = getFlagIndex(charset, i, skip);
|
char[] charset = text.toCharArray();
|
||||||
// if (fi.isFlag()) {
|
for (int i = 0; i < charset.length; i++) {
|
||||||
// if (fi.isWhiteWord()) {
|
FlagIndex fi = getFlagIndex(charset, i, skip);
|
||||||
// i += fi.getIndex().size() - 1;
|
if (fi.isFlag()) {
|
||||||
// } else {
|
if (fi.isWhiteWord()) {
|
||||||
// count++;
|
i += fi.getIndex().size() - 1;
|
||||||
// }
|
} else {
|
||||||
// }
|
count++;
|
||||||
// }
|
}
|
||||||
// return count;
|
}
|
||||||
// }
|
}
|
||||||
//
|
return count;
|
||||||
// public void check(final String text) {
|
}
|
||||||
// List<String> wordList = wordList(text);
|
|
||||||
// if (CollectionUtils.isNotEmpty(wordList)) {
|
public void check(final String text) {
|
||||||
// throw new IllegalArgumentException(String.format("内容包含敏感词 【%s】", String.join("、", wordList)));
|
List<String> wordList = wordList(text);
|
||||||
// }
|
if (CollectionUtil.isNotEmpty(wordList)) {
|
||||||
// }
|
throw new IllegalArgumentException(String.format("内容包含敏感词 【%s】", String.join("、", wordList)));
|
||||||
//
|
}
|
||||||
// /**
|
}
|
||||||
// * 获取敏感词列表
|
|
||||||
// *
|
/**
|
||||||
// * @param text 输入文本
|
* 获取敏感词列表
|
||||||
// */
|
*
|
||||||
// public List<String> wordList(final String text) {
|
* @param text 输入文本
|
||||||
// return wordList(text, 0);
|
*/
|
||||||
// }
|
public List<String> wordList(final String text) {
|
||||||
//
|
return wordList(text, 0);
|
||||||
// /**
|
}
|
||||||
// * 获取敏感词列表
|
|
||||||
// *
|
/**
|
||||||
// * @param text 输入文本
|
* 获取敏感词列表
|
||||||
// * @param skip 文本距离
|
*
|
||||||
// */
|
* @param text 输入文本
|
||||||
// public List<String> wordList(final String text, final int skip) {
|
* @param skip 文本距离
|
||||||
// List<String> wordList = new ArrayList<>();
|
*/
|
||||||
// char[] charset = text.toCharArray();
|
public List<String> wordList(final String text, final int skip) {
|
||||||
// for (int i = 0; i < charset.length; i++) {
|
List<String> wordList = new ArrayList<>();
|
||||||
// FlagIndex fi = getFlagIndex(charset, i, skip);
|
char[] charset = text.toCharArray();
|
||||||
// if (fi.isFlag()) {
|
for (int i = 0; i < charset.length; i++) {
|
||||||
// if (fi.isWhiteWord()) {
|
FlagIndex fi = getFlagIndex(charset, i, skip);
|
||||||
// i += fi.getIndex().size() - 1;
|
if (fi.isFlag()) {
|
||||||
// } else {
|
if (fi.isWhiteWord()) {
|
||||||
// StringBuilder builder = new StringBuilder();
|
i += fi.getIndex().size() - 1;
|
||||||
// for (int j : fi.getIndex()) {
|
} else {
|
||||||
// char word = text.charAt(j);
|
StringBuilder builder = new StringBuilder();
|
||||||
// builder.append(word);
|
for (int j : fi.getIndex()) {
|
||||||
// }
|
char word = text.charAt(j);
|
||||||
// wordList.add(builder.toString());
|
builder.append(word);
|
||||||
// }
|
}
|
||||||
// }
|
wordList.add(builder.toString());
|
||||||
// }
|
}
|
||||||
// return wordList;
|
}
|
||||||
// }
|
}
|
||||||
//
|
return wordList;
|
||||||
// /**
|
}
|
||||||
// * 获取标记索引
|
|
||||||
// *
|
/**
|
||||||
// * @param charset 输入文本
|
* 获取标记索引
|
||||||
// * @param begin 检测起始
|
*
|
||||||
// * @param skip 文本距离
|
* @param charset 输入文本
|
||||||
// */
|
* @param begin 检测起始
|
||||||
// private FlagIndex getFlagIndex(final char[] charset, final int begin, final int skip) {
|
* @param skip 文本距离
|
||||||
// FlagIndex fi = new FlagIndex();
|
*/
|
||||||
//
|
private FlagIndex getFlagIndex(final char[] charset, final int begin, final int skip) {
|
||||||
// Map current = wordMap;
|
FlagIndex fi = new FlagIndex();
|
||||||
// boolean flag = false;
|
|
||||||
// int count = 0;
|
Map current = wordMap;
|
||||||
// List<Integer> index = new ArrayList<>();
|
boolean flag = false;
|
||||||
// for (int i = begin; i < charset.length; i++) {
|
int count = 0;
|
||||||
// char word = charset[i];
|
List<Integer> index = new ArrayList<>();
|
||||||
// Map mapTree = (Map) current.get(word);
|
for (int i = begin; i < charset.length; i++) {
|
||||||
// if (count > skip || (i == begin && Objects.isNull(mapTree))) {
|
char word = charset[i];
|
||||||
// break;
|
Map mapTree = (Map) current.get(word);
|
||||||
// }
|
if (count > skip || (i == begin && Objects.isNull(mapTree))) {
|
||||||
// if (Objects.nonNull(mapTree)) {
|
break;
|
||||||
// current = mapTree;
|
}
|
||||||
// count = 0;
|
if (Objects.nonNull(mapTree)) {
|
||||||
// index.add(i);
|
current = mapTree;
|
||||||
// } else {
|
count = 0;
|
||||||
// count++;
|
index.add(i);
|
||||||
// if (flag && count > skip) {
|
} else {
|
||||||
// break;
|
count++;
|
||||||
// }
|
if (flag && count > skip) {
|
||||||
// }
|
break;
|
||||||
// if ("1".equals(current.get("isEnd"))) {
|
}
|
||||||
// flag = true;
|
}
|
||||||
// }
|
if ("1".equals(current.get("isEnd"))) {
|
||||||
// if ("1".equals(current.get("isWhiteWord"))) {
|
flag = true;
|
||||||
// fi.setWhiteWord(true);
|
}
|
||||||
// break;
|
if ("1".equals(current.get("isWhiteWord"))) {
|
||||||
// }
|
fi.setWhiteWord(true);
|
||||||
// }
|
break;
|
||||||
//
|
}
|
||||||
// fi.setFlag(flag);
|
}
|
||||||
// fi.setIndex(index);
|
|
||||||
//
|
fi.setFlag(flag);
|
||||||
// return fi;
|
fi.setIndex(index);
|
||||||
// }
|
|
||||||
//}
|
return fi;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user