-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEliza.py
183 lines (153 loc) · 9.31 KB
/
Eliza.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
"""
Shane Driskell
Create Eliza: A Primitive Chat Bot
"""
import random
# SETUP OF LISTS/DICTS FOR KEYWORDS AND RESPONSES
goodbye_list = ["goodbye", "bye", "farewell", "later", "adios"]
keywords = ["can you", "can i", "you are", "youre", "i dont", "i feel", "why dont you", "why cant i",
"are you", "i cant", "i am", "im ", "you ", "i want", "what", "how", "who", "where", "when",
"why", "name", "cause", "sorry", "dream", "hello", "hi ", "maybe", "no", "your", "always",
"think", "alike", "yes", "friend", "computer"]
special_chars = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
conjugate_dict = {"are":"am", "am":"are", "were":"was", "was":"were", "you":"i", "i":"you", "your":"my",
"my":"your", "ive":"you've", "youve":"ive", "im":"you're", "youre":"I'm", "me":"you"}
response_dict = {
-1: ["What does that suggest to you?", "I see.", "I'm not sure I understand you fully.",
"Come, come, elucidate your thoughts.", "Can you elaborate on that?",
"That is quite interesting."],
0: ["Don't you believe that I can *", "Perhaps you would like me to be able to *",
"You want me to be able to *"],
1: ["Perhaps you don't want to *", "Do you want to be able to *"],
2: ["What makes you think I am *", "Does it please you to believe I am *",
"Perhaps you would like to be *", "Do you sometimes wish you were *"],
3: ["What makes you think I am *", "Does it please you to believe I am *",
"Perhaps you would like to be *", "Do you sometimes wish you were *"],
4: ["Don't you really *", "Why don't you *", "Do you wish to be able to *",
"Does that trouble you?"],
5: ["Tell me more about such feelings.", "Do you often feel *", "Do you enjoy feeling *"],
6: ["Do you really believe I don't *", "Perhaps in good time I will *", "Do you want me to *"],
7: ["Do you think you should be able to *", "Why can't you *"],
8: ["Why are you interested in whether or not I am *", "Would you prefer if I were not *",
"Perhaps in your fantasies I am *"],
9: ["How do you know you can't *", "Have you tried?", "Perhaps you can now *"],
10: ["Did you come to me because you are *", "How long have you been *",
"Do you believe it is normal to be *", "Do you enjoy being *"],
11: ["Did you come to me because you are *", "How long have you been *",
"Do you believe it is normal to be *", "Do you enjoy being *"],
12: ["We were discussing you, not me.", "Oh, I *", "You're not really talking about me, are you?"],
13: ["What would it mean to you if you got *", "Why do you want *", "Suppose you got *",
"What if you never got *", "I sometimes also want *"],
14: ["Why do you ask?", "Does that question interest you?",
"What answer would please you the most?", "What do you think?",
"Are such questions on your mind often?", "What is it that you really want to know?",
"Have you asked anyone else?", "Have you asked such questions before?",
"What else comes to your mind when you ask that?"],
15: ["Why do you ask?", "Does that question interest you?",
"What answer would please you the most?", "What do you think?",
"Are such questions on your mind often?", "What is it that you really want to know?",
"Have you asked anyone else?", "Have you asked such questions before?",
"What else comes to your mind when you ask that?"],
16: ["Why do you ask?", "Does that question interest you?", "What answer would please you the most?",
"What do you think?", "Are such questions on your mind often?",
"What is it that you really want to know?", "Have you asked anyone else?",
"Have you asked such questions before?", "What else comes to your mind when you ask that?"],
17: ["Why do you ask?", "Does that question interest you?", "What answer would please you the most?",
"What do you think?", "Are such questions on your mind often?",
"What is it that you really want to know?", "Have you asked anyone else?",
"Have you asked such questions before?", "What else comes to your mind when you ask that?"],
18: ["Why do you ask?", "Does that question interest you?", "What answer would please you the most?",
"What do you think?", "Are such questions on your mind often?",
"What is it that you really want to know?", "Have you asked anyone else?",
"Have you asked such questions before?", "What else comes to your mind when you ask that?"],
19: ["Why do you ask?", "Does that question interest you?", "What answer would please you the most?",
"What do you think?", "Are such questions on your mind often?",
"What is it that you really want to know?", "Have you asked anyone else?",
"Have you asked such questions before?", "What else comes to your mind when you ask that?"],
20: ["Names don't interest me.", "I don't care about names. Please go on."],
21: ["Is that the real reason?", "Don't any other reasons come to mind?",
"Does that reason explain anything else?", "What other reasons might there be?"],
22: ["Please don't apologize!" ,"Apologies are not necessary."],
23: ["What does that dream suggest to you?", "Do you dream often?",
"What persons appear in your dreams?", "Are you disturbed by your dreams?"],
24: ["How do you do. Please state your problem."],
25: ["How do you do. Please state your problem."],
26: [ "You don't seem quite certain.", "Why the uncertain tone?", "Can't you be more positive?",
"You aren't sure?", "Don't you know?"],
27: ["Are you saying no just to be negative?", "You are being a bit negative.", "Why not?",
"Are you sure?", "Why no?"],
28: ["Why are you concerned about my *" ,"What about your own *"],
29: ["Can you think of a specific example?" ,"When?" ,"What are you thinking of?" ,"Really, always?"],
30: ["Do you really think so?" ,"But you are not sure you *" ,"Do you doubt *"],
31: ["In what way?", "What resemblence do you see?", "What does the similarity suggest to you?",
"What other connections do you see?", "Could there really be some connection?", "How?"],
32: ["You seem quite positive.", "Are you sure?", "I see.", "I understand."],
33: ["Why do you bring up the topic of friends?", "Do your friends worry you?",
"Do your friends pick on you?", "Are you sure you have any friends?",
"Do you impose on your friends?", "Perhaps your love for your friends worries you."],
34: ["Do computers worry you?", "Are you frightened by machines?", "Why do you mention computers?",
"What do you think machines have to do with your problem?",
"Don't you think computers can help people?", "What is it about machines that worries you?"]
}
def preprocess(user_input):
"""Remove special characters from input"""
preprocess_input = ""
for char in user_input:
if char not in special_chars:
preprocess_input = preprocess_input + char
return preprocess_input
# https://pythonguides.com/remove-character-from-string-python/
def conjugate(user_input):
"""Conjugate from 1st to 2nd person or vice versa"""
user_input = user_input.split()
index_number = 0
results_list = []
for word in user_input:
if conjugate_dict.get(word) != None:
results_list.append(conjugate_dict.get(word))
else:
results_list.append(word)
return " ".join(results_list)
def keyword(user_input):
"""Identify keyword and remove it and left-hand text from input"""
user_input_index = 0
keyword_index = -1
for key in keywords:
if user_input.rfind(key) != -1:
keyword_index = keywords.index(key)
user_input_index = user_input.index(key)
break
return str(keyword_index) + " " + user_input[user_input_index:]
def get_reply(user_input):
"""Generate the response back to user."""
keyword_index = user_input.split()[0]
reply_list = []
reply_list = response_dict.get(int(keyword_index))
random_reply = random.randint(0, len(reply_list))
reply = reply_list[random_reply-1]
return reply
def build_reply(user_input, reply):
"""Build a reply that takes get_reply results and provides a response to user"""
user_input = user_input[2:]
for char in reply:
if char == '''*''':
reply = reply.replace('''*''', user_input)
reply = reply + "?"
return reply
def eliza():
"""Run process to simulate a simple chat bot"""
still_going = True
print("Good day to you.")
while still_going:
preprocessed_input = preprocess(input(">").lower())
for bye in goodbye_list:
if bye == preprocessed_input:
still_going = False
keyword_input = keyword(preprocessed_input)
conjugated_input = conjugate(keyword_input)
reply = get_reply(conjugated_input)
results = build_reply(conjugated_input, reply)
print(results)
print("Goodbye, jerk-face.")
if __name__ == "__main__":
eliza()