-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.py
140 lines (113 loc) · 4.25 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
#!/usr/bin/env python
# PA6, CS124, Stanford, Winter 2019
# v.1.0.3
#
# Original Python code by Ignacio Cases (@cases)
#
# See: https://docs.python.org/3/library/cmd.html
######################################################################
import argparse
import cmd
import logging
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
_______ _______ ___
| || _ | | |
| _ || |_| | ____ | |___
| |_| || ||____| | _ |
| ___|| | | | | |
| | | _ | | |_| |
|___| |__| |__| |_______|
_______ __ __ _______ _______ _______ _______ _______ __
| || | | || _ || || _ || || || |
| || |_| || |_| ||_ _|| |_| || _ ||_ _|| |
| || || | | | | || | | | | | | |
| _|| || | | | | _ | | |_| | | | |__|
| |_ | _ || _ | | | | |_| || | | | __
|_______||__| |__||__| |__| |___| |_______||_______| |___| |__|
"""
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."""
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
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=pa6_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__':
args = process_command_line()
repl = REPL(creative=args.creative)
repl.cmdloop()