Ejemplo n.º 1
0
// Learn all the sentences in the given text file. The first and three last lines are ignored.
func learnPoem(filename string) {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Could not read %s.\n", filename)
		os.Exit(1)
	}
	lines := strings.Split(string(data), "\n")
	for i, line := range lines {
		if i > 1 && i < (len(lines)-3) {
			niall.Learn(line)
		}
	}
}
Ejemplo n.º 2
0
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()
}