forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fts.go
246 lines (224 loc) · 7.29 KB
/
fts.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package tok
import (
"gopkg.in/adibiarsotp/dgraph.v83/x"
"github.com/blevesearch/bleve/analysis/analyzer/custom"
"github.com/blevesearch/bleve/analysis/token/lowercase"
"github.com/blevesearch/bleve/analysis/token/porter"
"github.com/blevesearch/bleve/analysis/token/stop"
"github.com/blevesearch/bleve/analysis/token/unicodenorm"
"github.com/blevesearch/bleve/analysis/tokenizer/unicode"
"github.com/blevesearch/bleve/analysis/tokenmap"
"github.com/blevesearch/bleve/registry"
"github.com/blevesearch/blevex/stemmer"
"github.com/tebeka/snowball"
)
var (
bleveCache *registry.Cache
langToCode map[string]string // maps language name to country code
)
const (
FTSTokenizerName = "fulltext"
normalizerFormNFKC = "nfkc"
normalizerName = "nfkc_normalizer"
)
func initFullTextTokenizers() {
bleveCache = registry.NewCache()
defineNormalizer()
defineTermAnalyzer()
// List of supported languages (as defined in https://github.com/tebeka/snowball):
// danish, dutch, english, finnish, french, german, hungarian, italian, norwegian, porter,
// portuguese, romanian, russian, spanish, swedish, turkish
for _, lang := range snowball.LangList() {
if lang == "porter" {
continue
}
defineStemmer(lang)
defineStopWordsList(lang)
defineAnalyzer(lang)
RegisterTokenizer(&FullTextTokenizer{Lang: countryCode(lang)})
}
// Default full text tokenizer, with Porter stemmer (it works with English only).
defineDefaultFullTextAnalyzer()
RegisterTokenizer(FullTextTokenizer{})
}
// Create normalizer using Normalization Form KC (NFKC) - Compatibility Decomposition, followed
// by Canonical Composition. See: http://unicode.org/reports/tr15/#Norm_Forms
func defineNormalizer() {
_, err := bleveCache.DefineTokenFilter(normalizerName, map[string]interface{}{
"type": unicodenorm.Name,
"form": normalizerFormNFKC,
})
x.Check(err)
}
func defineStemmer(lang string) {
_, err := bleveCache.DefineTokenFilter(stemmerName(countryCode(lang)), map[string]interface{}{
"type": stemmer.Name,
"lang": lang,
})
x.Check(err)
}
func defineStopWordsList(lang string) {
name := stopWordsListName(countryCode(lang))
_, err := bleveCache.DefineTokenMap(name, map[string]interface{}{
"type": tokenmap.Name,
"tokens": stopwords[lang],
})
x.Check(err)
_, err = bleveCache.DefineTokenFilter(name, map[string]interface{}{
"type": stop.Name,
"stop_token_map": name,
})
x.Check(err)
}
// basic analyzer - splits on word boundaries, lowercase and normalize tokens
func defineTermAnalyzer() {
_, err := bleveCache.DefineAnalyzer("term", map[string]interface{}{
"type": custom.Name,
"tokenizer": unicode.Name,
"token_filters": []string{lowercase.Name, normalizerName},
})
x.Check(err)
}
// default full text search analyzer - does english stop-words removal and Porter stemming
func defineDefaultFullTextAnalyzer() {
_, err := bleveCache.DefineAnalyzer(FTSTokenizerName, map[string]interface{}{
"type": custom.Name,
"tokenizer": unicode.Name,
"token_filters": []string{
lowercase.Name,
normalizerName,
stopWordsListName("en"),
porter.Name},
})
x.Check(err)
}
// full text search analyzer - does language-specific stop-words removal and stemming
func defineAnalyzer(lang string) {
ln := countryCode(lang)
_, err := bleveCache.DefineAnalyzer(FtsTokenizerName(ln), map[string]interface{}{
"type": custom.Name,
"tokenizer": unicode.Name,
"token_filters": []string{
lowercase.Name,
normalizerName,
stopWordsListName(ln),
stemmerName(ln),
},
})
x.Check(err)
}
func FtsTokenizerName(lang string) string {
return FTSTokenizerName + lang
}
func stemmerName(lang string) string {
return stemmer.Name + lang
}
func stopWordsListName(lang string) string {
return stop.Name + lang
}
func countryCode(lang string) string {
code, ok := langToCode[lang]
x.AssertTruef(ok, "Unsupported language: %s", lang)
return code
}
func init() {
// List based on https://godoc.org/golang.org/x/text/language#Tag
// It contains more languages than supported by Bleve, to enable seamless addition of new langs.
langToCode = map[string]string{
"afrikaans": "af",
"amharic": "am",
"arabic": "ar",
"modernstandardarabic": "ar-001",
"azerbaijani": "az",
"bulgarian": "bg",
"bengali": "bn",
"catalan": "ca",
"czech": "cs",
"danish": "da",
"german": "de",
"greek": "el",
"english": "en",
"americanenglish": "en-us",
"britishenglish": "en-gb",
"spanish": "es",
"europeanspanish": "es-es",
"latinamericanspanish": "es-419",
"estonian": "et",
"persian": "fa",
"finnish": "fi",
"filipino": "fil",
"french": "fr",
"canadianfrench": "fr-ca",
"gujarati": "gu",
"hebrew": "he",
"hindi": "hi",
"croatian": "hr",
"hungarian": "hu",
"armenian": "hy",
"indonesian": "id",
"icelandic": "is",
"italian": "it",
"japanese": "ja",
"georgian": "ka",
"kazakh": "kk",
"khmer": "km",
"kannada": "kn",
"korean": "ko",
"kirghiz": "ky",
"lao": "lo",
"lithuanian": "lt",
"latvian": "lv",
"macedonian": "mk",
"malayalam": "ml",
"mongolian": "mn",
"marathi": "mr",
"malay": "ms",
"burmese": "my",
"nepali": "ne",
"dutch": "nl",
"norwegian": "no",
"punjabi": "pa",
"polish": "pl",
"portuguese": "pt",
"brazilianportuguese": "pt-br",
"europeanportuguese": "pt-pt",
"romanian": "ro",
"russian": "ru",
"sinhala": "si",
"slovak": "sk",
"slovenian": "sl",
"albanian": "sq",
"serbian": "sr",
"serbianlatin": "sr-latn",
"swedish": "sv",
"swahili": "sw",
"tamil": "ta",
"telugu": "te",
"thai": "th",
"turkish": "tr",
"ukrainian": "uk",
"urdu": "ur",
"uzbek": "uz",
"vietnamese": "vi",
"chinese": "zh",
"simplifiedchinese": "zh-hans",
"traditionalchinese": "zh-hant",
"zulu": "zu",
}
}