-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd5a743
commit b625de0
Showing
4 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
��ǹ | ||
�Ⱥ� | ||
���� | ||
� | ||
ϴ�� | ||
���� | ||
�ڶ� | ||
���� | ||
inaaa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
��³���� | ||
������ | ||
�߿�ʱ�� | ||
ŵ������ƽ�� | ||
inbbb |