-
Notifications
You must be signed in to change notification settings - Fork 2
/
fwew.go
805 lines (773 loc) · 18.6 KB
/
fwew.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
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
// Fwew is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Fwew is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Fwew. If not, see http://gnu.org/licenses/
// Package main contains all the things
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"log"
"math/rand"
"os"
"strconv"
"strings"
"time"
"github.com/c-bata/go-prompt"
)
// Global
const (
idField int = 0 // dictionary.txt line Field 0 is Database ID
lcField int = 1 // dictionary.txt line field 1 is Language Code
navField int = 2 // dictionary.txt line field 2 is Na'vi word
ipaField int = 3 // dictionary.txt line field 3 is IPA data
infField int = 4 // dictionary.txt line field 4 is Infix location data
posField int = 5 // dictionary.txt line field 5 is Part of Speech data
defField int = 6 // dictionary.txt line field 6 is Local definition
srcField int = 7 // dictionary.txt line field 7 is Source data
)
// flags / options
var (
configuration Config
language, posFilter *string
showVersion, showInfixes *bool
showIPA, reverse *bool
showSource *bool
useAffixes, numConvert *bool
markdown *bool
filename *string
)
func intersection(a, b string) (c string) {
m := make(map[rune]bool)
for _, r := range a {
m[r] = true
}
for _, r := range b {
if _, ok := m[r]; ok {
c += string(r)
}
}
return
}
func similarity(w0, w1 string) float64 {
if w0 == w1 {
return 1.0
}
if len(w0) > len(w1)+1 {
return 0.0
}
vowels := "aäeiìoulr"
w0v := intersection(w0, vowels)
w1v := intersection(w1, vowels)
wis := intersection(w0, w1)
wav := intersection(w0v, w1)
if len(w0v) > len(w1v) {
return 0.0
}
if len(wav) == 0 {
return 0.0
}
scc := len(wis)
iratio := float64(scc) / float64(len(w0))
lratio := float64(len(w0)) / float64(len(w1))
return (iratio + lratio) / 2
}
func fwew(word string) []Word {
var (
result Word
results []Word
fields []string
defString string
added bool
)
badChars := strings.Split("` ~ @ # $ % ^ & * ( ) [ ] { } < > _ / . , ; : ! ? | + \\", " ")
// remove all the sketchy chars from arguments
for _, c := range badChars {
word = strings.Replace(word, c, "", -1)
}
// No Results if empty string after removing sketch chars
if len(word) == 0 {
return results
}
// hardcoded hack for tseyä
if word == "tseyä" {
result = InitWordStruct(result, []string{
"5268", "eng", "tsaw", "t͡saw", "NULL", "dem., pn.", "that, it (as intransitive subject)",
"http://forum.learnnavi.org/language-updates/some-glossed-over-words/msg254625/#msg254625 (03 Jul 2010)",
})
result.Affixes[Text("suf")] = []string{"yä"}
results = append(results, result)
return results
}
// Prepare file for searching
dictData, err := os.Open(Text("dictionary"))
if err != nil {
fmt.Println(errors.New(Text("noDataError")))
log.Fatal(err)
}
scanner := bufio.NewScanner(dictData)
// Go through each line and see what we can find
for scanner.Scan() {
line := scanner.Text()
// Store the fields of the line into fields array
fields = strings.Split(line, "\t")
// Put the stuff from fields into the Word struct
result = InitWordStruct(result, fields)
if fields[lcField] == *language {
// Looking for Local word in Definition field
if *reverse {
// whole-word matching
defString = StripChars(fields[defField], ",;")
if *posFilter == "all" || fields[posField] == *posFilter {
for _, w := range strings.Split(defString, " ") {
if strings.ToLower(w) == strings.ToLower(word) && !added {
results = append(results, result)
added = true
}
}
}
added = false
// Looking for Na'vi word in Na'vi field
} else {
// be able to search with or without --word+ marking
fields[navField] = strings.Replace(fields[navField], "+", "", -1)
fields[navField] = strings.Replace(fields[navField], "--", "", -1)
word = strings.Replace(word, "--", "", -1)
if strings.ToLower(fields[navField]) == strings.ToLower(word) {
results = append(results, result)
if !*useAffixes {
break
}
} else if *useAffixes {
// skip words that obviously won't work
s := similarity(fields[navField], word)
if s < 0.50 {
//fmt.Printf("Target: %s | Line: %s | [%f]\n", word, fields[navField], s)
continue
}
result.Target = word
result = Reconstruct(result)
if result.ID != "-1" {
results = append(results, result)
}
// reset these to not catch the next word
result.Target = ""
result.Attempt = ""
}
}
}
}
err = dictData.Close()
if err != nil {
fmt.Println(errors.New(Text("dictCloseError")))
log.Fatal(err)
}
return results
}
func printResults(results []Word) {
if len(results) != 0 {
var out string
for i, w := range results {
num := fmt.Sprintf("[%d] ", i+1)
nav := fmt.Sprintf("%s", w.Navi)
ipa := fmt.Sprintf("[%s] ", w.IPA)
pos := fmt.Sprintf("%s", w.PartOfSpeech)
inf := fmt.Sprintf("%s ", w.InfixLocations)
def := fmt.Sprintf("%s\n", w.Definition)
src := fmt.Sprintf(" %s: %s\n", Text("src"), w.Source)
if *markdown {
nav = "**" + nav + "** "
pos = "*" + pos + "* "
} else {
nav += " "
pos += " "
}
out += num
out += nav
if *showIPA {
out += ipa
}
if *showInfixes && w.InfixLocations != "\\N" && w.InfixLocations != "NULL" {
out += inf
}
out += pos
out += def
if *useAffixes && len(w.Affixes) > 0 {
for key, value := range w.Affixes {
out += fmt.Sprintf(" %s: %s\n", key, value)
}
}
if *showSource && w.Source != "" {
out += src
}
}
out += fmt.Sprintf("\n")
fmt.Print(out)
} else {
fmt.Println(Text("none"))
}
}
func setFlags(arg string, argsMode bool) {
var (
flagList []string
err error
langs = strings.Split(Text("languages"), ", ")
)
if argsMode {
start := IndexStr(arg, '[') + 1
flagList = strings.Split(arg[start:len(arg)-1], ",")
} else {
flagList = strings.Split(arg, " ")
}
for _, f := range flagList {
switch {
case f == "":
case f == "r":
*reverse = !*reverse
case f == "i":
*showInfixes = !*showInfixes
case f == "ipa":
*showIPA = !*showIPA
case f == "s":
*showSource = !*showSource
case f == "a":
*useAffixes = !*useAffixes
case f == "n":
*numConvert = !*numConvert
case f == "m":
*markdown = !*markdown
case strings.HasPrefix(f, "l="):
if ContainsStr(langs, f[2:]) {
*language = f[2:]
} else {
err = fmt.Errorf("%s: %s (%s: %s)", Text("invalidLanguageError"), f[2:], Text("options"), Text("languages"))
fmt.Println(err)
fmt.Println()
}
case strings.HasPrefix(f, "p="):
*posFilter = f[2:]
default:
err = fmt.Errorf("%s: %s", Text("noOptionError"), f)
fmt.Println(err)
fmt.Println()
}
}
if err == nil {
var out string
out += Text("set") + " "
out += "[ "
if *reverse {
out += "r "
}
if *showInfixes {
out += "i "
}
if *showIPA {
out += "ipa "
}
if *showSource {
out += "s "
}
if *useAffixes {
out += "a "
}
if *numConvert {
out += "n "
}
if *markdown {
out += "m "
}
out += fmt.Sprintf("l=%s p=%s", *language, *posFilter)
out += " ]\n"
if len(*filename) == 0 {
fmt.Println(out)
}
}
}
func printHelp() {
flag.Usage = func() {
fmt.Printf("%s: ", Text("usage"))
fmt.Printf("%s [%s] [%s]\n", Text("bin"), Text("options"), Text("words"))
fmt.Printf("%s:\n", Text("options"))
flag.PrintDefaults()
}
flag.Usage()
}
func syllableCount(w Word) int64 {
// syllable dot counter
var sdc int64 = 0
for _, char := range w.IPA {
if char == '.' {
sdc += 1
}
}
return sdc + 1
}
func listWordsSubset(args []string, subset []Word) []Word {
var (
results []Word
what = args[0]
cond = args[1]
spec = args[2]
)
// /list word starts tì and pos is n.
// /list syllables > 2 and pos is v.
for _, w := range subset {
switch what {
case "pos":
if cond == "is" {
if w.PartOfSpeech == spec {
results = append(results, w)
}
} else if cond == "has" {
if strings.Contains(w.PartOfSpeech, spec) {
results = append(results, w)
}
}
case "word":
if cond == "starts" {
if strings.HasPrefix(w.Navi, spec) {
results = append(results, w)
}
} else if cond == "ends" {
if strings.HasSuffix(w.Navi, spec) {
results = append(results, w)
}
} else if cond == "has" {
if strings.Contains(w.Navi, spec) {
results = append(results, w)
}
}
case "syllables":
ispec, err := strconv.ParseInt(spec, 10, 64)
if err != nil {
fmt.Println(Text("invalidDecimalError"))
return nil
}
switch cond {
case "<":
if syllableCount(w) < ispec {
results = append(results, w)
}
case "<=":
if syllableCount(w) <= ispec {
results = append(results, w)
}
case "=":
if syllableCount(w) == ispec {
results = append(results, w)
}
case ">=":
if syllableCount(w) >= ispec {
results = append(results, w)
}
case ">":
if syllableCount(w) > ispec {
results = append(results, w)
}
}
}
}
return results
}
func countLines() int {
var (
count int
fields []string
)
dictData, err := os.Open(Text("dictionary"))
if err != nil {
fmt.Println(errors.New(Text("noDataError")))
log.Fatal(err)
}
count = 1
scanner := bufio.NewScanner(dictData)
for scanner.Scan() {
line := scanner.Text()
fields = strings.Split(line, "\t")
if fields[lcField] == *language {
count++
}
}
err = dictData.Close()
if err != nil {
fmt.Println(errors.New(Text("dictCloseError")))
log.Fatal(err)
}
return count
}
func listWords(args []string) []Word {
var (
result Word
results []Word
fields []string
what = args[0]
cond = args[1]
spec = args[2]
count int
numLines int
)
// /list what cond spec
// /list pos has svin.
// /list pos is v.
// /list word starts ft
// /list word ends ang
// /list word has ts
// /list words first 20
// /list words last 30
// /list syllables > 1
// /list syllables = 2
// /list syllables <= 3
dictData, err := os.Open(Text("dictionary"))
if err != nil {
fmt.Println(errors.New(Text("noDataError")))
log.Fatal(err)
}
count = 1
numLines = countLines()
scanner := bufio.NewScanner(dictData)
for scanner.Scan() {
line := scanner.Text()
fields = strings.Split(line, "\t")
if fields[lcField] == *language {
switch what {
case "pos":
spec = strings.ToLower(spec)
if cond == "is" {
if fields[posField] == spec {
result = InitWordStruct(result, fields)
results = append(results, result)
}
} else if cond == "has" {
if strings.Contains(fields[posField], spec) {
result = InitWordStruct(result, fields)
results = append(results, result)
}
}
case "word":
spec = strings.ToLower(spec)
word := strings.ToLower(fields[navField])
if cond == "starts" {
if strings.HasPrefix(word, spec) {
result = InitWordStruct(result, fields)
results = append(results, result)
}
} else if cond == "ends" {
if strings.HasSuffix(word, spec) {
result = InitWordStruct(result, fields)
results = append(results, result)
}
} else if cond == "has" {
if strings.Contains(word, spec) {
result = InitWordStruct(result, fields)
results = append(results, result)
}
}
case "words":
s, err := strconv.Atoi(spec)
if err != nil {
log.Fatal(err)
}
if cond == "first" {
if count <= s {
result = InitWordStruct(result, fields)
results = append(results, result)
}
} else if cond == "last" {
if count >= numLines-s && count <= numLines {
result = InitWordStruct(result, fields)
results = append(results, result)
}
}
count++
case "syllables":
result = InitWordStruct(result, fields)
ispec, err := strconv.ParseInt(spec, 10, 64)
if err != nil {
fmt.Println(Text("invalidDecimalError"))
return nil
}
switch cond {
case "<":
if syllableCount(result) < ispec {
results = append(results, result)
}
case "<=":
if syllableCount(result) <= ispec {
results = append(results, result)
}
case "=":
if syllableCount(result) == ispec {
results = append(results, result)
}
case ">=":
if syllableCount(result) >= ispec {
results = append(results, result)
}
case ">":
if syllableCount(result) > ispec {
results = append(results, result)
}
}
}
}
}
err = dictData.Close()
if err != nil {
fmt.Println(errors.New(Text("dictCloseError")))
log.Fatal(err)
}
return results
}
func randomSubset(k int, subset []Word) []Word {
var (
results []Word
i int
r *rand.Rand
)
r = rand.New(rand.NewSource(time.Now().UnixNano()))
if k == -1337 {
k = r.Intn(r.Intn(countLines()))
} else if k < 1 {
return results
}
for _, w := range subset {
if w.LangCode == *language {
if i < k {
results = append(results, w)
} else {
j := r.Intn(i)
if j < k {
results[j] = w
}
}
i++
}
}
return results
}
func random(k int) []Word {
var (
results []Word
result Word
fields []string
i int
r *rand.Rand
)
r = rand.New(rand.NewSource(time.Now().UnixNano()))
if k == -1337 {
k = r.Intn(r.Intn(countLines()))
} else if k < 1 {
return results
}
dictData, err := os.Open(Text("dictionary"))
if err != nil {
fmt.Println(errors.New(Text("noDataError")))
log.Fatal(err)
}
scanner := bufio.NewScanner(dictData)
for scanner.Scan() {
line := scanner.Text()
fields = strings.Split(line, "\t")
if fields[lcField] == *language {
if i < k {
result = InitWordStruct(result, fields)
results = append(results, result)
} else {
j := r.Intn(i)
if j < k {
result = InitWordStruct(result, fields)
results[j] = result
}
}
i++
}
}
err = dictData.Close()
if err != nil {
fmt.Println(errors.New(Text("dictCloseError")))
log.Fatal(err)
}
return results
}
func slashCommand(s string, argsMode bool) {
var (
sc []string
command string
args []string
exprs [][]string
nargs int
)
sc = strings.Split(s, " ")
sc = DeleteEmpty(sc)
command = sc[0]
if len(sc) > 1 {
args = sc[1:]
nargs = len(args)
}
switch command {
case "/help":
printHelp()
case "/commands":
fmt.Println(Text("slashCommandHelp"))
case "/set":
setFlags(strings.Join(args, " "), argsMode)
case "/unset":
setFlags(strings.Join(args, " "), argsMode)
case "/list":
// word starts tì
if nargs == 3 {
printResults(listWords(args))
// word starts tì and pos is n. and syllables > 10
} else if nargs > 3 {
//validate length of args, needs to be 4n-1
valid := false
// 10 nested query triplets is insane overkill
for n := 1; n <= 10; n++ {
if 4*n-1 == nargs {
valid = true
break
}
}
if !valid {
fmt.Println()
return
}
for i := 0; i < len(args); i += 4 {
exprs = append(exprs, args[i:i+3])
}
subset := listWords(exprs[0])
for _, expr := range exprs[1:] {
subset = listWordsSubset(expr, subset)
}
printResults(subset)
} else {
fmt.Println()
}
case "/random":
// k
if nargs == 1 && args[0] != "" {
if args[0] == "random" {
printResults(random(-1337))
} else {
k, err := strconv.Atoi(args[0])
if err != nil {
log.Fatal(err)
}
printResults(random(k))
}
// k where what cond spec [and what cond spec...]
} else if nargs >= 5 && args[1] == "where" {
fargs := args[2:]
nFargs := len(fargs)
if nFargs == 3 {
if args[0] == "random" {
printResults(randomSubset(-1337, listWords(fargs)))
} else {
k, err := strconv.Atoi(args[0])
if err != nil {
log.Fatal(err)
}
printResults(randomSubset(k, listWords(fargs)))
}
}
} else {
fmt.Println()
}
case "/update":
err := DownloadDict()
if err != nil {
log.Fatal(err)
}
case "/quit", "/exit", "/q", "/wc":
os.Exit(0)
default:
fmt.Println()
}
}
func main() {
var (
argsMode bool
fileMode bool
)
configuration = ReadConfig()
// Version flag, for displaying version data
showVersion = flag.Bool("v", false, Text("usageV"))
// Reverse direction flag, for local_lang -> Na'vi lookups
reverse = flag.Bool("r", false, Text("usageR"))
// Language specifier flag
language = flag.String("l", configuration.Language, Text("usageL"))
// Infixes flag, opt to show infix location data
showInfixes = flag.Bool("i", false, Text("usageI"))
// IPA flag, opt to show IPA data
showIPA = flag.Bool("ipa", false, Text("usageIPA"))
// Source flag, opt to show source data
showSource = flag.Bool("s", false, Text("usageS"))
// Filter part of speech flag, opt to filter by part of speech
posFilter = flag.String("p", configuration.PosFilter, Text("usageP"))
// Attempt to find all matches using affixes
useAffixes = flag.Bool("a", configuration.UseAffixes, Text("usageA"))
// Convert numbers
numConvert = flag.Bool("n", false, Text("usageN"))
// Markdown formatting
markdown = flag.Bool("m", false, Text("usageM"))
filename = flag.String("f", "", Text("usageF"))
flag.Parse()
argsMode = flag.NArg() > 0
fileMode = len(*filename) > 0
if *showVersion {
fmt.Println(Version)
if flag.NArg() == 0 {
os.Exit(0)
}
}
if fileMode { // FILE MODE
inFile, err := os.Open(*filename)
if err != nil {
fmt.Println(errors.New(Text("noFileError")))
log.Fatal(err)
}
scanner := bufio.NewScanner(inFile)
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "#") && line != "" {
fmt.Printf("cmd %s\n", line)
executor(line)
}
}
err = inFile.Close()
if err != nil {
fmt.Println(errors.New(Text("fileCloseError")))
log.Fatal(err)
}
} else if argsMode { // ARGS MODE
for _, arg := range flag.Args() {
arg = strings.Replace(arg, "’", "'", -1)
if strings.HasPrefix(arg, "set[") && strings.HasSuffix(arg, "]") {
setFlags(arg, argsMode)
} else if strings.HasPrefix(arg, "unset[") && strings.HasSuffix(arg, "]") {
setFlags(arg, argsMode)
} else {
executor(arg)
}
}
} else { // INTERACTIVE MODE
fmt.Println(Text("header"))
p := prompt.New(executor, completer,
prompt.OptionTitle(Text("name")),
prompt.OptionPrefix(Text("prompt")),
prompt.OptionSelectedDescriptionTextColor(prompt.DarkGray),
)
p.Run()
}
}