Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["rune_trie_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = ["//app/service/main/antispam/util:go_default_library"],
)
go_library(
name = "go_default_library",
srcs = [
"rune_trie.go",
"trie.go",
],
importpath = "go-common/app/service/main/antispam/util/trie",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,120 @@
package trie
func (trie *RuneTrie) find(contents []rune, accu []rune) (string, interface{}) {
if trie.value != nil {
return string(accu), trie.value
}
if len(contents) == 0 {
return "", nil
}
k := contents[0]
tt, ok := trie.children[k]
if !ok {
return "", nil
}
return tt.find(contents[1:], append(accu, k))
}
func (trie *RuneTrie) Find(content string, accu string) (string, interface{}) {
rcontent := []rune(content)
if len(rcontent) == 0 {
return "", nil
}
k, v := trie.find(rcontent, []rune(accu))
if v == nil {
return trie.Find(string(rcontent[1:]), accu)
}
return k, v
}
/*
* the belowing codes come from "https://github.com/dghubble/trie"
*/
type RuneTrie struct {
value interface{}
children map[rune]*RuneTrie
}
func NewRuneTrie() *RuneTrie {
return &RuneTrie{
children: make(map[rune]*RuneTrie),
}
}
func (trie *RuneTrie) Get(key string) interface{} {
node := trie
for _, r := range key {
node = node.children[r]
if node == nil {
return nil
}
}
return node.value
}
func (trie *RuneTrie) Put(key string, value interface{}) bool {
node := trie
for _, r := range key {
child := node.children[r]
if child == nil {
child = NewRuneTrie()
node.children[r] = child
}
node = child
}
isNewVal := node.value == nil
node.value = value
return isNewVal
}
func (trie *RuneTrie) Delete(key string) bool {
path := make([]nodeRune, len(key))
node := trie
for i, r := range key {
path[i] = nodeRune{r: r, node: node}
node = node.children[r]
if node == nil {
return false
}
}
node.value = nil
if node.isLeaf() {
for i := len(key) - 1; i >= 0; i-- {
parent := path[i].node
r := path[i].r
delete(parent.children, r)
if parent.value != nil || !parent.isLeaf() {
break
}
}
}
return true
}
func (trie *RuneTrie) Walk(walker WalkFunc) error {
return trie.walk("", walker)
}
type nodeRune struct {
node *RuneTrie
r rune
}
func (trie *RuneTrie) walk(key string, walker WalkFunc) error {
if trie.value != nil {
walker(key, trie.value)
}
for r, child := range trie.children {
err := child.walk(key+string(r), walker)
if err != nil {
return err
}
}
return nil
}
func (trie *RuneTrie) isLeaf() bool {
return len(trie.children) == 0
}
type WalkFunc func(key string, value interface{}) error

View File

@@ -0,0 +1,108 @@
package trie
import (
"errors"
"math/rand"
"testing"
"time"
"go-common/app/service/main/antispam/util"
)
func TestRuneTrieAdd(t *testing.T) {
tr := NewRuneTrie()
tr.Put("jimmy", 1)
tr.Put("anny", 87)
tr.Put("xxxx", 23)
tr.Put("jim", 2)
if v := tr.Get("jimxx"); v != nil {
t.Errorf("expected nil, got %v", v)
}
if v := tr.Get("jimmy"); v.(int) != 1 {
t.Errorf("expected val, got %v", v)
}
if v := tr.Get("anny"); v.(int) != 87 {
t.Errorf("expected val, got %v", v)
}
if v := tr.Get("xxxx"); v.(int) != 23 {
t.Errorf("expected val, got %v", v)
}
if v := tr.Get("jim"); v.(int) != 2 {
t.Errorf("expected val, got %v", v)
}
}
func BenchmarkRuneTriePut(b *testing.B) {
rand.Seed(time.Now().UnixNano())
tr := NewRuneTrie()
for i := 0; i < b.N; i++ {
tr.Put(util.RandStr(10), 845)
}
}
func TestRuneTrieFind(t *testing.T) {
tr := NewRuneTrie()
tr.Put("我才是大佬", 2)
tr.Put("我才是大佬", 88)
tr.Put("mm", 1)
tr.Put("mmp", 2)
tr.Put("my name is jimmymmp", 2)
tr.Put("xxx", 88)
tr.Put("jimmy xxx, hhjhmmp", 2)
cases := []struct {
content string
expectKey string
expectValue int
}{
{
content: "mm",
expectKey: "mm",
expectValue: 1,
},
{
content: "m都xx发生地方范德萨发爱迪生刚发的否多少发生的否阿萨德否收到符文大师否xxxmy name is jimy, hhjhmp",
expectKey: "xxx",
expectValue: 88,
},
{
content: "m都mxx发生地方范德萨发爱迪生刚发的否多少发生的否阿萨德否收到符文大师否xxxmy name is jimy, hhjhmp",
expectKey: "xxx",
expectValue: 88,
},
{
content: "我才是大佬",
expectKey: "我才是大佬",
expectValue: 88,
},
}
for _, c := range cases {
t.Run("", func(t *testing.T) {
k, v := tr.Find(c.content, "")
if v == nil {
t.Fatal(errors.New("val is nil"))
}
if k != c.expectKey || v.(int) != c.expectValue {
t.Errorf("want key: %s, val:%v, got key:%s, val:%v", c.expectKey, c.expectValue, k, v)
}
})
}
}
//BenchmarkTrieListFind-4 50000 25347 ns/op
func BenchmarkRuneTrieFind(b *testing.B) {
rand.Seed(time.Now().UnixNano())
tr := NewRuneTrie()
for i := 0; i < b.N; i++ {
tr.Put(util.RandStr(10), i)
}
tr.Put("地方考虑saDFFDSALK", 8888)
tr.Put("都说了开发贷款", 7512)
for i := 0; i < b.N; i++ {
tr.Find("dfa啥都发生地方的施工费按发的噶是打发士大夫撒旦噶尔尕热狗怕的是结果来看砥节奉公来人速度感而过;sfdsfas fsadf asd fsad f asd都说了sdfs gfdgd jimmy开发速度来发噶都说了开发贷款时间范德萨了空间发的是 jimmy按时到路口发生撒地方考虑saDFFDSALKFDFASDFASDFSDFSADFRGEWTRETGERG", "")
}
}

View File

@@ -0,0 +1,11 @@
package trie
type Trier interface {
Put(key string, value interface{}) bool
Find(in string, acc string) (key string, value interface{})
Get(key string) interface{}
}
func New() Trier {
return NewRuneTrie()
}