You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.1 KiB

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. func readTxt(path string) (string, error) {
  11. data, err := ioutil.ReadFile(path)
  12. if err != nil {
  13. //Do something
  14. }
  15. dataClean := strings.Replace(string(data), "\n", " ", -1)
  16. content := string(dataClean)
  17. return content, err
  18. }
  19. func main() {
  20. fmt.Print("entry the first word: ")
  21. newcommand := bufio.NewReader(os.Stdin)
  22. firstWord, _ := newcommand.ReadString('\n')
  23. firstWord = strings.TrimSpace(firstWord)
  24. fmt.Print("first word: ")
  25. fmt.Println(firstWord)
  26. fmt.Println("how many words you want on the text?")
  27. newcommand = bufio.NewReader(os.Stdin)
  28. answer, _ := newcommand.ReadString('\n')
  29. answer = strings.TrimSpace(answer)
  30. fmt.Print("Number of words on text to generate: ")
  31. fmt.Println(answer)
  32. count, err := strconv.Atoi(answer)
  33. if err != nil {
  34. fmt.Println("incorrect entry, need a positive number")
  35. }
  36. text, _ := readTxt("text.txt")
  37. fmt.Println("generating markov chains")
  38. states := markov.train(text)
  39. fmt.Println("generating text")
  40. generatedText := markov.generateText(states, firstWord, count)
  41. fmt.Println("")
  42. fmt.Println(generatedText)
  43. }