-
Notifications
You must be signed in to change notification settings - Fork 4
/
markov.go
61 lines (46 loc) · 1.14 KB
/
markov.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
55
56
57
58
59
60
61
package main
import (
"github.com/garyburd/redigo/redis"
"log"
"regexp"
"strings"
)
type Markov struct {
length int
}
func (m Markov) StoreUpdates(updates []Result, connection redis.Conn) {
for _, update := range updates {
message := update.Message.Text
if message != "" && !strings.HasPrefix(message, "/") {
m.Store(update.Message.Text, connection)
}
}
}
func (m Markov) Store(text string, c redis.Conn) {
splitted := strings.Split(text, " ")
for index, word := range splitted {
if index < len(splitted)-1 {
c.Do("SADD", word, splitted[index+1])
}
}
}
func (m Markov) Generate(seed string, connection redis.Conn) string {
log.Printf("seed: %s\n", seed)
s := []string{}
s = append(s, seed)
splitted := strings.Split(seed, " ")
// Start the chain with the last word of the seed
key := splitted[len(splitted)-1]
for i := 1; i < m.length; i++ {
next, _ := redis.String(connection.Do("SRANDMEMBER", key))
s = append(s, next)
matched, _ := regexp.MatchString(".*[\\.;!?¿¡]$", next)
if next == "" || matched {
break
}
key = next
}
text := strings.Join(s, " ")
log.Printf("Text: %s\n", text)
return text
}