-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathflashtext_test.go
118 lines (103 loc) · 3.22 KB
/
flashtext_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package flashtext
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestAddKeywords(t *testing.T) {
var testSet = []struct{
in map[string]string
expected map[string]string
} {
{map[string]string{"teacher": "tea"}, map[string]string{"teacher": "tea"}},
{map[string]string{"student": "stu", "中国": "中文"}, map[string]string{"student": "stu", "中国": "中文"}},
}
// Add keywords from map
for _, testItem := range testSet {
keywordProcessor := NewKeywordProcessor()
keywordProcessor.AddKeywordsFromMap(testItem.in)
assert.Equal(t, testItem.expected, keywordProcessor.GetAllKeywords())
}
}
func TestAddKeywordsFromFile(t *testing.T) {
var testSet = []struct{
in string
expected map[string]string
} {
{"examples/data.txt", map[string]string{"abc": "abc", "中国": "中文",}},
}
// Extract keywords from sentence by searching TrieTree
for _, testItem := range testSet {
keywordProcessor := NewKeywordProcessor()
keywordProcessor.AddKeywordsFromFile(testItem.in)
assert.Equal(t, testItem.expected, keywordProcessor.GetAllKeywords())
}
}
func TestRemoveKeywordsFromList(t *testing.T) {
// add keywords from Map
keywordMap := map[string]string{
"teacher": "tea",
"student": "stu",
"中国": "中文",
}
var testSet = []struct{
in []string
expected map[string]string
} {
{[]string{"teacher"}, map[string]string{"student": "stu", "中国": "中文"}},
{[]string{"student", "teacher"},map[string]string{"中国": "中文"}},
}
// Remove keywords from list
for _, testItem := range testSet {
keywordProcessor := NewKeywordProcessor()
keywordProcessor.AddKeywordsFromMap(keywordMap)
keywordProcessor.RemoveKeywordFromList(testItem.in)
assert.Equal(t, testItem.expected, keywordProcessor.GetAllKeywords())
}
}
func TestRemoveKeyword(t *testing.T) {
// add keywords from Map
keywordMap := map[string]string{
"teacher": "tea",
"student": "stu",
"中国": "中文",
}
var testSet = []struct{
in string
expected map[string]string
} {
{"teacher", map[string]string{"student": "stu", "中国": "中文",}},
{"student", map[string]string{"teacher": "tea", "中国": "中文",}},
}
// remove a keyword
for _, testItem := range testSet {
keywordProcessor := NewKeywordProcessor()
keywordProcessor.AddKeywordsFromMap(keywordMap)
keywordProcessor.RemoveKeyword(testItem.in)
assert.Equal(t, testItem.expected, keywordProcessor.GetAllKeywords())
}
}
func TestExtractKeywords(t *testing.T) {
keywordProcessor := NewKeywordProcessor()
// add keywords from Map
keywordMap := map[string]string{
"teacher": "tea",
"student": "stu",
}
keywordProcessor.AddKeywordsFromMap(keywordMap)
// 添加中文关键词
keywordProcessor.AddKeyword("中文", "中文")
keywordProcessor.AddKeyword("abc")
var testSet = []struct{
in string
expected []string
} {
{"hello abc, what up", []string{"abc"}},
{"hello, 你会说中文吗?", []string{"中文"}},
{"hello, abc 你会说中文吗? oHabc", []string{"abc", "中文", "abc"}},
}
// Extract keywords from sentence by searching TrieTree
for _, testItem := range testSet {
cleanNameList := keywordProcessor.ExtractKeywords(testItem.in)
assert.Equal(t, testItem.expected, cleanNameList)
}
}