-
Notifications
You must be signed in to change notification settings - Fork 1
/
repl.py
executable file
·162 lines (135 loc) · 4.75 KB
/
repl.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# PA6, CS124, Stanford, Winter 2018
# v.1.0.2
#
# Original Python code by Ignacio Cases (@cases)
#
# Refs
# https://docs.python.org/3/library/cmd.html
# https://pymotw.com/2/cmd/
#
######################################################################
import cmd
import argparse
from chatbot import *
pa6_description = 'Simple Read-Eval-Print-Loop that handles the input/output part of the conversational agent'
class REPL(cmd.Cmd):
"""Simple REPL to handle the conversation with the chatbot."""
is_turbo = False
chatbot = Chatbot(is_turbo)
name = chatbot.bot_name()
bot_prompt = '\001\033[96m\002%s> \001\033[0m\002' % name
prompt = '> '
greeting = chatbot.greeting()
intro = chatbot.intro() + '\n' + bot_prompt + greeting
debug = False
doc_header = ''
misc_header = ''
undoc_header = ''
ruler = '-'
def set_turbo(self, is_turbo = False):
self.chatbot.is_turbo = is_turbo
def cmdloop(self, intro=None):
if self.debug == True:
print 'cmdloop(%s)' % intro
return cmd.Cmd.cmdloop(self, intro)
def preloop(self):
print self.header()
self.debug_chatbot = False
if self.debug == True:
print 'preloop(); Chatbot %s created and loaded' % self.chatbot
def postloop(self):
if self.debug == True:
print 'postloop()'
def parseline(self, line):
if self.debug == True:
print 'parseline(%s) =>' % line,
print 'applying function chat to line \'' + line + '\'...'
[cmd, arg] = ['','']
return [cmd, arg, line]
def onecmd(self, s):
if self.debug == True:
print 'onecmd(%s)' % s
if s:
return cmd.Cmd.onecmd(self, s)
else:
return
def emptyline(self):
if self.debug == True:
print 'emptyline()'
return cmd.Cmd.emptyline(self)
def default(self, line):
if self.debug == True:
print 'default(%s)' % line
if line == ":quit":
return True
else:
response = self.chatbot.process(line)
print self.bot_says(response)
def precmd(self, line):
if self.debug == True:
print 'precmd(%s)' % line
return cmd.Cmd.precmd(self, line)
def postcmd(self, stop, line):
if self.debug == True:
print 'postcmd(%s, %s)' % (stop, line)
if line == ':quit':
return True
elif (line.lower() == 'who are you?'):
self.secret(line)
elif line == ':debug on':
print 'enabling debug...'
self.debug_chatbot = True
elif line == ':debug off':
print 'disabling debug...'
self.debug_chatbot = False
# Debugging the chatbot
if self.debug_chatbot == True:
print self.chatbot.debug(line)
return cmd.Cmd.postcmd(self, stop, line)
def secret(self, line):
story = """A long time ago, in a remote land, a young developer named Alberto Caso managed to build an ingenious and mysterious chatbot... Now it's your turn!"""
print story
def do_prompt(self, line):
"Change the interactive prompt"
self.prompt = line + ': '
def emptyline(self):
if self.debug == True:
print 'emptyline()'
return cmd.Cmd.emptyline(self)
def postloop(self):
goodbye = self.chatbot.goodbye()
print self.bot_says(goodbye)
def bot_says(self, response):
return self.bot_prompt + response
def header(self):
header ="""
Welcome to Stanford CS124
_______ _______ ___
| || _ | | |
| _ || |_| | ____ | |___
| |_| || ||____| | _ |
| ___|| | | | | |
| | | _ | | |_| |
|___| |__| |__| |_______|
_______ __ __ _______ _______ _______ _______ _______ __
| || | | || _ || || _ || || || |
| || |_| || |_| ||_ _|| |_| || _ ||_ _|| |
| || || | | | | || | | | | | | |
| _|| || | | | | _ | | |_| | | | |__|
| |_ | _ || _ | | | | |_| || | | | __
|_______||__| |__||__| |__| |___| |_______||_______| |___| |__|
"""
return header
def process_command_line():
parser = argparse.ArgumentParser(description=pa6_description)
# optional arguments
parser.add_argument('--turbo', dest='is_turbo', type=bool, default=False, help='Enables turbo mode')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = process_command_line()
repl = REPL()
repl.set_turbo(args.is_turbo) # sigh, this is hacky -- we should be able to pass it directly to the constructor or initialization method, but there is an inheritance issue
repl.cmdloop()