-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobamabot.py
182 lines (148 loc) · 5.25 KB
/
obamabot.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
#coding a simple chatbot
from textblob import TextBlob
import random
import logging
import os
os.environ['NLTK_DATA'] = os.getcwd() + '/nltk_data'
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
GREETINGS = ("hello", "hi", "how are you", "hi obama", "what's up")
RESPONSE = ["how are you", "hello to you", "hello"]
LOML = ("michelle", "michelle obama")
RANDOM_ANSWER =[ "Obama out!",
"You just kinda gotta let it *dusts shoulder* you know",
"Its what you gotta do",
"Hey! Get outta here",
"All of you know who I am"]
OBAMA_COMEBACK = ["I'd like to know what I'm talking about before I speak",
"Listen! You're in my house!",
"What the heck are you talking about?",
"He thought happy hour started earlier",
"What more do you think I should do"]
def greeting(sentence):
for word in sentence.words:
if word.lower() in GREETINGS:
return random.choice(RESPONSE)
def obama_reply(sentence):
logger.info("ObamaBot: respond to %s", sentence)
resp = respond(sentence)
return resp
def find_pronoun(sent):
pronoun = None
for w,p in sent.pos_tags:
if p == 'PRP' and w.lower() == 'you':
pronoun = 'I'
if p == 'PRP' and w == 'I':
pronoun = "You"
return pronoun
def find_noun(sent):
noun = None
if not noun:
for w,p in sent.pos_tags:
if p == 'NN':
noun = w
break
if noun:
logger.info("Found noun: %s", noun)
return noun
def find_verb(sent):
verb = None
pos = None
for w,p in sent.pos_tags:
if p.startswith('VB'):
verb = w
pos = p
break
return verb, pos
def find_adj(sent):
adj = None
for w,p in sent.pos_tags:
if p == 'JJ':
adj = w
break
return adj
def an_and_a(word):
return True if word[0] in 'aeiou' else False
def construct_response(pronoun, noun, verb):
response = []
if pronoun:
response.append(pronoun)
return "f"
def about_obama(pronoun, noun, adjective, verb):
resp = None
if pronoun == 'I' and (noun and adjective):
if noun:
if random.choice((True, False)):
resp = random.choice(OBAMA_NOUN).format(**{'noun': noun})
else:
resp = random.choice(OBAMA_VERB).format(**{'verb':verb})
return resp
OBAMA_NOUN = ["You're about four and a half years late on {noun} issue",
"I was fighting the {noun} fight!",
"Was that my {noun} oh goodness! Thats alright.",
"The fact of the matter is the {noun} was 1.3 trillion dollars. 1.3!",
"And we've got {noun} in this country! Which is great too!",
"Ask Michelle about {noun}",
"Your {noun} wasn't complicated, it was wrong!",
"This is the kinda {noun} designed to divide us",
"This is Barack Obama's {noun} plan",
"We we have fewer horses and {noun}"]
OBAMA_VERB = ["I'm looking forward to you {verb} me as well",
"What the heck are you {verb} about",
"Please {verb} governor",
"I know cuz I {verb} both of them",
"Well, I would have to investigate more Bill's {verb} before I accurately judge whether he was infact a brother",
"You said it was going to be {verb} and easy. You were wrong.",
"I don't understand {verb}",
"Who are they {verb} for?",
"This is part of the {verb} I've been going through for the past fifteen months",
"You're gonna sue me for {verb} my job?"]
def preprocess_text(sentence):
processed = []
words = sentence.split(' ')
for w in words:
if w == 'i':
w = 'I'
if w == "i'm":
w = "I'm"
if w.lower() in LOML:
w = "LOML"
processed.append(w)
return ' '.join(processed)
def respond(sentence):
processed = preprocess_text(sentence)
parsed = TextBlob(processed)
pronoun, noun, adjective, verb = token_sentence(parsed)
resp = about_obama(pronoun, noun, adjective, verb)
if not resp:
resp = greeting(parsed)
if not resp:
if not pronoun:
resp = random.choice(RANDOM_ANSWER)
elif pronoun == 'I' and not verb:
resp = random.choice(OBAMA_COMEBACK)
else:
resp = construct_response(pronoun, noun, verb)
if not resp:
resp = random.choice(RANDOM_ANSWER)
logger.info("Returning phrase '%s'", resp)
return resp
def token_sentence(sentence):
pronoun = None
noun = None
verb = None
adjective = None
for s in sentence.sentences:
pronoun = find_pronoun(s)
noun = find_noun(s)
verb = find_verb(s)
adjective = find_adj(s)
return pronoun, noun, adjective, verb
if __name__ == '__main__':
import sys
if len(sys.argv) > 0:
out = sys.argv[1]
else:
out = "Issa ObamaBot"
print(obama_reply(out))