-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
177 lines (130 loc) · 4.18 KB
/
main.py
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
177
import random
import telegram.ext
import traceback
import bottoken
import config
import log
from model.charbagdict import CharBagDictModel
from model.charbagdict2 import CharBagDict2Model
# from model.chatter import ChatterModel
from model.fuzzdict import FuzzDictModel
from model.memeda import MemedaModel
from model.memeda2 import Memeda2Model
from model.naivedict import NaiveDictModel
from model.partialcharbagdict import PartialCharBagDictModel
from model.partialfuzzdict import PartialFuzzDictModel
from model.repeat import RepeatModel
models = [
(0.1, 0, CharBagDictModel()),
(0.1, 0, CharBagDict2Model()),
# (0, 0, ChatterModel()), # TODO
(0.3, 0, FuzzDictModel()),
(0.02, 0, MemedaModel()),
(0.03, 0, Memeda2Model()),
(0.15, 0.8, NaiveDictModel()),
(0.1, 0, PartialCharBagDictModel()),
(0.15, 0, PartialFuzzDictModel()),
(0.05, 0.2, RepeatModel()),
]
def error_handler(update, context):
log.error(update, context.error)
def train(operation):
for _, _, model in models:
operation(model, False)
def collect(operation, event_index):
candidates = []
for text_weight, sticker_weight, model in models:
for weight, payload in operation(model, True):
candidates.append((
(text_weight, sticker_weight)[event_index] * weight,
payload,
))
if config.debug:
print(candidates)
return candidates
def choose(candidates, force):
# choose one from the candidates
total = sum(
weight
for weight, payload in candidates
)
if force or random.random() < total:
target = total * random.random()
for weight, payload in candidates:
target -= weight
if target <= 0:
if config.debug:
print(payload)
return payload
return None
def handler(update, event_index, collect_operation, reply_operation):
log.log(update)
try:
force = update.message.chat.id == update.message.from_user.id or (
update.message.reply_to_message is not None
and update.message.reply_to_message.from_user.id == bottoken.self
)
rate = (config.rate_text, config.rate_sticker)[event_index]
if force or random.random() < rate:
# collect the candidates
candidates = collect(collect_operation, 0)
# choose and send reply
payload = choose(candidates, force)
if payload is not None:
reply_operation(payload)
else:
# train without collecting
train(collect_operation)
except:
traceback.print_exc()
log.error(update, 'internal error')
def text_handler(update, _):
handler(
update,
0,
lambda model, predict: model.text(update.message, predict),
update.message.reply_text
)
def sticker_handler(update, _):
handler(
update,
1,
lambda model, predict: model.sticker(update.message, predict),
update.message.reply_sticker
)
def main():
# load historical data
count = 0
for update in log.read_log():
count += 1
if count % 1000 == 0:
print('load:', count)
if update.message is not None:
if update.message.text is not None:
for _, _, model in models:
model.text(update.message, False)
if update.message.sticker is not None:
for _, _, model in models:
model.sticker(update.message, False)
print('total:', count)
print('ready')
# start the bot
# TODO: remove `use_context=True`
updater = telegram.ext.Updater(bottoken.token, use_context=True)
updater.dispatcher.add_error_handler(error_handler)
updater.dispatcher.add_handler(
telegram.ext.MessageHandler(
telegram.ext.Filters.text,
text_handler
)
)
updater.dispatcher.add_handler(
telegram.ext.MessageHandler(
telegram.ext.Filters.sticker,
sticker_handler
)
)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()