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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
package main
import (
"bufio"
"fmt"
"log"
"math/rand"
"os"
"regexp"
"strconv"
"strings"
"github.com/go-redis/redis"
)
type incomingIRC struct {
Command string `json:"Command"`
Host string `json:"Host"`
Name string `json:"Name"`
Params []string `json:"Params"`
User string `json:"User"`
}
var quicklogMatch = regexp.MustCompile(`<(\w+)> (.+)`)
func main() {
redisc := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("127.0.0.1:%d", 6379),
Password: "", // no password set
DB: 0, // use default DB
})
fireaway := make(chan incomingIRC, 10)
go func() {
f, err := os.Open("tvl.txt")
if err != nil {
log.Printf("aaa %v", err)
os.Exit(0)
}
bio := bufio.NewReader(f)
for {
line, _, err := bio.ReadLine()
if err != nil {
break
}
sline := string(line)
offset := strings.Index(sline, "]")
notime := sline[offset+1:]
if quicklogMatch.MatchString(notime) {
bits := quicklogMatch.FindAllStringSubmatch(notime, -1)
if len(bits) != 0 {
if len(bits[0]) != 0 {
a := make([]string, 2)
a[1] = bits[0][2]
ic := incomingIRC{
Name: bits[0][1],
Params: a,
}
log.Printf("aa %#v", ic)
fireaway <- ic
}
}
}
}
}()
for msg := range fireaway {
// Learn
learnFromMessage(msg, redisc)
// os.Exit(0)
}
}
func generateMesasge(msg incomingIRC, redisc *redis.Client) string {
text := msg.Params[1]
username := msg.Name
text = strings.ToLower(text)
text = strings.Replace(text, ",", "", -1)
text = strings.Replace(text, ",", "", -1)
text = strings.Replace(text, ".", "", -1)
text = strings.Replace(text, "!", "", -1)
text = strings.Replace(text, "?", "", -1)
words := strings.Split(text, " ")
lastWord := propwords(username, words[0], redisc)
outputMsg := words[0] + " " + lastWord + " "
for {
lastWord = propwords(username, words[0], redisc)
if lastWord == "" || lastWord == "_END_" {
return outputMsg
}
outputMsg += lastWord + " "
if len(outputMsg) > 100 {
return outputMsg
}
}
}
func propwords(username string, start string, redisc *redis.Client) string {
userHash := redisc.HGetAll(fmt.Sprintf("%s-%s", username, start))
userHashMap, err := userHash.Result()
if err != nil {
genericHash := redisc.HGetAll(fmt.Sprintf("generic-%s", start))
userHashMap, err = genericHash.Result()
}
userIntHashMap, totalVectors := stringMaptoIntMap(userHashMap)
targetRand := rand.Intn(totalVectors)
progresRand := 0
for k, v := range userIntHashMap {
progresRand += v
if targetRand > progresRand {
return k
}
}
for k, _ := range userIntHashMap {
return k
}
return ""
}
func stringMaptoIntMap(in map[string]string) (outMap map[string]int, total int) {
outMap = make(map[string]int)
for k, v := range in {
i, err := strconv.ParseInt(v, 10, 64)
if err != nil {
continue
}
total += int(i)
outMap[k] = int(i)
}
return outMap, total
}
func learnFromMessage(msg incomingIRC, redisc *redis.Client) {
text := msg.Params[1]
text = strings.ToLower(text)
text = strings.Replace(text, ",", "", -1)
text = strings.Replace(text, ",", "", -1)
text = strings.Replace(text, ".", "", -1)
text = strings.Replace(text, "!", "", -1)
text = strings.Replace(text, "?", "", -1)
words := strings.Split(text, " ")
username := msg.Name
for k, word := range words {
// HINCRBY myhash field 1
nextWord := "_END_"
if len(words)-1 != k {
nextWord = words[k+1]
}
redisc.HIncrBy(fmt.Sprintf("%s-%s", username, word), nextWord, 1)
redisc.HIncrBy(fmt.Sprintf("generic-%s", word), nextWord, 1)
}
}
|