問(wèn)題發(fā)布功能的重點(diǎn)在于如何實(shí)現(xiàn)敏感詞過(guò)濾,基本的算法是前綴樹(shù)算法,前綴樹(shù)也就是字典樹(shù),通過(guò)前綴樹(shù)匹配可以加快敏感詞匹配的速度
下面是具體的代碼
- 前綴樹(shù)的結(jié)構(gòu)
其中包括有是否為結(jié)尾的標(biāo)志符end,當(dāng)在樹(shù)中找到這個(gè)位置時(shí),說(shuō)明找到了敏感詞,可以將其打碼設(shè)為**,從后面的字符繼續(xù)匹配
private class TrieNode {
private boolean end = false;
private Map<Character, TrieNode> map = new HashMap<Character, TrieNode>();
private Character c;
TrieNode() {
}
TrieNode(Character ch) {
c = ch;
}
public Character getch() {
return c;
}
public void setend(boolean a) {
this.end = a;
}
public boolean getend() {
return end;
}
public void addTrieNode(TrieNode tn) {
map.put(tn.getch(), tn);
}
public TrieNode getTrieNode(Character key) {
return map.get(key);
}
}
- 前綴樹(shù)的生成(從txt文件中讀取,每讀到一個(gè)詞語(yǔ)(一個(gè)詞語(yǔ)一行),利用addword來(lái)添加)
注意:這里該類implements InitializingBean其中的一個(gè)函數(shù)——public void afterPropertiesSet() throws Exception,這個(gè)函數(shù)表示在實(shí)例化bean之后,即Bean屬性注入之后,執(zhí)行的操作,我們通過(guò)這個(gè)函數(shù),在每次注入該服務(wù)時(shí),能夠初始化前綴樹(shù)
@Override
public void afterPropertiesSet() throws Exception {
rootNode = new TrieNode();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("C:\\Users\\erika\\Videos\\offer\\askme\\src\\main\\resources\\SensitiveWords.txt")),
"UTF-8"));
String lineTxt = null;
while ((lineTxt = br.readLine()) != null) {
addword(lineTxt.trim());
System.out.println(lineTxt);
}
br.close();
} catch (Exception e) {
logger.error("讀取文件失敗" + e);
System.out.println("111");
}
}
- 向前綴樹(shù)中添加詞語(yǔ)(每個(gè)字占一個(gè)節(jié)點(diǎn))
public void addword(String word) {
TrieNode tmp = rootNode;
for (int i = 0; i < word.length(); i++) {
if (isSymbol(word.charAt(i)))
continue;
if (tmp.getTrieNode(word.charAt(i)) != null) ;
else {
TrieNode tt = new TrieNode(word.charAt(i));
tmp.addTrieNode(tt);
}
tmp = tmp.getTrieNode(word.charAt(i));
if (i == word.length() - 1) {
tmp.setend(true);
}
}
}
- filter匹配字符串
public String filterword(String content) {
TrieNode root = rootNode;
TrieNode tmp = root;
StringBuilder result = new StringBuilder();
int head = 0;
int current = 0;
while (head < content.length()) {
Character c = content.charAt(head);
tmp = root;
if (isSymbol(c)) {
head++;
current++;
result.append(c);
continue;
}
boolean flag = false;
while (current < content.length() && tmp.getTrieNode(c) != null) {
if (tmp.getTrieNode(c).getend()) {
result.append("**");
flag = true;
break;
}
current++;
tmp = tmp.getTrieNode(c);
while(current<content.length() && isSymbol(content.charAt(current))) {
current++;
continue;
}
c = content.charAt(current);
}
if (flag) {
current++;
head = current;
} else {
result.append(content.charAt(head));
head++;
current = head;
}
}
return result.toString();
}
- 一個(gè)輔助函數(shù),用來(lái)判斷字符是否為空格,或者特殊字符
private boolean isSymbol(char c) {
int ic = (int) c;
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
//判斷是否為中文標(biāo)點(diǎn)
if (ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS
|| ub == Character.UnicodeBlock.VERTICAL_FORMS)
return true;
// 0x2E80-0x9FFF 東亞文字范圍
return Character.isSpaceChar(c)||(!CharUtils.isAsciiAlphanumeric(c) && (ic < 0x2E80 || ic > 0x9FFF));
}