Advertisement
Guest User

Untitled

a guest
Jul 14th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.92 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "math/rand"
  5.     "strings"
  6.     "bytes"
  7.     "io/ioutil"
  8.     "log"
  9.     "strconv"
  10.     "os"
  11.     "fmt"
  12. )
  13.  
  14. type Link struct {
  15.     target *State
  16.     occur int
  17. }
  18.  
  19. type State struct {
  20.     word  string
  21.     links []Link
  22. }
  23.  
  24. var MarkovSet []State
  25.  
  26. func AddLink(front *State, back *State) {
  27.     // See if front already has a link to back
  28.     for i, v := range front.links {
  29.         if v.target.word == back.word {
  30.             // Increment occurrences
  31.             front.links[i].occur++
  32.             return
  33.         }
  34.     }
  35.     // front does not have a link to back
  36.     front.links = append(front.links, Link{back, 1})
  37. }
  38.  
  39. // Safe method of adding states to avoid duplicates
  40. func AddState(word string) *State{
  41.     for _, i := range MarkovSet {
  42.         if i.word == word {
  43.             return nil
  44.         }
  45.     }
  46.  
  47.     MarkovSet = append(MarkovSet, State{word, nil})
  48.     return &MarkovSet[len(MarkovSet)-1]
  49. }
  50.  
  51. // Returns a State from MarkovSet with word value of word
  52. func StateFromWord(word string) *State {
  53.     for i, v := range MarkovSet {
  54.         if v.word == word {
  55.             return &MarkovSet[i]
  56.         }
  57.     }
  58.  
  59.     MarkovSet = append(MarkovSet, State{word, nil})
  60.     return &MarkovSet[len(MarkovSet)-1]
  61. }
  62.  
  63. func GetNextState(current *State) *State {
  64.     // Sum all occurrences
  65.     var sum int
  66.     for _, v := range current.links {
  67.         sum += v.occur
  68.     }
  69.     // Iterate through links
  70.     for _, v := range current.links {
  71.         if rand.Intn(sum) <= v.occur {
  72.             return v.target
  73.         }
  74.     }
  75.  
  76.     return nil // This should never happen
  77. }
  78.  
  79. func GenerateSentence() string {
  80.     var temp bytes.Buffer
  81.     current := GetNextState(StateFromWord("FRONT"))
  82.     for current.word != "BACK" {
  83.         temp.WriteString(current.word)
  84.         current = GetNextState(current)
  85.     }
  86.     temp.WriteString(".")
  87.     out := temp.String()
  88.     out = strings.Title(out)
  89.     return out
  90. }
  91.  
  92. func LoadSet(filename string) {
  93.     file, err := ioutil.ReadFile(filename)
  94.     if err != nil {
  95.         log.Fatal(err)
  96.     }
  97.  
  98.     if len(file) > 0 {
  99.         // Split file by line
  100.         lines := bytes.Split(file, []byte("\n"))
  101.         // Iterate through each line
  102.         for _, line := range lines {
  103.             // Split line into words
  104.             words := bytes.Split(line, []byte(" "))
  105.             key := words[0]
  106.             stateptr := StateFromWord(string(key))
  107.             // Remove the key
  108.             words = append(words[:0], words[:1]...)
  109.             for index := 0; index < len(words); index += 2 {
  110.                 // index = word, index+1 = occurrences
  111.                 fmt.Println(words[index])
  112.                 occur, err := strconv.Atoi(string(words[index+1]))
  113.                 if err != nil {
  114.                     occur = 1
  115.                 }
  116.                 stateptr.links = append(stateptr.links, Link{
  117.                     StateFromWord(string(words[index])),
  118.                     occur,
  119.                 })
  120.             }
  121.         }
  122.     }
  123. }
  124.  
  125. func SaveSet(filename string) {
  126.     if len(MarkovSet) > 0 {
  127.         file, err := os.Create(filename)
  128.         if err != nil {
  129.             log.Fatal(err)
  130.         }
  131.  
  132.         for _, curstate := range MarkovSet {
  133.             file.WriteString(curstate.word + " ")
  134.             for _, curlink := range curstate.links {
  135.                 file.WriteString(curlink.target.word + " " + strconv.Itoa(curlink.occur) + " ")
  136.             }
  137.             file.WriteString("\n")
  138.         }
  139.         file.Sync()
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement