-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtek.go
executable file
·541 lines (512 loc) · 11 KB
/
tek.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
tek is an automatic tagging library for Go.
*/
package tek
import (
"math"
"sort"
"strings"
"unicode"
)
const (
VERSION = "0.1.1"
)
// need to expand more and rearranged
var indonesianStopWords []string = []string{
"di",
"dari",
"juga",
"lalu",
"dengan",
"ke",
"ini",
"itu",
"dia",
"dan",
"aku",
"saya",
"kamu",
"anda",
"kita",
"mereka",
"yang",
"adalah",
"walaupun",
"jika",
"jadi",
"akan",
"tetapi",
"begitupun",
"bilamana",
"bagaimanapun",
"apa",
"untuk",
"kepada",
"menurut",
"siapa",
"dimana",
"kapan",
"bagaimana",
"kenapa",
"mengapa",
"pada",
"dalam",
"ada",
"adapun",
"apapun",
"ya",
"tidak",
"bukan",
}
var englishStopWords []string = []string{
"a",
"an",
"are",
"arent",
"about",
"alone",
"also",
"am",
"and",
"as",
"at",
"after",
"all",
"another",
"any",
"be",
"because",
"before",
"beside",
"besides",
"between",
"but",
"by",
"come",
"does",
"doesnt",
"did",
"didnt",
"do",
"dont",
"we",
"for",
"his",
"him",
"himself",
"himselves",
"her",
"herself",
"herselves",
"how",
"our",
"ours",
"yours",
"your",
"with",
"my",
"you",
"the",
"in",
"that",
"thats",
"out",
"on",
"off",
"if",
"will",
"these",
"there",
"theres",
"those",
"he",
"she",
"it",
"its",
"us",
"is",
"would",
"wouldnt",
"was",
"wasnt",
"have",
"havent",
"were",
"werent",
"has",
"hasnt",
"wont",
"not",
"had",
"hadnt",
"isnt",
"etc",
"for",
"i",
"or",
"of",
"on",
"other",
"others",
"so",
"than",
"that",
"though",
"to",
"too",
"they",
"through",
"until",
}
var lang string = "en"
var stopWords []string = englishStopWords
// Define your own stop words by providing a slice of string of stop words
func SetStopWords(s []string) {
stopWords = s
}
// need to tweak these values later
// var modifier map[string]float64 = map[string]float64{ "nama": 2.5, "nomina" : 1.75, "verba" : 1, "adjektiva" : 0.5, "adverbia" : 0.75, "numeralia" : 0.5 }
var modifier map[string]float64 = map[string]float64{"nama": 3.5, "nomina": 3.0, "verba": 2.0, "adjektiva": 1.0, "adverbia": 0.25, "numeralia": 0.5}
type Vocab struct {
Id int `json:"id"`
Word string `json:"word"`
Type string `json:"type"`
}
var pos []*Vocab
// Set language used, defaulted to english if not called. If argument is not "id" or "en", empty stop words will be used
// For now only support Indonesian and English stop words
func SetLang(l string) error {
switch l {
case "id":
stopWords = indonesianStopWords
pos = indonesianPos
break
case "en":
stopWords = englishStopWords
break
default:
// if undefined language, use empty stopwords
stopWords = []string{}
break
}
lang = l
return nil
}
func findIdf(idx int, termsInfo []*Info, sentences [][]string, termsCount float64, term string, done chan<- bool) {
count := 0.0
for _, sen := range sentences {
found := false
for _, word := range sen {
if term == word {
found = true
}
}
if found {
count++
}
}
idf := math.Log(termsCount / count)
termsInfo[idx] = &Info{term, idf, 0.0, 0.0}
done <- true
}
func findTfidf(idx int, termsInfo []*Info, termsCount float64, sentences [][]string, done chan<- bool) {
count := 0.0
for _, sen := range sentences {
for _, word := range sen {
word = sanitizeWord(word)
if termsInfo[idx].Term == word {
count++
}
}
}
termsInfo[idx].Tf = count / termsCount
termsInfo[idx].Tfidf = termsInfo[idx].Tf * termsInfo[idx].Idf
done <- true
}
func modifyTfidfId(idx int, termsInfo []*Info, pos []*Vocab, done chan<- bool) {
for _, vocab := range pos {
if termsInfo[idx].Term != vocab.Word {
termsInfo[idx].Tfidf += termsInfo[idx].Tfidf * modifier["nama"]
break
}
if termsInfo[idx].Term == vocab.Word {
// go vet complains if we use this form
// if vocab.Type != "lain-lain" || vocab.Type != "pronomina" || vocab.Type != "interjeksi" || vocab.Type != "preposisi" {
// termsInfo[idx].Tfidf += termsInfo[idx].Tfidf * modifier[vocab.Type]
// }
if vocab.Type != "lain-lain" {
termsInfo[idx].Tfidf += termsInfo[idx].Tfidf * modifier[vocab.Type]
}
if vocab.Type != "pronomina" {
termsInfo[idx].Tfidf += termsInfo[idx].Tfidf * modifier[vocab.Type]
}
if vocab.Type != "interjeksi" {
termsInfo[idx].Tfidf += termsInfo[idx].Tfidf * modifier[vocab.Type]
}
if vocab.Type != "preposisi" {
termsInfo[idx].Tfidf += termsInfo[idx].Tfidf * modifier[vocab.Type]
}
break
}
}
done <- true
}
type Info struct {
Term string
Idf float64
Tf float64
Tfidf float64
}
// The main method of this package, return a slice of *Info struct, sorted by their weight descending.
func GetTags(text string, num int) []*Info {
// sequential ops, cannot go parallel
dict := createDictionary(text)
seq := createSeqDict(dict)
// we could go concurrent here
rmStopWordsChan := make(chan []string)
createSentencesChan := make(chan [][]string)
defer close(rmStopWordsChan)
defer close(createSentencesChan)
go removeStopWords(seq, stopWords, rmStopWordsChan)
go createSentences(text, createSentencesChan)
sens := <-createSentencesChan
seq = <-rmStopWordsChan
// end
termsCount := float64(len(flatten(sens)))
// More concurrency below
// original idf function
// var termsInfo []*Info
// for _, term := range seq {
// find idf of each term by counting word occurence first
// count := 0.0
// for _, sen := range sens {
// found := false
// for _, word := range sen {
// if term == word {
// found = true
// }
// }
// if found {
// count++
// }
// }
// idf := math.Log(termsCount / count)
// termsInfo = append(termsInfo, &Info{term, idf, 0.0, 0.0})
// }
// Parallelize the original function
doneChan := make(chan bool, len(seq))
defer close(doneChan)
termsInfo := make([]*Info, len(seq))
for i, term := range seq {
go findIdf(i, termsInfo, sens, termsCount, term, doneChan)
}
for range termsInfo {
<-doneChan
}
//
// original tf-idf function
// find each word their tf-idf
// for i, term := range termsInfo {
// var count float64
// for _, sen := range sens {
// for _, word := range sen {
// word = sanitizeWord(word)
// if term.Term == word {
// count++
// }
// }
// }
// termsInfo[i].Tf = count / termsCount
// termsInfo[i].Tfidf = termsInfo[i].Tf * term.Idf
// }
// parallelized
for i, _ := range termsInfo {
go findTfidf(i, termsInfo, termsCount, sens, doneChan)
}
for range termsInfo {
<-doneChan
}
//
if lang == "id" {
// original modifier function
// for i, term := range termsInfo {
// for _, vocab := range pos {
// if term.Term != vocab.Word {
// termsInfo[i].Tfidf += termsInfo[i].Tfidf * modifier["nama"]
// break
// }
// if vocab.Word == term.Term {
// if vocab.Type != "lain-lain" || vocab.Type != "pronomina" || vocab.Type != "interjeksi" || vocab.Type != "preposisi" {
// termsInfo[i].Tfidf += termsInfo[i].Tfidf * modifier[vocab.Type]
// break
// }
// break
// }
// }
// }
// paralellized modifier function
for i, _ := range termsInfo {
go modifyTfidfId(i, termsInfo, pos, doneChan)
}
for range termsInfo {
<-doneChan
}
//
}
// sort descending by tfidf
for i, v := range termsInfo {
j := i - 1
for j >= 0 && termsInfo[j].Tfidf < v.Tfidf {
termsInfo[j+1] = termsInfo[j]
j -= 1
}
termsInfo[j+1] = v
}
// out of range error guard
if num >= len(termsInfo) {
num = len(termsInfo)
}
// sort from highest tfidf first
sort.SliceStable(termsInfo, func(i, j int) bool {
return termsInfo[j].Tfidf < termsInfo[i].Tfidf
})
// return only N number of tags
result := make([]*Info, num)
copy(result, termsInfo[:num])
// empty termsInfo
termsInfo = []*Info{}
return result
}
func flatten(sens [][]string) []string {
var flat []string
for _, v := range sens {
flat = append(flat, v...)
}
return flat
}
func createSentences(text string, createSentencesChan chan<- [][]string) {
text = strings.TrimSpace(text)
words := strings.Fields(text)
var sentence []string
var sentences [][]string
for _, word := range words {
// lowercase them FIX 1
word = strings.ToLower(word)
// if there isn't . ? or !, append to sentence. If found, also append (and remove the non alphanumerics) but reset the sentence
if strings.ContainsRune(word, '.') || strings.ContainsRune(word, '!') || strings.ContainsRune(word, '?') {
word = strings.Map(func(r rune) rune {
if r == '.' || r == '!' || r == '?' {
return -1
}
return r
}, word)
// sanitize them FIX 2
word = sanitizeWord(word)
sentence = append(sentence, word)
sentences = append(sentences, sentence)
sentence = []string{}
} else {
// sanitize them FIX 2
word = sanitizeWord(word)
sentence = append(sentence, word)
}
}
if len(sentence) > 0 {
sentences = append(sentences, sentence)
}
sentences = uniqSentences(sentences)
createSentencesChan <- sentences
}
func uniqSentences(sentences [][]string) [][]string {
z := make([]string, len(sentences))
for i, v := range sentences {
j := strings.Join(v, " ")
z[i] = j
}
m := make(map[string]bool)
var uniq []string
for _, v := range z {
if m[v] {
continue
}
uniq = append(uniq, v)
m[v] = true
}
unique := make([][]string, len(uniq))
for i, v := range uniq {
unique[i] = strings.Fields(v)
}
return unique
}
func removeStopWords(seq []string, StopWords []string, rmStopWordsChan chan<- []string) {
var res []string
for _, v := range seq {
stopWord := false
for _, x := range StopWords {
if v == x {
stopWord = true
break
}
}
if !stopWord {
res = append(res, v)
}
}
rmStopWordsChan <- res
}
func sanitizeWord(word string) string {
word = strings.ToLower(word)
var prev rune
word = strings.Map(func(r rune) rune {
// don't remove '-' if it exists after alphanumerics
if r == '-' && ((prev >= '0' && prev <= '9') || (prev >= 'a' && prev <= 'z') || prev == 'ä' || prev == 'ö' || prev == 'ü' || prev == 'ß' || prev == 'é') {
return r
}
if !unicode.IsDigit(r) && !unicode.IsLetter(r) && !unicode.IsSpace(r) {
return -1
}
prev = r
return r
}, word)
return word
}
func createSeqDict(dict map[string]int) []string {
var seq []string
for term, _ := range dict {
seq = append(seq, term)
}
return seq
}
func createDictionary(text string) map[string]int {
// trim all spaces
text = strings.TrimSpace(text)
// lowercase the text
text = strings.ToLower(text)
// remove all non alphanumerics but spaces
var prev rune
text = strings.Map(func(r rune) rune {
// don't remove '-' if it exists after alphanumerics
if r == '-' && ((prev >= '0' && prev <= '9') || (prev >= 'a' && prev <= 'z') || prev == 'ä' || prev == 'ö' || prev == 'ü' || prev == 'ß' || prev == 'é') {
return r
}
if !unicode.IsDigit(r) && !unicode.IsLetter(r) && !unicode.IsSpace(r) {
return -1
}
prev = r
return r
}, text)
// TRYING TO FIX BUG : remove all double spaces left
text = strings.Replace(text, " ", " ", -1)
// turn it into bag of words
words := strings.Fields(text)
// turn it into dictionary
dict := make(map[string]int)
i := 1
for _, word := range words {
if dict[word] == 0 {
dict[word] = i
i++
}
}
return dict
}