-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive_bot.py
45 lines (38 loc) · 1.46 KB
/
live_bot.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
import re
import random
from bot_chatdata import BotChatData
class LiveBot():
def __init__(self):
#self.chatdata = BotChatData()
self.reflections = BotChatData().reflections
self.psychobabble = BotChatData().psychobabble
def reflect(self,fragment):
tokens = fragment.lower().split()
for i, token in enumerate(tokens):
if token in self.reflections:
tokens[i] = self.reflections[token]
return ' '.join(tokens)
def chat(self,statement):
for pattern, responses in self.psychobabble:
try:
match = re.match(pattern, statement.lower().rstrip(".!"))
if match:
response = random.choice(responses)
try:
return response.format(*[self.reflect(g) for g in match.groups()])
except:
pass
except:
return "Random " + self.genericResponse()
def genericResponse(self):
for pattern, responses in self.psychobabble:
try:
match = re.match(pattern, "qwertwet qwerty qwerqt")
if match:
response = random.choice(responses)
try:
return response.format(" that")
except:
pass
except:
return "Let change the topic. Tell me about your friends."