| 1 |
(i_word, i_tag) = (0, 1) |
|---|
| 2 |
(i_backward, i_forward) = (0, 1) |
|---|
| 3 |
from random import choice |
|---|
| 4 |
|
|---|
| 5 |
class QuestionHandler: |
|---|
| 6 |
@staticmethod |
|---|
| 7 |
def answer_what(brain, screenname, question, log): |
|---|
| 8 |
tagged = -1 |
|---|
| 9 |
|
|---|
| 10 |
for index, tuple in enumerate(question): |
|---|
| 11 |
if "VB" in tuple[i_tag]: |
|---|
| 12 |
tagged = index |
|---|
| 13 |
elif tagged > 0: |
|---|
| 14 |
break |
|---|
| 15 |
|
|---|
| 16 |
sentences = [] |
|---|
| 17 |
for index, word in enumerate(question): |
|---|
| 18 |
if not brain.d.has_node(word): |
|---|
| 19 |
continue |
|---|
| 20 |
takenCareOf = 0 |
|---|
| 21 |
if brain.is_word_nva(word): |
|---|
| 22 |
for existing_sentence in sentences: |
|---|
| 23 |
if brain.d.edge(existing_sentence[0],word,0) or brain.d.edge(word,existing_sentence[0],0): |
|---|
| 24 |
takenCareOf = 1 |
|---|
| 25 |
if takenCareOf == 0: |
|---|
| 26 |
sentences.append([word]) |
|---|
| 27 |
|
|---|
| 28 |
half_answers = brain.generate_ngrams(sentences, brain.d, screenname, i_backward) |
|---|
| 29 |
full_answers = brain.generate_ngrams(half_answers, brain.d, screenname, i_forward) |
|---|
| 30 |
|
|---|
| 31 |
sentenceChoices = [sentence for sentence in full_answers if sentence[-1] == brain.end and not sentence[-2][i_word] == '?'] |
|---|
| 32 |
|
|---|
| 33 |
if len(sentenceChoices) == 0: |
|---|
| 34 |
return QuestionHandler.unsure() |
|---|
| 35 |
|
|---|
| 36 |
maxWeight = -1.0 |
|---|
| 37 |
for sentenceChoice in sentenceChoices: |
|---|
| 38 |
weight = 0.0 |
|---|
| 39 |
for tuple in sentenceChoice: |
|---|
| 40 |
for question_tuple in question: |
|---|
| 41 |
if question_tuple == tuple: |
|---|
| 42 |
weight += 1.0 |
|---|
| 43 |
elif brain.a.has_edge(tuple, question_tuple): |
|---|
| 44 |
weight += brain.compute_word_associativity(tuple, question_tuple) |
|---|
| 45 |
|
|---|
| 46 |
if weight > maxWeight: |
|---|
| 47 |
maxWeight = weight |
|---|
| 48 |
bestResponse = sentenceChoice |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
if(bestResponse[1][i_word] == "is" and bestResponse[2][i_word] == "it"): |
|---|
| 52 |
temp = bestResponse[1] |
|---|
| 53 |
bestResponse[1] = bestResponse[2] |
|---|
| 54 |
bestResponse[2] = temp |
|---|
| 55 |
|
|---|
| 56 |
return bestResponse |
|---|
| 57 |
|
|---|
| 58 |
@staticmethod |
|---|
| 59 |
def answer_vbnnvb(brain, screenname, question, log): |
|---|
| 60 |
tagged = -1 |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
interjections = 0; |
|---|
| 64 |
for interindex, word in enumerate(question): |
|---|
| 65 |
if word[i_word] in ["oh", "uh", "well", "huh", "now"] or ',' in word[i_tag]: |
|---|
| 66 |
interjections = interjections + 1; |
|---|
| 67 |
continue |
|---|
| 68 |
else: |
|---|
| 69 |
break |
|---|
| 70 |
|
|---|
| 71 |
modifiedquestion = question[interjections:]; |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
for smileindex, word in enumerate(modifiedquestion): |
|---|
| 76 |
if word[i_word] in [":", "^", "~", "-", ";"] and smileindex < 4: |
|---|
| 77 |
return QuestionHandler.smilies(); |
|---|
| 78 |
if smileindex >= 4: |
|---|
| 79 |
break |
|---|
| 80 |
|
|---|
| 81 |
for tagindex, tuple in enumerate(modifiedquestion): |
|---|
| 82 |
if tagindex < 3: |
|---|
| 83 |
continue |
|---|
| 84 |
if "NN" in tuple[i_tag] or "NNS" in tuple[i_tag] or "NNP" in tuple[i_tag] or "NNPS" in tuple[i_tag] or "PRP" in tuple[i_tag] or "PRP$" in tuple[i_tag]: |
|---|
| 85 |
tagged = tagindex |
|---|
| 86 |
elif tagged > 0: |
|---|
| 87 |
break |
|---|
| 88 |
|
|---|
| 89 |
sentences = [] |
|---|
| 90 |
if tagged > 0: |
|---|
| 91 |
if brain.d.has_node(modifiedquestion[tagged]): |
|---|
| 92 |
sentences.append([modifiedquestion[tagged]]) |
|---|
| 93 |
for index, branch in brain.d.successors(modifiedquestion[tagged]): |
|---|
| 94 |
takenCareOf = 0 |
|---|
| 95 |
if brain.is_word_nva(branch): |
|---|
| 96 |
for existing_sentence in sentences: |
|---|
| 97 |
if brain.d.edge(existing_sentence[0],branch,0) or brain.d.edge(branch,existing_sentence[0],0): |
|---|
| 98 |
takenCareOf = 1 |
|---|
| 99 |
if takenCareOf == 0: |
|---|
| 100 |
sentences.append([branch]) |
|---|
| 101 |
for index, branch in brain.d.predecessors(modifiedquestion[tagged]): |
|---|
| 102 |
takenCareOf = 0 |
|---|
| 103 |
if brain.is_word_nva(branch): |
|---|
| 104 |
for existing_sentence in sentences: |
|---|
| 105 |
if brain.d.edge(existing_sentence[0],branch,0) or brain.d.edge(branch,existing_sentence[0],0): |
|---|
| 106 |
takenCareOf = 1 |
|---|
| 107 |
if takenCareOf == 0: |
|---|
| 108 |
sentences.append([branch]) |
|---|
| 109 |
else: |
|---|
| 110 |
return QuestionHandler.unsure() |
|---|
| 111 |
|
|---|
| 112 |
half_answers = brain.generate_ngrams(sentences, brain.d, screenname, i_backward) |
|---|
| 113 |
full_answers = brain.generate_ngrams(half_answers, brain.d, screenname, i_forward) |
|---|
| 114 |
|
|---|
| 115 |
sentenceChoices = [sentence for sentence in full_answers if sentence[-1] == brain.end and not sentence[-2][i_word] == '?'] |
|---|
| 116 |
|
|---|
| 117 |
print sentenceChoices |
|---|
| 118 |
|
|---|
| 119 |
if len(sentenceChoices) == 0: |
|---|
| 120 |
return QuestionHandler.negative() |
|---|
| 121 |
|
|---|
| 122 |
maxWeight = -1.0 |
|---|
| 123 |
for sentenceChoice in sentenceChoices: |
|---|
| 124 |
weight = 0.0 |
|---|
| 125 |
for tuple in sentenceChoice: |
|---|
| 126 |
for question_tuple in modifiedquestion: |
|---|
| 127 |
if question_tuple == tuple: |
|---|
| 128 |
weight += 1.0 |
|---|
| 129 |
elif brain.a.has_edge(tuple, question_tuple): |
|---|
| 130 |
weight += brain.compute_word_associativity(tuple, question_tuple) |
|---|
| 131 |
|
|---|
| 132 |
if weight > maxWeight: |
|---|
| 133 |
maxWeight = weight |
|---|
| 134 |
bestResponse = sentenceChoice |
|---|
| 135 |
|
|---|
| 136 |
modifiedResponse = []; |
|---|
| 137 |
if len(sentenceChoices) == 1: |
|---|
| 138 |
modifiedResponse = QuestionHandler.positive(); |
|---|
| 139 |
else: |
|---|
| 140 |
modifiedResponse = [('yes', 'UH'),(',',',')]+bestResponse[1:] |
|---|
| 141 |
|
|---|
| 142 |
return modifiedResponse |
|---|
| 143 |
|
|---|
| 144 |
@staticmethod |
|---|
| 145 |
def answer_isare(brain, screenname, question, log): |
|---|
| 146 |
tagged = -1 |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
interjections = 0; |
|---|
| 150 |
for interindex, word in enumerate(question): |
|---|
| 151 |
if word[i_word] in ["oh", "uh", "well", "huh", "now"] or ',' in word[i_tag]: |
|---|
| 152 |
interjections = interjections + 1; |
|---|
| 153 |
continue |
|---|
| 154 |
else: |
|---|
| 155 |
break |
|---|
| 156 |
|
|---|
| 157 |
modifiedquestion = question[interjections:]; |
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
for smileindex, word in enumerate(modifiedquestion): |
|---|
| 162 |
if word[i_word] in [":", "^", "~", "-", ";"] and smileindex < 4: |
|---|
| 163 |
return QuestionHandler.smilies(); |
|---|
| 164 |
if smileindex >= 4: |
|---|
| 165 |
break |
|---|
| 166 |
|
|---|
| 167 |
for tagindex, tuple in enumerate(modifiedquestion): |
|---|
| 168 |
if tagindex < 1: |
|---|
| 169 |
continue |
|---|
| 170 |
if "NN" in tuple[i_tag] or "NNS" in tuple[i_tag] or "NNP" in tuple[i_tag] or "NNPS" in tuple[i_tag] or "PRP" in tuple[i_tag] or "PRP$" in tuple[i_tag]: |
|---|
| 171 |
tagged = tagindex |
|---|
| 172 |
elif tagged > 0: |
|---|
| 173 |
break |
|---|
| 174 |
|
|---|
| 175 |
sentences = [] |
|---|
| 176 |
if tagged > 0: |
|---|
| 177 |
if brain.d.has_node(modifiedquestion[tagged]): |
|---|
| 178 |
sentences.append([modifiedquestion[tagged]]) |
|---|
| 179 |
for index, branch in brain.d.successors(modifiedquestion[tagged]): |
|---|
| 180 |
takenCareOf = 0 |
|---|
| 181 |
if brain.is_word_nva(branch): |
|---|
| 182 |
for existing_sentence in sentences: |
|---|
| 183 |
if brain.d.edge(existing_sentence[0],branch,0) or brain.d.edge(branch,existing_sentence[0],0): |
|---|
| 184 |
takenCareOf = 1 |
|---|
| 185 |
if takenCareOf == 0: |
|---|
| 186 |
sentences.append([branch]) |
|---|
| 187 |
for index, branch in brain.d.predecessors(modifiedquestion[tagged]): |
|---|
| 188 |
takenCareOf = 0 |
|---|
| 189 |
if brain.is_word_nva(branch): |
|---|
| 190 |
for existing_sentence in sentences: |
|---|
| 191 |
if brain.d.edge(existing_sentence[0],branch,0) or brain.d.edge(branch,existing_sentence[0],0): |
|---|
| 192 |
takenCareOf = 1 |
|---|
| 193 |
if takenCareOf == 0: |
|---|
| 194 |
sentences.append([branch]) |
|---|
| 195 |
else: |
|---|
| 196 |
return QuestionHandler.unsure() |
|---|
| 197 |
|
|---|
| 198 |
half_answers = brain.generate_ngrams(sentences, brain.d, screenname, i_backward) |
|---|
| 199 |
full_answers = brain.generate_ngrams(half_answers, brain.d, screenname, i_forward) |
|---|
| 200 |
|
|---|
| 201 |
sentenceChoices = [sentence for sentence in full_answers if sentence[-1] == brain.end and not sentence[-2][i_word] == '?'] |
|---|
| 202 |
|
|---|
| 203 |
print sentenceChoices |
|---|
| 204 |
|
|---|
| 205 |
if len(sentenceChoices) == 0: |
|---|
| 206 |
return QuestionHandler.negative() |
|---|
| 207 |
|
|---|
| 208 |
maxWeight = -1.0 |
|---|
| 209 |
for sentenceChoice in sentenceChoices: |
|---|
| 210 |
weight = 0.0 |
|---|
| 211 |
for tuple in sentenceChoice: |
|---|
| 212 |
for question_tuple in modifiedquestion: |
|---|
| 213 |
if question_tuple == tuple: |
|---|
| 214 |
weight += 1.0 |
|---|
| 215 |
elif brain.a.has_edge(tuple, question_tuple): |
|---|
| 216 |
weight += brain.compute_word_associativity(tuple, question_tuple) |
|---|
| 217 |
|
|---|
| 218 |
if weight > maxWeight: |
|---|
| 219 |
maxWeight = weight |
|---|
| 220 |
bestResponse = sentenceChoice |
|---|
| 221 |
|
|---|
| 222 |
modifiedResponse = []; |
|---|
| 223 |
if len(sentenceChoices) == 1: |
|---|
| 224 |
modifiedResponse = QuestionHandler.positive(); |
|---|
| 225 |
else: |
|---|
| 226 |
if(bestResponse[1][i_word] == "is" and bestResponse[2][i_word] == "it"): |
|---|
| 227 |
temp = bestResponse[1] |
|---|
| 228 |
bestResponse[1] = bestResponse[2] |
|---|
| 229 |
bestResponse[2] = temp |
|---|
| 230 |
modifiedResponse = QuestionHandler.confirmed()+[(',',',')]+bestResponse |
|---|
| 231 |
|
|---|
| 232 |
return modifiedResponse |
|---|
| 233 |
|
|---|
| 234 |
@staticmethod |
|---|
| 235 |
def unsure(): |
|---|
| 236 |
unsureResponses = ["uh i dunno", "umm not really sure", "hmm idk", "i dunno... do you?", "no idea", "not entirely sure", "idk, ask someone else", "hrm, no clue", |
|---|
| 237 |
"idk", "not sure", "don't really know", "beats me", "i'm uncertain about that", "i wish i knew", "don't know", "dunno", "hmmm... i don't know", "hmmm... not sure"] |
|---|
| 238 |
return choice(unsureResponses) |
|---|
| 239 |
|
|---|
| 240 |
@staticmethod |
|---|
| 241 |
def positive(): |
|---|
| 242 |
positiveResponses = ["yes", "yeah", "i believe so", "perhaps", "maybe"] |
|---|
| 243 |
return choice(positiveResponses) |
|---|
| 244 |
|
|---|
| 245 |
@staticmethod |
|---|
| 246 |
def negative(): |
|---|
| 247 |
negativeResponses = ["no", "i don't think so", "nope", "not that i'm aware"] |
|---|
| 248 |
return choice(negativeResponses) |
|---|
| 249 |
|
|---|
| 250 |
@staticmethod |
|---|
| 251 |
def smilies(): |
|---|
| 252 |
negativeResponses = ["uh, what is that supposed to mean", ">.>", "sigh", "please stop joking around", "*facepalm*", "how wonderful"] |
|---|
| 253 |
return choice(negativeResponses) |
|---|
| 254 |
|
|---|
| 255 |
@staticmethod |
|---|
| 256 |
def confirmed(): |
|---|
| 257 |
confirmedResponses = [[('yes', 'UH')],[('i','UH'),('think', 'UH'),('so','UH')],[('yeah', 'UH')],[('uh', 'UH'),('huh','UH')]] |
|---|
| 258 |
return choice(confirmedResponses) |
|---|
| 259 |
|
|---|
| 260 |
@staticmethod |
|---|
| 261 |
def answer_other(brain, screenname, sentence): |
|---|
| 262 |
for index in range (0, len(sentence) - 2): |
|---|
| 263 |
if ('NN' in sentence[index][i_tag] and 'VB' in sentence[index + 1][i_tag]): |
|---|
| 264 |
tmp = sentence[index] |
|---|
| 265 |
sentence[index] = sentence[index + 1] |
|---|
| 266 |
sentence[index + 1] = tmp |
|---|
| 267 |
break |
|---|
| 268 |
|
|---|
| 269 |
return QuestionHandler.unsure() |
|---|
| 270 |
|
|---|
| 271 |
|
|---|