-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
142 lines (128 loc) · 2.59 KB
/
model.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
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
package main
import (
"bufio"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
"unicode/utf8"
)
type Model struct {
*User
Name string `json:"name"`
Busy bool `json:"busy"`
}
type replyFeed struct {
login string
bot string
prompt string
result []string
busy bool
}
var (
nologinRx = regexp.MustCompile(`^[\\p{L}\\d_-–—]+:`)
nourlRx = regexp.MustCompile(`\w+:\/{2}[\d\w-]+(\.[\d\w-]+)*(?:(?:\/[^\s/]*))*`)
mites = []string{
"Delphis",
"megalomaniac",
"tearjerker",
"mahinaci9",
"miss_iceheart",
"BLUEWATER",
"Fulano",
}
goodp, badp []string
)
func readprompts(fname string) []string {
f, _ := os.Open(fname)
scanner := bufio.NewScanner(f)
var vec []string
for scanner.Scan() {
vec = append(vec, scanner.Text())
}
return vec
}
func prefix4(author string) string {
ismite := false
for _, mite := range mites {
if author == mite {
ismite = true
break
}
}
// 50%
if rand.Float32() > 0.5 {
return ""
}
if ismite {
badp = readprompts(datadir + "/bad.txt")
return badp[rand.Intn(len(badp))]
}
goodp = readprompts(datadir + "/good.txt")
return goodp[rand.Intn(len(goodp))]
}
func (model *Model) deliver(author string, res []string) {
if author != "" {
author += ": "
}
for i, line := range res {
line = strings.TrimSpace(line)
n := utf8.RuneCountInString(line)
<-time.After(time.Duration(n/6) * time.Second)
msg := line
if i == 0 {
msg = author + line
}
model.User.broadcast(msg)
if model.T != nil {
msg = ø(cue, model.Login, msg)
bot.Send(model.T, msg)
}
}
}
func (model *Model) feed(author, body string) []string {
body = nologinRx.ReplaceAllString(body, "")
body = nourlRx.ReplaceAllString(body, "")
body = strings.TrimSpace(body)
// if utf8.RuneCountInString(body) < 25 {
// return
// }
limit := 100 + rand.Intn(50)
path := "http://" + model.Name + ":8000/prompt"
req, _ := http.NewRequest("GET", path, nil)
q := req.URL.Query()
q.Add("q", body)
q.Add("prefix", prefix4(author))
q.Add("limit", strconv.Itoa(limit))
req.URL.RawQuery = q.Encode()
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Printf("%s error: %v\n", model.User.Login, err)
return nil
}
defer resp.Body.Close()
var result struct {
Lines []string `json:"result"`
}
json.NewDecoder(resp.Body).Decode(&result)
res := result.Lines
if len(res) < 1 {
return nil
}
if len(res) > 1 {
res = res[:len(res)-1]
}
if len(res) > 2 && rand.Float32() < 0.5 {
res = res[1:]
}
for i := range res {
res[i] = strings.ToLower(res[i])
}
return res
// go model.deliver(author, res)
}