Skip to content

Commit

Permalink
'init'
Browse files Browse the repository at this point in the history
  • Loading branch information
hanguofeng committed Mar 29, 2014
1 parent bd5a743 commit b625de0
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
60 changes: 60 additions & 0 deletions confilter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package confilter

import (
"bytes"
"io/ioutil"

"github.com/cloudflare/ahocorasick"
)

type ConfilterServerConfig struct {
Dictionaries map[string]string
}

type ConfilterServer struct {
config *ConfilterServerConfig
matchers map[string]*ahocorasick.Matcher
}

func CreateConfilterServer(config *ConfilterServerConfig) (*ConfilterServer, error) {
server := new(ConfilterServer)
server.config = config
err := server.initMatchers()
if nil != err {
return nil, err
}
return server, nil
}

func (this *ConfilterServer) initMatchers() error {
matchers := make(map[string]*ahocorasick.Matcher)
for k, path := range this.config.Dictionaries {
dictContent, err := ioutil.ReadFile(path)
if nil != err {
return err
}
dictContentSplited := bytes.Split(dictContent, []byte("\n"))
matcher := ahocorasick.NewMatcher(dictContentSplited)
matchers[k] = matcher
}

this.matchers = matchers
return nil
}

func (this *ConfilterServer) Judge(s string) (matches []string) {
sbytes := []byte(s)
matches = this.JudgeBytes(sbytes)
return matches
}

func (this *ConfilterServer) JudgeBytes(s []byte) (matches []string) {
matches = []string{}
for k, matcher := range this.matchers {
hits := matcher.Match(s)
if len(hits) > 0 {
matches = append(matches, k)
}
}
return matches
}
49 changes: 49 additions & 0 deletions confilter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package confilter

import (
"testing"
)

func TestCreate(t *testing.T) {
_, err := createServer()
if nil != err {
t.Fatalf("create server error:%s", err)
t.Fail()
}

}

func TestJudge(t *testing.T) {
server, err := createServer()
if nil != err {
t.Fatalf("create server error:%s", err)
}
result := server.Judge("inaaa")
t.Logf("result:%s", result)
if len(result) < 1 {
t.Fatalf("result length <1")
t.Fail()
}
}

func BenchmarkJudge(b *testing.B) {
server, err := createServer()
if nil != err {
b.Fatalf("create server error:%s", err)
}
for i := 0; i < b.N; i++ {
server.Judge("inainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaainaaaaa")
}

}

func createServer() (*ConfilterServer, error) {
config := new(ConfilterServerConfig)
dicts := make(map[string]string)
dicts["aaa"] = "./data/aaa.txt"
dicts["bbb"] = "./data/bbb.txt"
config.Dictionaries = dicts

return CreateConfilterServer(config)

}
9 changes: 9 additions & 0 deletions data/aaa.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
��ǹ
�Ⱥ�
����
�
ϴ��
����
�ڶ�
����
inaaa
5 changes: 5 additions & 0 deletions data/bbb.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
��³����
������
�߿�ʱ��
ŵ������ƽ��
inbbb

0 comments on commit b625de0

Please sign in to comment.