Example #1
0
func whitePrefixWords(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		words := trie.WhitePrefixTrie().ReadAll()
		str := strings.Join(words, "\n")
		w.Header().Set("Server", "goo")
		w.Header().Set("Content-Type", "text/html; charset=utf-8")
		w.WriteHeader(200)
		w.Write([]byte(str))

	} else if r.Method == "POST" {
		resp := make(map[string]interface{})
		q := r.FormValue("q")
		op := r.FormValue("type")
		if op == "init" {
			trie.NewWhitePrefixTrie()
		}

		if q == "" {
			resp["code"] = 0
			resp["error"] = "参数q不能为空"
		} else {
			i := 0
			words := strings.Split(q, ",")
			for _, s := range words {
				trie.WhitePrefixTrie().Add(strings.Trim(s, " "))
				i++
			}

			resp["code"] = 1
			resp["mess"] = fmt.Sprintf("共添加了%d个白名称前缀词", i)
		}

		serveJson(w, resp)
	}
}
Example #2
0
// 从字典中装入敏感词库
func LoadDict() {
	trieHandle := trie.BlackTrie()
	load(trieHandle, "add", "./dictionary/black/default")
	load(trieHandle, "del", "./dictionary/black/exclude")

	trieHandle = trie.WhitePrefixTrie()
	load(trieHandle, "add", "./dictionary/white/prefix")

	trieHandle = trie.WhiteSuffixTrie()
	load(trieHandle, "add", "./dictionary/white/suffix")
}