-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpetscop_translator.go
85 lines (62 loc) · 1.43 KB
/
petscop_translator.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
package main
import (
"bufio"
"encoding/gob"
"fmt"
"log"
"os"
"petscop"
)
func main() {
verboseFlag := len(os.Args) > 1 && os.Args[1] == "-v"
fmt.Println("Loading dictionary...")
dictionaryFh, err := os.Open("data/dictionary.gob")
if err != nil {
log.Fatal(err)
}
tree := petscop.BKTree{}
if err := tree.Deserialize(dictionaryFh); err != nil {
log.Fatal(err)
}
frequenciesFh, err := os.Open("data/frequencies.gob")
if err != nil {
log.Fatal(err)
}
frequencies := make(map[string]int)
decoder := gob.NewDecoder(frequenciesFh)
if err := decoder.Decode(&frequencies); err != nil {
log.Fatal(err)
}
frequenciesFh.Close()
fmt.Println("---")
input := bufio.NewScanner(os.Stdin)
fmt.Print("Ask? ")
for input.Scan() {
line := input.Text()
if line == "quit" {
break
}
phonemes, err := petscop.ToPhonemes(petscop.ParseButtons(line))
if err != nil {
fmt.Printf("%s\n\nAsk? ", err.Error())
} else {
if verboseFlag {
fmt.Printf("Phonemes: %s\n", phonemes)
}
matches := tree.Find(phonemes, 1)
if verboseFlag {
fmt.Printf("All matches: %v\n", matches)
}
if len(matches.Matches) == 0 {
fmt.Print("No matches.\n\nAsk? ")
} else {
match := petscop.PickMatch(matches.Matches, frequencies)
if match.Distance == 0 {
fmt.Printf("> %s (exact match)\n\nAsk? ", match.Word)
} else {
fmt.Printf("> %s (approximate match)\n\nAsk? ", match.Word)
}
}
}
}
}