網路遊戲作為一種多人互動遊戲,方便玩家在遊戲中交流是很重要的。
為了防止玩家在遊戲中說違禁的東西,所有的聊天系統都需要對聊天資訊進行過濾。
本文描述了一種簡單的基於dfa的演算法用於過濾聊天內容中的違禁詞,演算法的執行複雜度,遍歷
輸入字串n,最多對每個字元執行一次二分查詢lgn,所以最壞情況下也是o(nlgn).
因為編寫這段**的時間很短,所以**寫得有點醜陋,但已經足以描述其主要思想了:
#include #include#include
#include
#include
"wordfilter.h
"struct
token;
typedef
struct
wordfilter*wordfilter_t;
struct token *inserttoken(struct token *tok,char
c)
else
else
tmp[i] = tok->children[i];
}if(!flag)
tmp[tok->children_size] =child;
else
tmp[tok->children_size] = tok->children[tok->children_size-1
]; free(tok->children);
tok->children =tmp;
}tok->children_size++;
return
child;
}
static
struct token *getchild(struct token *tok,char
c) }
static
struct token *addchild(struct token *tok,char
c)static
void nextchar(struct token *tok,const
char *str,int i,int *maxmatch)
else
}
static uint8_t processword(wordfilter_t filter,const
char *str,int *pos)
else
else
}return0;
}wordfilter_t wordfilter_new(
const
char **forbidwords)
int j = 1
;
for(; j < size;++j)
tok =addchild(tok,str[j]);
tok->end = 1
; }
return
filter;
}
uint8_t isvaildword(wordfilter_t filter,
const
char *str)
} return
ret;
}string_t wordfiltrate(wordfilter_t filter,
const
char *str,char
replace)
}string_t ret =new_string(tmp);
//將連續的replace符號合成1個
int flag = 0
; j = 0
;
for(i = 0; i < size; ++i)
}else
}free(tmp);
((char*)to_cstr(ret))[j] = 0
;
return
ret;
}
通過Trie實現違禁詞過濾
敏感詞過濾 生活在 的 必須要有保持和諧的工具。根據 的規模不同選擇不同的技術方案 1.前期上乙個敏感詞過濾系統,發的文章只要命中敏感詞就不讓發。2.後期可以通過機器學習來自動識別一篇簡歷是否是正常簡歷,一篇正常簡歷的特徵還是很明顯的,通過訓練機器識別正常簡歷的語料,能讓機器自動判斷是否是違規資訊。...
敏感詞過濾
最近需要實現對聊天裡的敏感詞過濾,要求比較簡單,只需要對字型檔中存在的關鍵字進行匹配,所以不需要非常複雜的實現,但是需要能夠快速地對乙個關鍵字集合進行匹配。搜了一下相關的資料,比較簡單的乙個演算法是使用aho corasick演算法,以下簡稱ac演算法。該演算法的基本思想中包含了kmp演算法,即利用...
敏感詞過濾
敏感詞過濾 防沉迷,基本所有上線系統都會用的基礎功能吧,網上挺多 的,也很多種方式,看的有點繞寫了個自己感覺比較乾淨清楚的,結構也比較簡單,記錄一下insert 和 審查 檢查 都是遞迴,效率還好,win release版本5700的樣本,length 1000的 str 大概0.2ms支援比如敏感...