-
Notifications
You must be signed in to change notification settings - Fork 0
/
jieba_tokenizer_test.go
158 lines (138 loc) · 3.67 KB
/
jieba_tokenizer_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package blugejieba
import (
"context"
"fmt"
"github.com/blugelabs/bluge/analysis"
"github.com/yanyiwu/gojieba"
"log"
"os"
"testing"
"github.com/blugelabs/bluge"
)
func TestBluge(t *testing.T) {
indexDir := ".gojieba.bluge"
config := bluge.DefaultConfig(indexDir)
// config.DefaultSearchAnalyzer = xxx
writer, err := bluge.OpenWriter(config)
if err != nil {
log.Fatalf("error opening writer: %v", err)
}
defer writer.Close()
doc := bluge.NewDocument("example_doc_id001").
AddField(bluge.NewTextField("name", "bluge"))
err = writer.Update(doc.ID(), doc)
if err != nil {
log.Fatalf("error updating document: %v", err)
}
reader, err := writer.Reader()
if err != nil {
log.Fatalf("error getting index reader: %v", err)
}
defer reader.Close()
query := bluge.NewMatchQuery("bluge").SetField("name")
request := bluge.NewTopNSearch(10, query).
WithStandardAggregations()
documentMatchIterator, err := reader.Search(context.Background(), request)
if err != nil {
log.Fatalf("error executing search: %v", err)
}
match, err := documentMatchIterator.Next()
for err == nil && match != nil {
err = match.VisitStoredFields(func(field string, value []byte) bool {
if field == "_id" {
fmt.Printf("match _id: %s\n", string(value))
}
return true
})
if err != nil {
log.Fatalf("error loading stored fields: %v", err)
}
match, err = documentMatchIterator.Next()
}
if err != nil {
log.Fatalf("error iterator document matches: %v", err)
}
}
func ExampleBlugeJieba() {
indexDir := ".gojieba.bluge"
messages := []struct {
Id string
Body string
}{
{
Id: "1",
Body: "你好",
},
{
Id: "2",
Body: "交代",
},
{
Id: "3",
Body: "长江大桥",
},
}
os.RemoveAll(indexDir)
// var ana bluge.Analyzer
ana := &analysis.Analyzer{
Tokenizer: NewJiebaTokenizer(gojieba.DICT_PATH, gojieba.HMM_PATH, gojieba.USER_DICT_PATH, gojieba.IDF_PATH, gojieba.STOP_WORDS_PATH),
}
config := bluge.DefaultConfig(indexDir)
// config.DefaultSearchAnalyzer = ana
writer, err := bluge.OpenWriter(config)
if err != nil {
log.Fatalf("error opening writer: %v", err)
}
defer writer.Close()
for _, msg := range messages {
doc := bluge.NewDocument(msg.Id).
AddField(bluge.NewTextField("name", msg.Body).WithAnalyzer(ana))
err = writer.Update(doc.ID(), doc)
if err != nil {
log.Fatalf("error updating document: %v", err)
}
}
// clean index when example finished
defer os.RemoveAll(indexDir)
querys := []string{
"你好世界",
"亲口交代",
"长江",
}
reader, err := writer.Reader()
if err != nil {
log.Fatalf("error getting index reader: %v", err)
}
defer reader.Close()
for _, q := range querys {
query := bluge.NewMatchQuery(q).SetField("name").SetAnalyzer(ana)
request := bluge.NewTopNSearch(10, query).
WithStandardAggregations()
documentMatchIterator, err := reader.Search(context.Background(), request)
if err != nil {
log.Fatalf("error executing search: %v", err)
}
match, err := documentMatchIterator.Next()
for err == nil && match != nil {
err = match.VisitStoredFields(func(field string, value []byte) bool {
if field == "_id" {
fmt.Printf("match _id: %s, _score: %v\n", string(value), match.Score)
}
return true
})
if err != nil {
log.Fatalf("error loading stored fields: %v", err)
}
match, err = documentMatchIterator.Next()
}
if err != nil {
log.Fatalf("error iterator document matches: %v", err)
}
}
// ana.(*analysis.Analyzer).Tokenizer.(*JiebaTokenizer).Free()
ana.Tokenizer.(*JiebaTokenizer).Free()
// Output:
// match _id: 1, _score: 0.6543330458293132
// match _id: 2, _score: 0.6543330458293132
// match _id: 3, _score: 0.4123194535362795
}