forked from JoshRosen/cmps140_creative_cooking_assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
greeter.py
291 lines (253 loc) · 9.9 KB
/
greeter.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"""greeter: assigns user name, selects agent, and initializes system"""
# for new users
# Gets and or creates a user name.
# reloads (pickles the user's data) - for now a dummy call saying this.
# TODO:
# When the user types in a good user name I need to recall the preferences.
# Add good bye throughout code.
# Add basic /profile command to code - once preferences in place.
# Add the feedback on dishes reccommended.
# Add pickle code to the system based on the selected user.
# Add some crowd sourcing for cruel, affectionate sayings.
# Integrate randomness into all utterances.
#
import re
import os
import os.path
# from test_nlg import *
nlg = None
def out(msg):
print(msg)
PROMPT = '>'
def out(string):
"""temporary fake nlg - just prints out"""
print(string)
help_string = """
Welcome to the Creative Cooking Agent. We have a few minimal features to make your
experience more pleasant. The system keeps track of your profile as it
goes along. That is it tries to guess what your tastes are, and uses that
information to make suggestion. This system is still under development so
please be patient with its various quirks. The following commands are made
available at the prompt:
/debug - gives you a python command line promt. This is mainly useful
for testing.
/profile - this will display your profile.
/emacs - this will switch the readline xface to emacs-mode. (new user default)
/vi - this will switch the readline xface to vi-mode.
/lu - will list all the existing user names.
Full source code documentation for this system can be found at:
http://mamabot.cse.ucsc.edu/docs
Current Limitations:
Too many to mention.
Suggestions of types of sentences that work:
...
If the system is being too rude to you, you may be using Helga the dominatrix
as your agent, please change to another agent - by typing: 'I want a new agent'.
Don't use Helga if you are squeamish.
Enjoy.
"""
root_path = '.'
users_dir = '%s/new_users' % root_path
# user_name
user_name = None
# agent info.
agent_name = None
agent_greeting = None
agent_mode = None
def help():
"""Provides help to the user"""
out(help_string)
def get_feedback():
"""for returning user, asks how they liked recent recipes"""
pass
user_max_len = 20 # come on it shouldn't be that long unless you are an excellent typist.
user_min_len = 2
def clean_valid_user_name(user_name):
try_user_name = user_name.strip()
good_chars = re.search('^\s*\w[\w\d_]*\s*$', try_user_name)
sz = len(try_user_name)
if good_chars and ((sz>=user_min_len) and (sz>=user_min_len)):
return(try_user_name)
else:
return(None)
good_user_name_help = """
We are a bit picky. Sorry. New user names must have between %d and %d characters,
they must start with an alphabetic character and must be made up only of alphabetic,
numeric characters or underscores.
""" % (user_min_len, user_max_len)
# Cruel
"I'm going to be force feeding you some recipes of my choice, you worthless s***.\n" \
+ "So be nice to me."
"You better be nice or I will be gagging you with butter and lard."
# Do some crowd sourcing of cruel sayings.
# Affectionate ...
"Hey, why don't you come to my kitchen sometime? Please be nice you sweetie pie"
"Hi I'm Helga the best dominatrix chef in the business."
agent_records = [
['Helga', 'cruel',
'Helga the best dominatrix chef in the business'],
['Marilyn', 'affectionate',
'Marylin Monrovia, the kindest, most affectionate chef, ever'],
['Kathy', 'normal',
'Kathy, the most straightforward and creative chef'],
]
def do_exit():
exit()
def select_an_agent(user_name):
"""Selects an agent. Cannot exit without selecting one.
We should add bye as a possible input. *EYE.
These sets the global vars: agent_name, agent_mode, agent_greeting.
"""
# This function assumes that these exist and are valid:
# dir_for_user
# user_name
global agent_name, agent_mode, agent_greeting
msg='Please select from one of these agents:\n'
cnt = 0
for rec in agent_records:
# show description of each agent.
cnt += 1
msg += '\t%d: %s\n' % (cnt, rec[2])
out(msg)
agent_name = None
while agent_name is None: # loop until selection happens.
msg = 'Please type a number for the agent between %d and %d.' \
% (1, len(agent_records) + 1)
out(msg)
agent_no = raw_input(PROMPT)
#EYE
if re.search('^\s*help\s*$', agent_no):
help()
continue
if re.search('^\s*bye\s*$', agent_no):
do_exit()
m = re.search('(\d)', agent_no) # look for any number.
if m:
agent_no = int(m.group(0)) - 1
if agent_no > len(agent_records):
out('\nInvalid number. Try again.')
continue
else:
out('\nInvalid number. Try again.')
continue
rec = agent_records[agent_no]
agent_name = rec[0]
agent_greeting = "Hi I'm %s" % rec[2]
agent_mode = rec[1]
# save the agent.
f = open(dir_for_user + '/agent', 'w')
f.write(agent_name + '\n')
f.close()
out('Ok you are working with agent %s' % agent_name)
return()
def create_new_user(user_name):
# create_new_user is called from only one place.
# It assumes that we have checked that the user does not exist
# and that the user name is valid.
# if this completes it returns True else it returns False.
# It defines the variables:
# agent_name
# nlg_mode
# and creates a users_dir
# If a user exists it will always have an agent name.
# right now it is put in a file.
#
global dir_for_user
os.path.exists('/etc/passwd')
if not os.path.exists(users_dir):
# os.makedirs if intermediate levels are needed.
os.mkdir(users_dir)
dir_for_user = '%s/%s.usr' % (users_dir, user_name)
if not os.path.exists(dir_for_user):
os.mkdir(dir_for_user)
out("\nOk, your user name will be %s\n" % user_name)
select_an_agent(user_name)
return()
def find_user(user_name):
global agent_name, agent_mode, agent_greeting
"""finds the user and if found restores the agent info"""
dir_for_user = '%s/%s.usr' % (users_dir, user_name)
if os.path.exists(dir_for_user):
agent_path = '%s/%s' % (dir_for_user, 'agent')
f = open(agent_path, 'r')
test_agent_name = f.readline()
test_agent_name = test_agent_name.strip()
for inx in range(len(agent_records)):
rec = agent_records[inx]
name = rec[0]
if name == test_agent_name:
agent_name = name
agent_greeting = rec[2]
agent_mode = rec[1]
return(True)
return(False)
else:
return(False)
def startup(nlg):
welcome = """
Welcome to the CREATIVE COOKING ADVISOR
Type help at any time to get helpful hints for using our program.
To exit simply say: bye. For help and hints on using the program type: help.
"""
global user_name
out(welcome)
logged_in = False
while not logged_in:
# a logged in user has a user_name, a loaded profile and an agent.
# initially a profile will just consist of an agent. That is it.
out("Please enter an existing user name or type 'new' to become a "
+ "new user.")
user_name = raw_input(PROMPT)
#EYE
if re.search('^\s*help\s*$', user_name):
help()
continue
if re.search('^\s*bye\s*$', user_name):
do_exit()
wants_new_user = re.search("^\s*new\s*$", user_name, re.IGNORECASE)
if wants_new_user: # to exit new user you must say good bye and start over.
new_user = None
out('As a new user you will need a user name. Please type one.')
while not new_user:
user_name = raw_input(PROMPT)
#EYE
if re.search('^\s*help\s*$', user_name):
help()
continue
if re.search('^\s*bye\s*$', user_name):
do_exit()
new_user = clean_valid_user_name(user_name)
if new_user is None:
out(good_user_name_help)
out('Please enter a good user name for yourself')
continue
found_user = find_user(new_user)
if found_user:
out('You have entered a user name that already exists.\n'
+ 'Please enter a different new user name or type bye '
+ 'to quit.')
new_user = None
continue
else:
out('Thanks, that is a great user name.')
create_new_user(new_user)
user_name = new_user
found_user = find_user(user_name)
# defines global vars: agent_name, agent_mode, agent_greeter
if not found_user:
out('I could not find that user name.')
continue # starts over trying to get a user name or new.
logged_in = True
if agent_mode == 'cruel':
hand_desc = 'strong grasping claws'
elif agent_mode == 'affectionate':
hand_desc = 'beautiful manicured hands'
else: # normal
hand_desc = 'competent hands'
nlg.change_tone(agent_mode) # cruel, affectionate, normal
out("Remember that any time you can type 'help' to get helpful hints\n"
+ 'for using our system. Now I will now put you in the '
+ '%s of chef: %s' % (hand_desc, agent_name)
)
out('Your user_name is %s, agent_name is %s, agent_mode is %s\nand agent_greeting is: %s'
% (user_name, agent_name, agent_mode, agent_greeting))