-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole_chatbot.py
executable file
·78 lines (54 loc) · 1.84 KB
/
console_chatbot.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
#!/usr/bin/env python
import sys
import base_chatbot
class ConsoleBot(base_chatbot.Bot):
def initialize(self, configFile):
super(ConsoleBot,self).initialize(configFile)
self.registerCommands()
def registerCommands(self):
super(ConsoleBot, self).registerCommands()
print "Registering 'test'"
self.commands.update({'test' : self._cmd_test})
return
def _cmd_test(self, text):
print "Test command"
return True
def amDirectlyAddressed(self, text):
words = self.preprocessText(text).split()
if words[0] == self.NICK:
print "Directly addressed"
return words[0], ' '.join(words[1:])
return None, None
def run(self):
super(ConsoleBot, self).run()
print "Hi, my name is", self.NICK
while True:
try:
txt = raw_input("> ")
except EOFError:
print
break
direct, cmd_text = self.amDirectlyAddressed(txt)
if direct is not None:
# If we are directly addressed, then probability of our reponse should be 1
if direct == self.NICK:
if self.handleCommand(cmd_text):
continue
msg = {}
msg["text"] = txt
msg["speaker"] = "user"
msg["speaking_to"] = None
msg["p_reply"] = self.p_reply
reply = self.possiblyReply(msg)
if reply is not None:
print reply
self.addPhrase(txt)
#####
if __name__ == "__main__":
bot = ConsoleBot()
if len(sys.argv) > 1:
config_file = sys.argv[1]
print "Reading configuration from", config_file
bot.initialize(config_file)
bot.run()
bot.quit()