-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.go
54 lines (51 loc) · 1.33 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package chatfreely
import (
"fmt"
"github.com/mb-14/gomarkov"
"log"
"strings"
)
// PrintBlogPost prints out a post from the given training data.
func PrintBlogPost(chain *gomarkov.Chain) error {
order := chain.Order
if order == 0 {
order = 1
}
log.Printf("Generating post. Order %d", order)
tokens := []string{gomarkov.StartToken}
for i := 1; i < order; i++ {
tokens = append(tokens, gomarkov.StartToken)
}
for tokens[len(tokens)-1] != gomarkov.EndToken {
next, err := chain.Generate(tokens[(len(tokens) - order):])
if err != nil {
return err
}
if next != gomarkov.EndToken {
fmt.Print(next + " ")
}
tokens = append(tokens, next)
}
//fmt.Println(strings.Join(tokens[1:len(tokens)-1], " "))
return nil
}
// GenerateBlogPost generates and returns a post from the given training data.
func GenerateBlogPost(chain *gomarkov.Chain) (string, error) {
order := chain.Order
if order == 0 {
order = 1
}
log.Printf("Generating post. Order %d", order)
tokens := []string{gomarkov.StartToken}
for i := 1; i < order; i++ {
tokens = append(tokens, gomarkov.StartToken)
}
for tokens[len(tokens)-1] != gomarkov.EndToken {
next, err := chain.Generate(tokens[(len(tokens) - order):])
if err != nil {
return "", err
}
tokens = append(tokens, next)
}
return strings.Join(tokens[order:len(tokens)-1], " "), nil
}