Golang基于前缀树的敏感词过滤算法

一个简单的基于Golang的敏感词过滤算法
package mainimport ("fmt" "unicode/utf8")// 敏感词过滤type Trie struct {child map[rune]*Trie wordstring}// 插入func (trie *Trie) insert(word string) *Trie {cur := triefor _, v := range []rune(word) {if _, ok := cur.child[v]; !ok {newTrie := NewTrie()cur.child[v] = newTrie}cur = cur.child[v]}cur.word = wordreturn trie}// 过滤func (trie *Trie) filerKeyWords(word string) string {cur := triefor i, v := range []rune(word) {if _, ok := cur.child[v]; ok {cur = cur.child[v]if cur.word != "" {word = replaceStr(word, "*", i-utf8.RuneCountInString(cur.word)+1, i)cur = trie}} else {cur = trie}}return word}func replaceStr(word string, replace string, left, right int) string {str := "" for i, v := range []rune(word) {if i >= left && i <= right {str += replace} else {str += string(v)}}return str}func NewTrie() *Trie {return &Trie{word:"", child: make(map[rune]*Trie, 0), }}func main() {trie := NewTrie()}转自:
【Golang基于前缀树的敏感词过滤算法】参考:go语言中文文档:www.topoer.com


    推荐阅读