php聊天功能逻辑原理,@xxx提及用户,这种功能的实现原理是怎样的?
12
2014-12-13 22:05:54 +08:00 ? 1
在??途W http://www.nowcoder.com 用戶在我們網站提交的評論,我們會進行敏感詞過濾,其算法是基于有限狀態機DFA過濾的。 我覺得可以用敏感詞過濾的方法來查找@的用戶ID。 (用戶ID就是好多敏感詞), 具體的代碼如下:
/**
* 過濾敏感詞
*
* @param text
* @return
*/
public String filter(String text) {
if (StringUtils.isBlank(text)) {
return text;
}
String replacement = DEFAULT_REPLACEMENT;
StringBuilder result = new StringBuilder();
DFATreeNode tempNode = rootNode;
int begin = 0; // 回滾數
int position = 0; // 當前比較的位置
while (position < text.length()) {
char c = text.charAt(position);
// 空格直接跳過
if (isSymbol(c)) {
++position;
continue;
}
tempNode = tempNode.getSubNode(c);
// 當前位置的匹配結束
if (tempNode == null) {
// 以begin開始的字符串不存在敏感詞
result.append(text.charAt(begin));
// 跳到下一個字符開始測試
position = begin + 1;
begin = position;
// 回到樹初始節點
tempNode = rootNode;
} else if (tempNode.isKeywordEnd()) {
// 發現敏感詞, 從begin到position的位置用replacement替換掉
result.append(replacement);
position = position + 1;
begin = position;
tempNode = rootNode;
} else {
++position;
}
}
result.append(text.substring(begin));
return result.toString();
}
總結
以上是生活随笔為你收集整理的php聊天功能逻辑原理,@xxx提及用户,这种功能的实现原理是怎样的?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机两列布局,正方形
- 下一篇: PHP程序中时间戳,php 时间戳常用代