Ejemplo n.º 1
0
// Generate a poem with the given number of strophes and lines per strophe.
func generatePoem(strophes int, linesPerStrophe int) {
	// Find a catchy title
	title := strings.Trim(strings.Title(niall.Talk()), ".,;:!?_")
	if strings.Count(title, " ") > 4 {
		words := strings.Split(title, " ")
		title = strings.Join(words[:4], " ")
	}
	// Generate an appropriate amount of dashes
	dashes := ""
	for i := 0; i < len(title); i++ {
		dashes += "-"
	}
	// Output the header
	fmt.Printf("\n%s\n%s\n\n", title, dashes)
	// Generate and output all the srophes
	var line string
	for reps := 0; reps < strophes; reps++ {
		for i := 0; i < linesPerStrophe; i++ {
			// Try to get more than just a few words, up to 5 times
			for i2 := 0; i2 < 5; i2++ {
				line = niall.Talk()
				if strings.Count(line, " ") >= 3 {
					break
				}
			}
			fmt.Println(line)
		}
		fmt.Println()
	}
}
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()
}