-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatBot2.py
366 lines (290 loc) · 12.2 KB
/
chatBot2.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"""
This program is for the chatbot that is used to talk to the user
"""
import random
import csv
class ChattingBot:
def __init__(self):
self.imageData = None
def change_username_in_files(self, name):
'''
This function copies and changes the user_name word to the the actual user_name variable valuem, and writes
it to the personalQuestionBank folder
'''
file_list = ['tell_something_addonsLOCATION', 'tell_something_addonsPEOPLE',
'tell_something_addonsTITLE', 'ask_question_addonsLOCATION',
'ask_question_addonsPEOPLE', 'ask_question_addonsTITLE']
for file_name in file_list:
with open('QuestionBank/' + file_name + '.txt', 'r') as file_in:
with open('personalQuestionBank/' + file_name + '.txt', 'w') as file_out:
for line in file_in:
file_out.write(line.replace('user_name', name))
def iterate_images(self):
'''
This function iterates through metadata in images.csv and inputs the data in the main_function
'''
with open('imageDatabase/database.csv', 'r') as file:
count = 0
for line in file:
count += 1
file.seek(0)
wanted_line_number = random.randrange(0, count, 1)
count = 0
for line in file:
if count == wanted_line_number:
self.line1 = line.split(",")
file.close()
break
else:
count += 1
self.title = self.line1[1]
self.location = self.line1[2]
self.people = self.line1[3].split('|')
self.special_question = self.line1[4]
self.special_answer = self.line1[5]
self.score = self.line1[6]
self.im = image(self.title, self.location, self.people, self.special_question, self.special_answer, self.score)
self.imageData = [self.im, self.line1[0], self.line1]
return(self.im, self.line1[0], self.line1)
def main_function(self, image_input):
'''
This function runs the entirety of the talking about the images
'''
attributeList = []
while len(attributeList) < 4:
attribute_index = random.randrange(0, 4, 1)
attribute_tag = image_input.get_attribute_tag(attribute_index)
attribute = image_input.get_attribute(attribute_index)
if attribute not in attributeList:
should_ask = random.randrange(0, 2, 1)
if should_ask:
self.interfaceWriter(image_input.ask_question(attribute_tag))
while True:
self.answer = self.interfaceReader()
if self.answer == None:
continue
self.answer = self.answer[0]
break
self.interfaceWriter(image_input.check_answer(attribute, self.answer))
else:
if image_input.tell_something(attribute_tag) is not None:
self.interfaceWriter(image_input.tell_something(attribute_tag))
while True:
self.answer = self.interfaceReader()
if self.answer == None:
continue
self.answer = self.answer[0].lower()
break
if attribute_tag == 'location' and (self.answer == 'yes' or self.answer == 'yeah'):
self.interfaceWriter('Oh cool!')
elif attribute_tag == 'location' and self.answer == 'no':
self.interfaceWriter('Oh you should go there then. It is a really nice place.')
elif attribute_tag == 'people' and self.answer == 'yes':
self.interfaceWriter("Oh ok. Are they family?")
while True:
self.answer = self.interfaceReader()
if self.answer == None:
continue
self.answer = self.answer[0].lower()
break
if (self.answer == 'yes' or self.answer == 'yeah'):
self.interfaceWriter('Oh nice!')
else:
self.interfaceWriter('Oh ok. But you are still close to them. Nice!')
elif attribute_tag == 'people' and self.answer == 'no':
self.interfaceWriter('Oh ok')
attributeList.append(attribute)
else:
continue
self.interfaceWriter('Those are all of the questions')
def interfaceWriter(self, line):
with open("interface/interfaceSpeech.txt", "a") as file:
file.write(line)
file.write("\n")
def interfaceReader(self):
with open("interface/interfaceInput.txt") as file:
self.inputLine = []
for line in file:
self.inputLine.append(line)
if self.inputLine == []:
return(None)
else:
self.interfaceClearer()
return(self.inputLine)
def interfaceClearer(self):
print("cleared")
open("interface/interfaceInput.txt", "w").close()
def find_random_line(self, file_name):
'''
This functions returns a random line in a file given the file name/directory
'''
with open(file_name, 'r') as file:
count = 0
for line in file:
count += 1
file.seek(0)
wanted_line_number = random.randrange(0, count, 1)
count = 0
for line in file:
if count == wanted_line_number:
line1 = line
file.close()
return line1.strip('\n')
else:
count += 1
class image:
def __init__(self, title, location, people, special_question, special_answer, score):
self.title = title
self.location = location
self.people = people
self.special_question = special_question
self.special_answer = special_answer
self.score = score
def get_attribute_tag(self, number):
'''
This function returns the attribute type given a number from 0-4 inclusive
'''
if number == 0:
return 'title'
elif number == 1:
return 'location'
elif number == 2:
return 'people'
elif number == 3:
return 'special_question'
else:
raise Exception('number out of range')
def get_attribute(self, number):
'''
This function returns the attribute given a number from 0-4 inclusive
'''
if number == 0:
return self.title
elif number == 1:
return self.location
elif number == 2:
return self.people
elif number == 3:
return self.special_answer
else:
raise Exception('get_attribute NUMBER OUT OF RANGE')
def check_answer(self, obj, ans):
'''
This function returns a message based on ans relevance with obj as a answer key
'''
if type(obj) != list:
if ans.lower() in obj.lower():
return 'Yes, that is correct!'
elif obj.lower() in ans.lower():
return 'Yes...'
else:
return 'Not really.... It\'s ' + str(obj)
else:
ans = str(ans)
if ans.lower or ans.capitalize() in obj:
return 'Yes, that is correct!'
else:
print(obj)
print(ans)
return 'Not really.... It\'s ' + str(obj).strip('[').strip(']')
def ask_question(self, attr):
'''
This function asks a question giving an attribute type and returns a sentence
'''
if attr == 'title':
file = 'personalQuestionBank/ask_question_addonsTITLE.txt'
elif attr == 'location':
file = 'personalQuestionBank/ask_question_addonsLOCATION.txt'
elif attr == 'people':
file = 'personalQuestionBank/ask_question_addonsPEOPLE.txt'
elif attr == 'special_question':
return self.special_question
else:
raise Exception('ask_question ATTR IS NOT VALID')
return self.find_random_line(file)
def tell_something(self, attr):
'''
This function tells information given an attribute tag and uses info on the metadata of the image to return a
sentence
'''
if attr == 'title':
file_name = 'personalQuestionBank/tell_something_addonsTITLE.txt'
return self.find_random_line(file_name) + ' ' + self.title
elif attr == 'location':
file_name = 'personalQuestionBank/tell_something_addonsLOCATION.txt'
end_file_name = 'QuestionBank/tell_something_addonsLOCATIONend.txt'
return self.find_random_line(file_name) + ' ' + self.location + '. ' + self.find_random_line(end_file_name)
elif attr == 'people':
file_name = 'personalQuestionBank/tell_something_addonsPEOPLE.txt'
end_file_name = 'QuestionBank/tell_something_addonsPEOPLEend.txt'
return self.find_random_line(file_name) + ' ' + str(", ".join(self.people)) + '. ' + self.find_random_line(end_file_name)
elif attr == 'special_question':
pass
else:
raise Exception('tell_something ATTR IS NOT VALID')
def find_random_line(self, file_name):
'''
This functions returns a random line in a file given the file name/directory
'''
with open(file_name, 'r') as file:
count = 0
for line in file:
count += 1
file.seek(0)
wanted_line_number = random.randrange(0, count, 1)
count = 0
for line in file:
if count == wanted_line_number:
line1 = line
file.close()
return line1.strip('\n')
else:
count += 1
class smallTalker:
def __init__(self, name):
self.name = name
self.change_username_in_files()
def firstQuestion(self):
return(self.find_random_line('personal_con_init.txt'))
def reply(self, ans):
if 'you' in ans:
return(self.find_random_line('personal_con_reply.txt'))
elif 'sad' in ans or 'angry' in ans or 'furious' in ans:
return('I am so sorry to hear that. Perhaps I can cheer you up')
elif ' happy' in ans or 'joyful' in ans or 'cool' in ans or 'good' in ans or 'great' in ans:
return('I am happy to hear that')
def find_random_line(self, file_name):
'''
This functions returns a random line in a file given the file name/directory
'''
with open(file_name, 'r') as file:
count = 0
for line in file:
count += 1
file.seek(0)
wanted_line_number = random.randrange(0, count, 1)
count = 0
for line in file:
if count == wanted_line_number:
line1 = line
file.close()
return line1.strip('\n')
else:
count += 1
def change_username_in_files(self):
'''
This function copies and changes the user_name word to the the actual user_name variable valuem, and writes
it to the personalQuestionBank folder
'''
file_list = ['con_init', 'con_reply']
for file_name in file_list:
with open(file_name + '.txt', 'r') as file_in:
with open('personal_' + file_name + '.txt', 'w') as file_out:
for line in file_in:
file_out.write(line.replace('user_name', self.name))
# Main
"""
st = smallTalker()
chatBot = ChattingBot()
chatBot.iterate_images()
"""