package com.leo.common.util;
import java.util.*;
public class SensitiveWordUtilCopy {
public static Map<String, Object> dictionaryMap = new HashMap<String, Object>();
/**
* @Description: 生成关键词字典库
* @Param:
* @Return:
* @Author leo
* @Date 2022/9/26 21:34
*/
public static void initMap(Collection<String> words) {
if (words == null) {
System.out.println("列表不能为空");
return;
}
Map<String, Object> map = new HashMap<>(words.size());
Map<String, Object> curMap = null;
Iterator<String> iterator = words.iterator();
while (iterator.hasNext()) {
String word = iterator.next();
// 地址值传递,同增同改
curMap = map;
int len = word.length();
for (int i = 0; i < len; i++) {
String key = String.valueOf(word.charAt(i)).toUpperCase();
Map<String, Object> wordMap = (Map<String, Object>) curMap.get(key);
if (wordMap == null) {
wordMap = new HashMap<>(2);
wordMap.put("isEnd", 0);
curMap.put(key, wordMap);
}
curMap = wordMap;
if (i == len - 1) {
curMap.put("isEnd", "1");
}
}
}
dictionaryMap = map;
}
/**
* @Description: 搜索文本中某个文字是否匹配关键词
* @Param:
* @Return:
* @Author leo
* @Date 2022/9/26 21:50
*/
public static int checkWord(String text, int beginIndex) {
if (dictionaryMap == null) {
throw new RuntimeException("字典不能为空");
}
boolean isEnd = false;
int wordLength = 0;
Map<String, Object> curMap = dictionaryMap;
int len = text.length();
for (int i = beginIndex; i < len; i++) {
String key = String.valueOf(text.charAt(i)).toUpperCase();
curMap = (Map<String, Object>) curMap.get(key);
if (curMap == null) {
break;
} else {
wordLength++;
if ("1".equals(curMap.get("isEnd"))) {
isEnd = true;
}
}
}
if (!isEnd) {
wordLength = 0;
}
return wordLength;
}
/**
* @Description: 获取匹配的关键词和命中次数
* @Param:
* @Return:
* @Author leo
* @Date 2022/9/26 21:59
*/
public static Map<String,Integer> matchWords(String text){
Map<String,Integer> wordMap = new HashMap<>();
text = text.toUpperCase();
int len = text.length();
for (int i = 0; i < len; i++) {
int wordLength = checkWord(text, i);
if (wordLength > 0) {
String word = text.substring(i, i + wordLength);
if (wordMap.containsKey(word)) {
wordMap.put(word,wordMap.get(word)+1);
}else {
wordMap.put(word,1);
}
i += wordLength -1;
}
}
return wordMap;
}
public static String getBestResult(Map<String,Integer> resultMap){
String bestRes = "";
Integer num = 0;
for (String k : resultMap.keySet()) {
if (k.length() > bestRes.length()) {
bestRes = k;
}else if (k.length() == bestRes.length()){
if (resultMap.get(k) > num) {
num= resultMap.get(k);
bestRes = k;
}
}
}
return bestRes;
}
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("冰");
list.add("a");
list.add("b");
System.out.println(list);
initMap(list);
String content = "冰的,冰,ABA,ABAA";
System.out.println(content);
Map<String,Integer> map = matchWords(content);
System.out.println(map);
System.out.println(getBestResult(map));
}
}
DFA实现步骤
最新推荐文章于 2024-09-25 10:59:43 发布