forked from pebbe/textcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextcat.go
338 lines (303 loc) · 6.62 KB
/
textcat.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package textcat
import (
"errors"
"sort"
"strings"
"unicode/utf8"
)
const (
defaultThresholdValue = 1.03
defaultMaxCandidates = 5
defaultMinDocSize = 25
)
var (
errShort = errors.New("SHORT")
errUnknown = errors.New("UNKNOWN")
errAvail = errors.New("NOPATTERNS")
)
type TextCat struct {
utf8 bool
raw bool
lang map[string]bool
thresholdValue float64
maxCandidates int
minDocSize int
extra map[string]map[string]int
}
type resultType struct {
score int
lang string
}
type resultsType []*resultType
func (r resultsType) Len() int {
return len(r)
}
func (r resultsType) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
func (r resultsType) Less(i, j int) bool {
if r[i].score != r[j].score {
return r[i].score < r[j].score
}
return r[i].lang < r[j].lang
}
func NewTextCat() *TextCat {
tc := &TextCat{
lang: make(map[string]bool),
thresholdValue: defaultThresholdValue,
maxCandidates: defaultMaxCandidates,
minDocSize: defaultMinDocSize}
for d := range data {
tc.lang[d] = false
}
tc.extra = make(map[string]map[string]int)
return tc
}
func (tc *TextCat) SetThresholdValue(thresholdValue float64) {
tc.thresholdValue = thresholdValue
}
func (tc *TextCat) GetThresholdValue() float64 {
return tc.thresholdValue
}
func (tc *TextCat) SetMaxCandidates(maxCandidates int) {
tc.maxCandidates = maxCandidates
}
func (tc *TextCat) GetMaxCandidates() int {
return tc.maxCandidates
}
func (tc *TextCat) SetMinDocSize(minDocSize int) {
tc.minDocSize = minDocSize
}
func (tc *TextCat) GetMinDocSize() int {
return tc.minDocSize
}
func (tc *TextCat) ActiveLanguages() []string {
a := make([]string, 0, len(tc.lang))
for lang := range tc.lang {
if tc.lang[lang] {
a = append(a, lang)
}
}
sort.Strings(a)
return a
}
func (tc *TextCat) AvailableLanguages() []string {
a := make([]string, 0, len(tc.lang))
for lang := range tc.lang {
a = append(a, lang)
}
sort.Strings(a)
return a
}
func (tc *TextCat) DisableLanguages(language ...string) {
for _, lang := range language {
if _, exists := tc.lang[lang]; exists {
tc.lang[lang] = false
}
}
tc.raw = false
tc.utf8 = false
for lang := range tc.lang {
if tc.lang[lang] {
if !tc.raw && strings.HasSuffix(lang, ".raw") {
tc.raw = true
} else if !tc.utf8 && strings.HasSuffix(lang, ".utf8") {
tc.utf8 = true
}
if tc.raw && tc.utf8 {
break
}
}
}
}
func (tc *TextCat) DisableAllRawLanguages() {
tc.raw = false
for lang := range tc.lang {
if strings.HasSuffix(lang, ".raw") {
tc.lang[lang] = false
}
}
}
func (tc *TextCat) DisableAllUtf8Languages() {
tc.utf8 = false
for lang := range tc.lang {
if strings.HasSuffix(lang, ".utf8") {
tc.lang[lang] = false
}
}
}
func (tc *TextCat) EnableLanguages(language ...string) {
for _, lang := range language {
if _, exists := tc.lang[lang]; exists {
tc.lang[lang] = true
if strings.HasSuffix(lang, ".raw") {
tc.raw = true
} else if strings.HasSuffix(lang, ".utf8") {
tc.utf8 = true
}
}
}
}
func (tc *TextCat) EnableAllRawLanguages() {
for lang := range tc.lang {
if strings.HasSuffix(lang, ".raw") {
tc.lang[lang] = true
tc.raw = true
}
}
}
func (tc *TextCat) EnableAllUtf8Languages() {
for lang := range tc.lang {
if strings.HasSuffix(lang, ".utf8") {
tc.lang[lang] = true
tc.utf8 = true
}
}
}
func (tc *TextCat) Classify(text string) (languages []string, err error) {
languages, _, err = tc.ClassifyWithScores(text)
return
}
func (tc *TextCat) ClassifyWithScores(text string) (languages []string, outScores []int, err error) {
var mydata map[string]int
languages = make([]string, 0, tc.maxCandidates)
if tc.raw && len(text) < tc.minDocSize {
err = errShort
return
}
if tc.utf8 && utf8.RuneCountInString(strings.TrimSpace(reInvalid.ReplaceAllString(text, " "))) < tc.minDocSize {
err = errShort
return
}
scores := make([]*resultType, 0, len(tc.lang))
pattypes := make([]bool, 0, 2)
if tc.utf8 {
pattypes = append(pattypes, true)
}
if tc.raw {
pattypes = append(pattypes, false)
}
for _, utf8 := range pattypes {
patt := GetPatterns(text, utf8)
suffix := ".raw"
if utf8 {
suffix = ".utf8"
}
for lang := range tc.lang {
if !tc.lang[lang] || !strings.HasSuffix(lang, suffix) {
continue
}
if _, ok := tc.extra[lang]; ok {
mydata = tc.extra[lang]
} else {
mydata = data[lang]
}
score := 0
for n, p := range patt {
i, ok := mydata[p.S]
if !ok {
i = MaxPatterns
}
if n > i {
score += n - i
} else {
score += i - n
}
}
scores = append(scores, &resultType{score, lang})
}
}
if len(scores) == 0 {
err = errAvail
return
}
minScore := MaxPatterns * MaxPatterns
for _, sco := range scores {
if sco.score < minScore {
minScore = sco.score
}
}
threshold := float64(minScore) * tc.thresholdValue
nCandidates := 0
for _, sco := range scores {
if float64(sco.score) <= threshold {
nCandidates += 1
}
}
if nCandidates > tc.maxCandidates {
err = errUnknown
return
}
lowScores := make([]*resultType, 0, nCandidates)
for _, sco := range scores {
if float64(sco.score) <= threshold {
lowScores = append(lowScores, sco)
}
}
sort.Sort(resultsType(lowScores))
for _, sco := range lowScores {
languages = append(languages, sco.lang)
outScores = append(outScores, sco.score)
}
return
}
/*
// Add language created by 'textpat' in package github.com/pebbe/textcat/textpat
func (tc *TextCat) AddLanguage(language, filename string) error {
rawlines := make([]string, 0, MaxPatterns)
utf8lines := make([]string, 0, MaxPatterns)
target := 0
r, err := util.NewLinesReaderFromFile(filename)
if err != nil {
return err
}
for line := range r.ReadLines() {
if line == "[[[RAW]]]" {
target = 1
} else if line == "[[[UTF8]]]" {
target = 2
} else {
fields := strings.Fields(line)
if len(fields) > 0 {
if target == 1 {
rawlines = append(rawlines, fields[0])
} else if target == 2 {
utf8lines = append(utf8lines, fields[0])
}
}
}
}
if len(rawlines) == 0 && len(utf8lines) == 0 {
return errors.New("No patterns found in file \"" + filename + "\"")
}
if len(rawlines) > 0 {
a := make(map[string]int)
for i, p := range rawlines {
if i == MaxPatterns {
break
}
a[p] = i
}
l := language + ".raw"
tc.extra[l] = a
if _, ok := tc.lang[l]; !ok {
tc.lang[l] = false
}
}
if len(utf8lines) > 0 {
a := make(map[string]int)
for i, p := range utf8lines {
if i == MaxPatterns {
break
}
a[p] = i
}
l := language + ".utf8"
tc.extra[l] = a
if _, ok := tc.lang[l]; !ok {
tc.lang[l] = false
}
}
return nil
}
*/