-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtek.go
More file actions
executable file
·637 lines (535 loc) · 33.3 KB
/
tek.go
File metadata and controls
executable file
·637 lines (535 loc) · 33.3 KB
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
tek is an automatic tagging library for Go.
*/
package tek
import (
"math"
"runtime"
"sort"
"strings"
"unicode"
)
const (
VERSION = "0.1.1"
)
func init() {
// Initialize default stop words map for English
stopWordsMap = make(map[string]bool, len(englishStopWords))
for _, word := range englishStopWords {
stopWordsMap[word] = true
}
}
// Comprehensive Indonesian stop words
var indonesianStopWords []string = []string{
// Common function words
"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",
// Personal pronouns
"aku", "saya", "engkau", "kamu", "anda", "beliau", "ia", "dia", "kita", "kami", "mereka",
// Demonstratives and relatives
"ini", "itu", "tersebut", "sini", "situ", "sana", "yang", "dimana", "kemana", "darimana",
// Conjunctions
"dan", "atau", "tetapi", "melainkan", "namun", "serta", "lalu", "kemudian", "lalu", "lalu", "lantas", "lalu", "lagi", "juga", "juga", "pun", "pula", "serta", "selanjutnya",
// Prepositions
"di", "ke", "dari", "pada", "dalam", "atas", "bawah", "antara", "dengan", "tanpa", "untuk", "bagi", "tentang", "hingga", "sampai", "sejak", "selama", "selama", "waktu",
// Question words
"apa", "siapa", "mengapa", "kenapa", "bagaimana", "berapa", "kapan", "dimana", "manakah", "bila",
// Negation and affirmation
"tidak", "bukan", "jangan", "tak", "jangan", "belum", "sudah", "telah", "pernah", "mungkin", "mestinya", "hendaknya", "semestinya",
// Auxiliary verbs
"dapat", "bisa", "akan", "mau", "ingin", "harus", "perlu", "mesti", "wajib", "boleh", "seharusnya", "sepatutnya",
// Common verbs (generic)
"adalah", "ialah", "yakni", "yaitu", "memiliki", "mempunyai", "terdapat", "ada", "ada", "terdapat", "wujud", "ada",
// Time expressions
"sekarang", "tadi", "tadinya", "nanti", "besok", "kemarin", "lusa", "tadi", "dulu", "dahulu", "sedang", "akan", "telah", "sudah", "masih", "belum", "pernah", "juga", "lagi", "juga", "sementara", "sementara", "sewaktu", "ketika", "tatkala",
// Place expressions
"sini", "situ", "sana", "dalam", "luar", "atas", "bawah", "depan", "belakang", "samping", "tengah", "pinggir", "ujung",
// Quantity and degree
"semua", "seluruh", "para", "segala", "berbagai", "macam", "jenis", "rupa", "banyak", "sedikit", "beberapa", "hampir", "kurang", "lebih", "paling", "sangat", "amat", "sekali", "cukup", "hanya", "saja", "melainkan", "selain", "kecuali",
// Modality and certainty
"mungkin", "barangkali", "tentu", "pasti", "jelas", "nyata", "jelas", "tentu", "pasti", "yakin", "percaya", "rasa", "kira", "agak", "agaknya", "kira-kira", "kayaknya", "sepertinya",
// Logical connectors
"karena", "sebab", "akibat", "demi", "guna", "agar", "supaya", "jika", "jikalau", "apabila", "bilamana", "manakala", "walaupun", "meskipun", "kendati", "sekalipun", "biarpun", "walaupun", "meski",
// Additional common function words
"bahwa", "bahwa", "bahwa", "hanya", "saja", "melainkan", "selain", "kecuali", "selain", "terkecuali", "justru", "justru", "justru", "malah", "malahan", "bahkan", "lebih-lebih", "terlebih", "apalagi",
// Numerical and temporal markers
"pertama", "kedua", "ketiga", "keempat", "kelima", "keenam", "ketujuh", "kedelapan", "kesembilan", "kesepuluh", "terakhir", "awal", "mula-mula", "sejak", "semenjak", "sedari",
// Discourse markers
"nah", "lho", "kok", "toh", "dong", "kan", "tuh", "nih", "deh", "yah", "yak", "yoi", "iya", "ya", "betul", "benar", "tentu", "pasti",
// Common Indonesian particles and interjections
"dong", "kan", "kok", "lho", "toh", "nih", "tuh", "deh", "yah", "sih", "dong", "yah", "yak", "ya", "oh", "ah", "aduh", "wah", "asyik", "asyik", "mantap", "cihuy",
// Additional generic action words
"melakukan", "membuat", "menjadi", "terjadi", "memberikan", "menggunakan", "mengatakan", "mendapatkan", "menjalankan", "melihat", "mendapat", "memberi", "mengambil", "menuju", "terhadap", "mengalami", "pergi",
// Common filler and transition words
"umumnya", "biasanya", "pada umumnya", "pada dasarnya", "intinya", "singkatnya", "ringkasnya", "garis besarnya", "sekali lagi", "lagi-lagi", "berulang kali", "berkali-kali", "beberapa kali", "sekian",
// Indonesian specific connectors
"sedangkan", "padahal", "biarpun", "sekalipun", "kendatipun", "meskipun", "walaupun", "daripada", "alih-alih", "justru", "malahan", "bahkan", "terlebih", "apalagi",
// Common discourse adverbs
"sering", "kerap", "acap", "kerapkali", "seringkali", "kerapkali", "biasanya", "umumnya", "lazimnya", "umumnya", "pada umumnya", "pada dasarnya", "pada hakikatnya",
// Additional common Indonesian words
"tersebut", "tersebut", "tersebut", "berikut", "berikut", "berikut", "berikutnya", "selanjutnya", "berikutnya", "seterusnya", "kemudian", "lalu", "lantas", "langsun", "langsung",
// Common Indonesian expressions
"hal", "sesuatu", "segala", "macam", "jenis", "bentuk", "rupa", "kondisi", "keadaan", "situasi", "kedudukan", "posisi", "tempat", "lokasi", "area", "wilayah", "daerah",
// More function words
"sebab", "karena", "akibat", "dampak", "pengaruh", "efek", "imbas", "akibatnya", "oleh karena itu", "oleh sebab itu", "maka", "maka dari itu", "karenanya", "oleh sebab itu",
// Comparative and superlative markers
"lebih", "paling", "ter-", "ber-", "per-", "tersebut", "terkait", "berkaitan", "menyangkut", "mengenai", "tentang", "soal", "urusan", "hal", "masalah",
// Common Indonesian modal particles
"lah", "kah", "tah", "dong", "kan", "toh", "kok", "sih", "nih", "tuh", "deh", "yah", "yuk", "ayo", "mari", "silakan", "pergilah", "datanglah",
}
// Comprehensive English stop words
var englishStopWords []string = []string{
// Articles and determiners
"a", "an", "the", "this", "that", "these", "those", "another", "other", "others", "some", "any", "no", "every", "each", "either", "neither", "both", "all", "several", "various", "certain", "such", "what", "whatever", "which", "whichever", "many", "much", "more", "most", "few", "fewer", "fewest", "little", "less", "least", "some", "somebody", "someone", "something", "somewhere", "anybody", "anyone", "anything", "anywhere", "everybody", "everyone", "everything", "everywhere", "nobody", "nothing", "nowhere",
// Personal pronouns
"i", "me", "my", "mine", "myself", "you", "your", "yours", "yourself", "yourselves", "he", "him", "his", "himself", "she", "her", "hers", "herself", "it", "its", "itself", "we", "us", "our", "ours", "ourselves", "they", "them", "their", "theirs", "themselves",
// Possessive pronouns
"my", "your", "his", "her", "its", "our", "their", "mine", "yours", "hers", "ours", "theirs",
// Demonstratives
"this", "that", "these", "those", "here", "there", "now", "then", "ago", "before", "after", "since", "until", "when", "whenever", "while", "during",
// Prepositions
"about", "above", "across", "after", "against", "along", "amid", "among", "amongst", "around", "as", "at", "atop", "before", "behind", "below", "beneath", "beside", "besides", "between", "betwixt", "beyond", "by", "despite", "down", "during", "except", "for", "from", "in", "inside", "into", "like", "near", "next", "of", "off", "on", "onto", "out", "outside", "over", "per", "since", "through", "throughout", "till", "to", "toward", "towards", "under", "underneath", "until", "unto", "up", "upon", "via", "with", "within", "without", "worth",
// Conjunctions
"and", "but", "or", "nor", "for", "so", "yet", "after", "although", "as", "because", "before", "even", "if", "lest", "once", "provided", "rather", "since", "so", "than", "that", "though", "till", "unless", "when", "whenever", "where", "whereas", "wherever", "whether", "while", "why",
// Auxiliary verbs
"be", "am", "is", "are", "was", "were", "been", "being", "have", "has", "had", "having", "do", "does", "did", "doing", "done", "shall", "should", "will", "would", "can", "could", "may", "might", "must", "ought", "need", "dare", "used", "suppose", "seem", "appear", "happen", "chance",
// Common verbs (generic)
"be", "become", "been", "being", "have", "has", "had", "having", "do", "does", "did", "doing", "done", "go", "goes", "went", "going", "gone", "come", "comes", "came", "coming", "get", "gets", "got", "gotten", "getting", "make", "makes", "made", "making", "take", "takes", "took", "taken", "taking", "give", "gives", "gave", "given", "giving", "see", "sees", "saw", "seen", "seeing", "look", "looks", "looked", "looking", "say", "says", "said", "saying", "tell", "tells", "told", "telling", "ask", "asks", "asked", "asking", "work", "works", "worked", "working", "try", "tries", "tried", "trying", "need", "needs", "needed", "needing", "feel", "feels", "felt", "feeling", "seem", "seems", "seemed", "seeming", "leave", "leaves", "left", "leaving", "call", "calls", "called", "calling", "experience", "experiences", "experienced", "experiencing",
// Common adverbs
"also", "always", "never", "often", "sometimes", "usually", "rarely", "seldom", "occasionally", "frequently", "constantly", "continuously", "continually", "again", "once", "twice", "seldom", "rarely", "hardly", "scarcely", "barely", "almost", "nearly", "approximately", "roughly", "about", "around", "here", "there", "everywhere", "nowhere", "somewhere", "anywhere", "abroad", "away", "back", "forward", "backward", "up", "down", "upward", "downward", "inward", "outward", "north", "south", "east", "west", "northward", "southward", "eastward", "westward", "abroad", "outside", "inside", "indoors", "outdoors", "upstairs", "downstairs", "across", "along", "aside", "away", "back", "by", "down", "forth", "home", "near", "off", "on", "out", "over", "past", "round", "through", "under", "up",
// Time adverbs
"now", "then", "today", "tomorrow", "yesterday", "tonight", "soon", "later", "early", "late", "before", "after", "since", "until", "when", "whenever", "while", "during", "ago", "already", "yet", "still", "even", "just", "now", "presently", "recently", "currently", "lately", "immediately", "instantly", "quickly", "rapidly", "slowly", "gradually", "suddenly", "abruptly", "unexpectedly",
// Degree adverbs
"very", "quite", "rather", "fairly", "pretty", "somewhat", "slightly", "a little", "a bit", "extremely", "incredibly", "amazingly", "surprisingly", "remarkably", "notably", "particularly", "especially", "mainly", "mostly", "primarily", "chiefly", "principally", "largely", "generally", "usually", "typically", "normally", "commonly", "frequently", "often", "regularly", "repeatedly", "constantly", "continually", "always", "never", "rarely", "seldom", "hardly", "scarcely", "barely",
// Question words
"who", "whom", "whose", "what", "which", "where", "when", "why", "how", "whatever", "whichever", "whoever", "whomever", "whenever", "wherever", "however", "whyever", "howsoever",
// Negation
"not", "no", "none", "nothing", "nowhere", "neither", "never", "no one", "nobody", "hardly", "scarcely", "barely", "rarely", "seldom", "without", "lack", "lacking", "absence", "absent", "deny", "denied", "refuse", "refused", "reject", "rejected", "negative", "negatively",
// Affirmation
"yes", "yeah", "yep", "yup", "indeed", "certainly", "definitely", "absolutely", "surely", "truly", "really", "actually", "in fact", "of course", "naturally", "obviously", "clearly", "plainly", "evidently", "apparently", "seemingly", "supposedly", "presumably", "allegedly", "reportedly",
// Logical connectors
"therefore", "thus", "hence", "consequently", "accordingly", "so", "then", "henceforth", "thereby", "thereupon", "wherefore", "why", "because", "since", "as", "for", "due to", "owing to", "thanks to", "because of", "on account of", "by virtue of", "in view of", "in light of", "considering", "given", "granted", "assuming", "supposing", "provided", "providing", "if", "unless", "whether", "either", "or", "neither", "nor", "both", "and", "but", "yet", "however", "nevertheless", "nonetheless", "notwithstanding", "although", "though", "even though", "despite", "in spite of", "regardless of", "irrespective of", "disregarding",
// Additional common function words
"also", "too", "either", "neither", "both", "all", "every", "each", "any", "some", "none", "no", "not", "only", "just", "merely", "simply", "purely", "solely", "exclusively", "particularly", "especially", "specifically", "exactly", "precisely", "approximately", "roughly", "about", "around", "nearly", "almost", "virtually", "practically", "essentially", "basically", "fundamentally", "primarily", "mainly", "chiefly", "principally", "largely", "mostly", "generally", "usually", "typically", "normally", "commonly", "frequently", "often", "regularly", "repeatedly", "constantly", "continually", "always", "never", "rarely", "seldom", "hardly", "scarcely", "barely",
// Modal and auxiliary expressions
"can", "could", "may", "might", "must", "shall", "should", "will", "would", "ought", "need", "dare", "used to", "have to", "had to", "must", "should", "ought to", "supposed to", "going to", "about to", "bound to", "likely to", "expected to", "required to", "forced to", "compelled to", "made to",
// Common discourse markers
"well", "now", "so", "then", "anyway", "anyhow", "besides", "moreover", "furthermore", "additionally", "also", "too", "as well", "what's more", "in addition", "further", "again", "once more", "anyway", "anyhow", "regardless", "nevertheless", "nonetheless", "however", "but", "yet", "still", "though", "although", "even so", "all the same", "in any case", "in any event", "at any rate", "in either case", "whatever happens", "come what may",
// Filler and hesitation words
"uh", "um", "er", "ah", "oh", "well", "like", "you know", "I mean", "kind of", "sort of", "I guess", "I suppose", "I think", "I believe", "I feel", "I reckon", "I suspect", "I imagine", "I expect", "I hope", "I wish", "I want", "I need", "I'd like", "I'd prefer", "I'd rather", "I'd better", "I should", "I must", "I have to", "I've got to",
// Contractions and informal forms
"im", "youre", "hes", "shes", "its", "were", "theyre", "ive", "youve", "weve", "theyve", "id", "youd", "hed", "shed", "wed", "theyd", "ill", "youll", "hell", "shell", "well", "theyll", "isnt", "arent", "wasnt", "werent", "havent", "hasnt", "hadnt", "dont", "doesnt", "didnt", "wont", "wouldnt", "cant", "couldnt", "shouldnt", "shouldnt", "mustnt", "mightnt", "neednt", "darent", "didnt", "couldnt", "shouldnt", "wouldnt", "werent", "wasnt", "arent", "isnt", "havent", "hasnt", "hadnt", "doesnt", "dont", "didnt", "wont", "wouldnt", "cant", "couldnt", "shouldnt", "mustnt", "mightnt", "neednt",
// Additional common expressions
"etc", "et cetera", "and so on", "and so forth", "etcetera", "vice versa", "per se", "de facto", "de jure", "status quo", "modus operandi", "modus vivendi", "sine qua non", "terra incognita", "tabula rasa", "raison d'etre", "tour de force", "piece de resistance", "cri de coeur", "bete noire", "enfant terrible", "nom de plume", "nom de guerre", "tour de force", "force majeure", "fait accompli", "coup de grace", "coup d'etat", "pièce de résistance", "raison d'être",
// Numbers and numerical expressions
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred", "thousand", "million", "billion", "trillion", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "once", "twice", "thrice", "again", "single", "double", "triple", "quadruple", "multiple", "several", "various", "numerous", "countless", "innumerable", "many", "few", "several", "some", "any", "no", "none",
// Days, months, seasons
"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december", "spring", "summer", "autumn", "fall", "winter", "morning", "afternoon", "evening", "night", "midnight", "noon", "dawn", "dusk", "sunrise", "sunset", "daytime", "nighttime",
// Directions and locations
"north", "south", "east", "west", "northeast", "northwest", "southeast", "southwest", "up", "down", "left", "right", "forward", "backward", "upward", "downward", "inward", "outward", "across", "along", "around", "through", "between", "among", "amid", "behind", "below", "beneath", "beside", "beyond", "inside", "outside", "upon", "within", "without", "aboard", "about", "above", "across", "after", "against", "along", "amid", "among", "anti", "around", "as", "at", "before", "behind", "below", "beneath", "beside", "besides", "between", "beyond", "but", "by", "concerning", "considering", "despite", "down", "during", "except", "excepting", "excluding", "following", "for", "from", "in", "inside", "into", "like", "minus", "near", "of", "off", "on", "onto", "opposite", "outside", "over", "past", "per", "plus", "regarding", "round", "save", "since", "than", "through", "to", "toward", "towards", "under", "underneath", "unlike", "until", "up", "upon", "versus", "via", "with", "within", "without",
// Colors and basic adjectives
"red", "blue", "green", "yellow", "orange", "purple", "pink", "brown", "black", "white", "gray", "grey", "big", "small", "large", "little", "tiny", "huge", "enormous", "giant", "massive", "microscopic", "miniature", "minuscule", "long", "short", "tall", "high", "low", "deep", "shallow", "wide", "narrow", "thick", "thin", "fat", "skinny", "slim", "heavy", "light", "hot", "cold", "warm", "cool", "freezing", "boiling", "scorching", "icy", "new", "old", "young", "fresh", "stale", "modern", "ancient", "recent", "current", "past", "future", "present", "good", "bad", "nice", "nasty", "pleasant", "unpleasant", "beautiful", "ugly", "pretty", "handsome", "attractive", "repulsive", "clean", "dirty", "tidy", "messy", "neat", "disorganized", "organized", "orderly", "chaotic", "easy", "difficult", "hard", "simple", "complex", "complicated", "straightforward", "basic", "advanced", "elementary", "sophisticated",
// Basic shapes and materials
"circle", "square", "triangle", "rectangle", "oval", "round", "flat", "curved", "straight", "bent", "wood", "metal", "plastic", "glass", "paper", "stone", "rock", "sand", "water", "oil", "fire", "air", "earth", "soil", "dirt", "clay", "concrete", "steel", "iron", "gold", "silver", "copper", "bronze", "brass", "lead", "tin", "aluminum", "plastic", "rubber", "leather", "cloth", "fabric", "silk", "cotton", "wool", "linen", "nylon", "polyester",
// Food and drink basics
"food", "eat", "drink", "water", "milk", "bread", "rice", "meat", "fish", "chicken", "beef", "pork", "vegetable", "fruit", "apple", "banana", "orange", "grape", "strawberry", "potato", "tomato", "onion", "carrot", "lettuce", "cabbage", "corn", "wheat", "flour", "sugar", "salt", "pepper", "oil", "butter", "cheese", "egg", "coffee", "tea", "juice", "soda", "beer", "wine", "alcohol", "cocktail", "dessert", "cake", "cookie", "pie", "ice cream", "chocolate", "candy", "sweet", "sour", "bitter", "spicy", "hot", "mild", "fresh", "cooked", "raw", "baked", "fried", "boiled", "grilled", "roasted", "steamed", "frozen", "canned", "dried", "preserved",
// Body parts and health
"head", "face", "eye", "ear", "nose", "mouth", "lip", "tongue", "tooth", "teeth", "hair", "neck", "throat", "shoulder", "arm", "elbow", "wrist", "hand", "finger", "thumb", "chest", "back", "stomach", "waist", "hip", "leg", "knee", "ankle", "foot", "toe", "skin", "bone", "muscle", "blood", "heart", "lung", "brain", "liver", "kidney", "stomach", "health", "healthy", "sick", "ill", "disease", "illness", "pain", "ache", "hurt", "injury", "wound", "cut", "bruise", "medicine", "drug", "pill", "tablet", "capsule", "treatment", "therapy", "cure", "heal", "recover", "rest", "sleep", "wake", "tired", "exhausted", "energetic", "strong", "weak", "fit", "exercise", "workout", "diet", "nutrition",
// Family and relationships
"family", "mother", "father", "parent", "brother", "sister", "sibling", "son", "daughter", "child", "kid", "baby", "infant", "toddler", "teenager", "adult", "elderly", "grandmother", "grandfather", "grandparent", "uncle", "aunt", "cousin", "nephew", "niece", "husband", "wife", "spouse", "partner", "boyfriend", "girlfriend", "friend", "enemy", "neighbor", "colleague", "coworker", "boss", "employee", "employer", "manager", "leader", "follower", "teacher", "student", "mentor", "protégé", "role model", "hero", "villain",
// Education and learning
"school", "college", "university", "academy", "institute", "class", "course", "lesson", "subject", "topic", "study", "learn", "teach", "educate", "train", "practice", "exercise", "homework", "assignment", "project", "research", "experiment", "test", "exam", "quiz", "grade", "score", "mark", "degree", "diploma", "certificate", "qualification", "skill", "ability", "talent", "gift", "knowledge", "information", "data", "fact", "truth", "wisdom", "intelligence", "smart", "clever", "brilliant", "stupid", "dumb", "ignorant", "uneducated", "illiterate", "literate", "educated", "learned", "scholarly", "academic", "theoretical", "practical", "applied", "basic", "advanced", "elementary", "intermediate", "beginner", "expert", "master", "professional", "amateur", "novice", "specialist", "generalist",
// Work and career
"work", "job", "career", "profession", "occupation", "business", "company", "corporation", "organization", "institution", "agency", "department", "office", "factory", "shop", "store", "market", "industry", "trade", "commerce", "economy", "finance", "money", "cash", "currency", "dollar", "pound", "euro", "yen", "price", "cost", "expense", "budget", "income", "salary", "wage", "pay", "earn", "profit", "loss", "gain", "investment", "saving", "spending", "buying", "selling", "shopping", "purchase", "sale", "discount", "bargain", "deal", "offer", "contract", "agreement", "negotiation", "meeting", "conference", "presentation", "report", "document", "file", "record", "database", "computer", "technology", "internet", "website", "email", "phone", "call", "message", "communication", "conversation", "discussion", "argument", "debate", "dispute", "conflict", "resolution", "solution", "answer", "question", "problem", "issue", "challenge", "opportunity", "success", "failure", "achievement", "accomplishment", "goal", "objective", "target", "purpose", "mission", "vision", "strategy", "plan", "project", "task", "duty", "responsibility", "obligation", "commitment", "dedication", "effort", "attempt", "try", "performance", "result", "outcome", "consequence", "effect", "impact", "influence", "change", "development", "progress", "improvement", "growth", "expansion", "increase", "decrease", "reduction", "decline", "rise", "fall", "growth", "shrink", "expand", "contract",
}
var lang string = "en"
var stopWords []string = englishStopWords
var stopWordsMap map[string]bool
func SetStopWords(s []string) {
stopWords = s
stopWordsMap = make(map[string]bool, len(s))
for _, word := range s {
stopWordsMap[word] = true
}
}
// 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
var posMap map[string]*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
// Build POS map for O(1) lookup
posMap = make(map[string]*Vocab, len(indonesianPos))
for _, vocab := range indonesianPos {
posMap[vocab.Word] = vocab
}
// Build stop words map
stopWordsMap = make(map[string]bool, len(indonesianStopWords))
for _, word := range indonesianStopWords {
stopWordsMap[word] = true
}
break
case "en":
stopWords = englishStopWords
// Build stop words map for English
stopWordsMap = make(map[string]bool, len(englishStopWords))
for _, word := range englishStopWords {
stopWordsMap[word] = true
}
posMap = nil
break
default:
// if undefined language, use empty stopwords
stopWords = []string{}
stopWordsMap = make(map[string]bool)
posMap = nil
break
}
lang = l
return nil
}
func findIdf(idx int, termsInfo []*Info, sentences [][]string, termsCount float64, term string) {
count := 0.0
for _, sen := range sentences {
found := false
for _, word := range sen {
if term == word {
found = true
// Don't break here - the original logic didn't break
}
}
if found {
count++
}
}
idf := math.Log(termsCount / count)
termsInfo[idx] = &Info{term, idf, 0.0, 0.0}
}
func findTfidf(idx int, termsInfo []*Info, termsCount float64, sentences [][]string) {
count := 0.0
term := termsInfo[idx].Term
for _, sen := range sentences {
for _, word := range sen {
word = sanitizeWord(word) // Restore sanitizeWord call for correctness
if term == word {
count++
}
}
}
termsInfo[idx].Tf = count / termsCount
termsInfo[idx].Tfidf = termsInfo[idx].Tf * termsInfo[idx].Idf
}
func modifyTfidfId(idx int, termsInfo []*Info, posMap map[string]*Vocab) {
term := termsInfo[idx].Term
found := false
for _, vocab := range pos { // Use original pos array for exact same behavior
if term != vocab.Word {
termsInfo[idx].Tfidf += termsInfo[idx].Tfidf * modifier["nama"]
found = true
break
}
if term == vocab.Word {
// Apply the exact same logic as original
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]
}
found = true
break
}
}
// If word not found in POS dictionary at all
if !found {
termsInfo[idx].Tfidf += termsInfo[idx].Tfidf * modifier["nama"]
}
}
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 {
return GetTagsWithWorkers(text, num, runtime.NumCPU())
}
// GetTagsWithWorkers allows specifying the number of workers for concurrent processing.
// If numWorkers is 0 or negative, it defaults to the number of available CPU cores.
func GetTagsWithWorkers(text string, num int, numWorkers 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, stopWordsMap, rmStopWordsChan)
go createSentences(text, createSentencesChan)
sens := <-createSentencesChan
seq = <-rmStopWordsChan
// end
termsCount := float64(len(flatten(sens)))
// Use worker pools for better concurrency
if numWorkers <= 0 {
numWorkers = runtime.NumCPU()
}
if len(seq) < numWorkers {
numWorkers = len(seq)
}
// Parallel IDF calculation with worker pool
termsInfo := make([]*Info, len(seq))
idfJobs := make(chan int, len(seq))
idfDone := make(chan bool, numWorkers)
// Start IDF workers
for w := 0; w < numWorkers; w++ {
go func() {
for idx := range idfJobs {
findIdf(idx, termsInfo, sens, termsCount, seq[idx])
}
idfDone <- true
}()
}
// Send jobs
for i := range seq {
idfJobs <- i
}
close(idfJobs)
// Wait for workers to complete
for i := 0; i < numWorkers; i++ {
<-idfDone
}
// Parallel TF-IDF calculation with worker pool
tfidfJobs := make(chan int, len(termsInfo))
tfidfDone := make(chan bool, numWorkers)
// Start TF-IDF workers
for w := 0; w < numWorkers; w++ {
go func() {
for idx := range tfidfJobs {
findTfidf(idx, termsInfo, termsCount, sens)
}
tfidfDone <- true
}()
}
// Send jobs
for i := range termsInfo {
tfidfJobs <- i
}
close(tfidfJobs)
// Wait for workers to complete
for i := 0; i < numWorkers; i++ {
<-tfidfDone
}
if lang == "id" {
// Parallel Indonesian POS modification with worker pool
posJobs := make(chan int, len(termsInfo))
posDone := make(chan bool, numWorkers)
// Start POS workers
for w := 0; w < numWorkers; w++ {
go func() {
for idx := range posJobs {
modifyTfidfId(idx, termsInfo, posMap)
}
posDone <- true
}()
}
// Send jobs
for i := range termsInfo {
posJobs <- i
}
close(posJobs)
// Wait for workers to complete
for i := 0; i < numWorkers; i++ {
<-posDone
}
}
// Sort only once using sort.SliceStable (remove the insertion sort)
sort.SliceStable(termsInfo, func(i, j int) bool {
return termsInfo[i].Tfidf > termsInfo[j].Tfidf
})
// out of range error guard
if num >= len(termsInfo) {
num = len(termsInfo)
}
// return only N number of tags
result := make([]*Info, num)
copy(result, termsInfo[:num])
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)
// Skip empty strings
if word != "" {
sentence = append(sentence, word)
}
if len(sentence) > 0 {
sentences = append(sentences, sentence)
}
sentence = []string{}
} else {
// sanitize them FIX 2
word = sanitizeWord(word)
// Skip empty strings
if 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, stopWordsMap map[string]bool, rmStopWordsChan chan<- []string) {
// Pre-allocate result slice with estimated capacity
res := make([]string, 0, len(seq))
for _, v := range seq {
// Skip empty strings and stop words
if v != "" && !stopWordsMap[v] {
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)
// Exclude pure numbers from tagging
if word != "" && isNumeric(word) {
return ""
}
return word
}
// isNumeric checks if a string contains only digits (and optionally hyphens for number ranges)
func isNumeric(s string) bool {
if s == "" {
return false
}
hasDigit := false
for _, r := range s {
if unicode.IsDigit(r) {
hasDigit = true
} else if r != '-' {
// If any character other than digit or hyphen, it's not a pure number
return false
}
}
return hasDigit
}
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 {
// Sanitize the word to exclude numbers
word = sanitizeWord(word)
// Skip empty strings
if word != "" && dict[word] == 0 {
dict[word] = i
i++
}
}
return dict
}