/
main.go
60 lines (54 loc) · 1.22 KB
/
main.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
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"strings"
"github.com/xyproto/niall"
)
func readln(prompt string) string {
fmt.Print(prompt)
in := bufio.NewReader(os.Stdin)
var cmd []byte
cmd, _ = in.ReadBytes('\n')
buf := bytes.NewBuffer(cmd)
return strings.TrimSpace(buf.String())
}
func main() {
niall.Init()
input := readln("> ")
END: for {
switch input {
case "quit","exit":
break END
case "save":
niall.SaveDictionary("niall.brain")
case "load":
niall.LoadDictionary("niall.brain")
case "help":
fmt.Println("Commands:")
fmt.Println(" load - loads niall.brain")
fmt.Println(" save - saves niall.brain")
fmt.Println(" correct [from] [to] - corrects spelling")
fmt.Println(" quit - exits")
fmt.Println(" exit - exits")
default:
if strings.Index(input, "correct") == 0 {
if strings.Count(input, " ") == 2 {
fields := strings.Split(input, " ")
from, to := fields[1], fields[2]
niall.CorrectSpelling(from, to)
fmt.Println("ok, corrected spelling")
} else {
fmt.Println("syntax for correcting spelling: correct [from] [to]")
}
} else {
niall.Learn(input)
fmt.Println(niall.Talk())
}
}
input = readln("> ")
}
niall.Quit()
}