forked from cs124/pa7-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.py
More file actions
179 lines (143 loc) · 5.41 KB
/
repl.py
File metadata and controls
179 lines (143 loc) · 5.41 KB
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
178
179
#!/usr/bin/env python
# PA7, CS124, Stanford
# v.1.0.4
#
# Original Python code by Ignacio Cases (@cases)
#
# See: https://docs.python.org/3/library/cmd.html
######################################################################
import argparse
import cmd
import logging
import sys
logging.basicConfig()
logger = logging.getLogger(__name__)
# Uncomment me to see which REPL commands are being run!
# logger.setLevel(logging.DEBUG)
from chatbot import Chatbot
# Modular ASCII font from http://patorjk.com/software/taag/
HEADER = """Welcome to Stanford CS124's
_______ _______ ___________
| || _ | \ \
| _ || |_| | ____ ------ /
| |_| || ||____| / /
| ___|| | / /
| | | _ | / /
|___| |__| |__| /___/
_______ __ __ _______ _______ _______ _______ _______ __
| || | | || _ || || _ || || || |
| || |_| || |_| ||_ _|| |_| || _ ||_ _|| |
| || || | | | | || | | | | | | |
| _|| || | | | | _ | | |_| | | | |__|
| |_ | _ || _ | | | | |_| || | | | __
|_______||__| |__||__| |__| |___| |_______||_______| |___| |__|
"""
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."""
prompt = '> '
doc_header = ''
misc_header = ''
undoc_header = ''
ruler = '-'
def __init__(self, creative=False):
super().__init__()
self.chatbot = Chatbot(creative=creative)
self.name = self.chatbot.name
self.bot_prompt = '\001\033[96m\002%s> \001\033[0m\002' % self.name
self.greeting = self.chatbot.greeting()
self.intro = self.chatbot.intro() + '\n' + self.bot_prompt + \
self.greeting
self.debug = False
self.debug_chatbot = False
def cmdloop(self, intro=None):
logger.debug('cmdloop(%s)', intro)
return super().cmdloop(intro)
def preloop(self):
logger.debug('preloop(); Chatbot %s created and loaded', self.chatbot)
print(HEADER)
self.debug_chatbot = False
def postloop(self):
goodbye = self.chatbot.goodbye()
print(self.bot_says(goodbye))
def onecmd(self, s):
logger.debug('onecmd(%s)', s)
if s:
return super().onecmd(s)
else:
return False # Continue processing special commands.
def emptyline(self):
logger.debug('emptyline()')
return super().emptyline()
def default(self, line):
logger.debug('default(%s)', line)
# Stop processing commands if the user enters :quit
if line == ":quit":
return True
else:
response = self.chatbot.process(line)
print(self.bot_says(response))
def precmd(self, line):
logger.debug('precmd(%s)', line)
return super().precmd(line)
def postcmd(self, stop, line):
logger.debug('postcmd(%s, %s)', stop, line)
if line == ':quit':
return True
elif line.lower() == 'who are you?':
self.do_secret(line)
elif ':debug on' in line.lower():
print('enabling debug...')
self.debug_chatbot = True
elif ':debug off' in line.lower():
print('disabling debug...')
self.debug_chatbot = False
# Debugging the chatbot
if self.debug_chatbot:
print(self.chatbot.debug(line))
return super().postcmd(stop, line)
def bot_says(self, response):
return self.bot_prompt + response
def do_prompt(self, line):
"""Set the interactive prompt."""
self.prompt = line + ': '
def do_secret(self, line):
"""Could it be... a secret message?"""
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 process_command_line():
parser = argparse.ArgumentParser(description=description)
parser.add_argument('--creative', dest='creative', action='store_true',
default=False, help='Enables creative mode')
args = parser.parse_args()
return args
if __name__ == '__main__':
#########################
# ADDED FOR TESTING #
#########################
class Tee(object):
# Modified from
# https://stackoverflow.com/questions/34366763/input-redirection-with-python
def __init__(self, input_handle, output_handle):
self.input = input_handle
self.output = output_handle
def readline(self):
result = self.input.readline()
self.output.write(result)
self.output.write('\n')
self.output.flush()
return result
# Forward all other attribute references to the input object.
def __getattr__(self, attr):
return getattr(self.input, attr)
if not sys.stdin.isatty():
sys.stdin = Tee(input_handle=sys.stdin, output_handle=sys.stdout)
#########################
# END TESTING CODE #
#########################
args = process_command_line()
repl = REPL(creative=args.creative)
repl.cmdloop()