-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbox.go
176 lines (148 loc) · 2.66 KB
/
chatbox.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
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
175
176
package main
import (
"bufio"
"fmt"
"os"
"strings"
"unicode"
)
func nonword(c rune) bool {
return !(unicode.IsLetter(c) || unicode.IsNumber(c))
}
func makeWords(q string) []string {
words := strings.FieldsFunc(q, nonword)
for i, w := range words {
words[i] = strings.ToLower(w)
}
return words
}
type chatter struct {
responses map[string]map[string]int
words map[string]map[string]int
}
func newChatter() *chatter {
return &chatter{
responses: make(map[string]map[string]int),
words: make(map[string]map[string]int),
}
}
func (c *chatter) add(q, a string) {
if c.responses[q] == nil {
c.responses[q] = make(map[string]int)
}
c.responses[q][a]++
words := makeWords(q)
for _, w := range words {
if c.words[w] == nil {
c.words[w] = make(map[string]int)
}
c.words[w][a]++
}
}
func getMostUsed(useMap map[string]int) (string, int) {
mostUsed := ""
highest := -1
for s, u := range useMap {
if u > highest {
mostUsed = s
highest = u
}
}
return mostUsed, highest
}
func (c *chatter) getBestResponse(q string) (string, bool) {
if c.responses[q] == nil {
return "", false
}
resp, _ := getMostUsed(c.responses[q])
c.responses[q][resp]++
return resp, true
}
func (c *chatter) getBestFromWords(q string) (string, bool) {
words := makeWords(q)
var (
source []string
resps []string
usage []int
)
for _, w := range words {
if c.words[w] == nil {
continue
}
resp, use := getMostUsed(c.words[w])
source = append(source, w)
resps = append(resps, resp)
usage = append(usage, use)
}
if len(resps) == 0 {
return "", false
}
src := ""
resp := ""
use := -1
for i := 0; i < len(resps); i++ {
if usage[i] > use {
src = source[i]
resp = resps[i]
use = usage[i]
}
}
c.words[src][resp]++
return resp, true
}
func (c *chatter) getLeastUsed() string {
var (
src string
resp string
use = int(^uint(0) >> 1) // Max integer
)
for q, m := range c.responses {
for a, u := range m {
if u < use {
src = q
resp = a
use = u
}
}
}
c.responses[src][resp]++
return resp
}
func (c *chatter) getBest(q string) string {
var (
resp string
ok bool
)
if resp, ok = c.getBestResponse(q); ok {
return resp
}
if resp, ok = c.getBestFromWords(q); ok {
return resp
}
return c.getLeastUsed()
}
func main() {
var (
chat = newChatter()
in = bufio.NewReader(os.Stdin)
cur string
prev = "Hello!\n"
err error
)
if len(os.Args) >= 2 {
prev = os.Args[2]
}
for {
fmt.Print(prev)
fmt.Print("> ")
cur, err = in.ReadString('\n')
if err != nil {
fmt.Println("bye bye")
break
}
chat.add(prev, cur)
prev = cur
cur = chat.getBest(prev)
prev = cur
}
}