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.

92 lines
1.8 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "strconv"
  6. "strings"
  7. )
  8. type Markov struct{}
  9. /*type NextState struct {
  10. Word string
  11. Count int
  12. Prob float64
  13. }*/
  14. type State struct {
  15. Word string
  16. Count int
  17. Prob float64
  18. NextStates []State
  19. }
  20. var markov Markov
  21. func addWordToStates(states []State, word string) ([]State, int) {
  22. iState := -1
  23. for i := 0; i < len(states); i++ {
  24. if states[i].Word == word {
  25. iState = i
  26. }
  27. }
  28. if iState > 0 {
  29. states[iState].Count++
  30. } else {
  31. var tempState State
  32. tempState.Word = word
  33. tempState.Count = 1
  34. states = append(states, tempState)
  35. iState = len(states) - 1
  36. }
  37. //fmt.Println(iState)
  38. return states, iState
  39. }
  40. func countWordsProb(words []string) {
  41. var states []State
  42. totalWordsCount := 0
  43. //count words
  44. for i := 0; i < len(words); i++ {
  45. var iState int
  46. totalWordsCount++
  47. states, iState = addWordToStates(states, words[i])
  48. if iState != len(words)-1 {
  49. states[iState].NextStates, _ = addWordToStates(states[iState].NextStates, words[iState+1])
  50. }
  51. }
  52. //count prob
  53. for i := 0; i < len(states); i++ {
  54. states[i].Prob = (float64(states[i].Count) / float64(totalWordsCount) * 100)
  55. for j := 0; j < len(states[i].NextStates); j++ {
  56. states[i].NextStates[j].Prob = (float64(states[i].NextStates[j].Count) / float64(totalWordsCount) * 100)
  57. }
  58. }
  59. fmt.Println("totalWordsCount: " + strconv.Itoa(totalWordsCount))
  60. fmt.Println(states)
  61. }
  62. func textToWords(text string) []string {
  63. s := strings.Split(text, " ")
  64. words := s
  65. return words
  66. }
  67. func readText(path string) (string, error) {
  68. data, err := ioutil.ReadFile(path)
  69. if err != nil {
  70. //Do something
  71. }
  72. content := string(data)
  73. return content, err
  74. }
  75. func (markov Markov) train(firstWord string, path string) string {
  76. text, _ := readText(path)
  77. words := textToWords(text)
  78. countWordsProb(words)
  79. return text
  80. }