-
Notifications
You must be signed in to change notification settings - Fork 0
/
HQ.py
executable file
·273 lines (199 loc) · 6.38 KB
/
HQ.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
`#!/usr/bin/env python3
import time
import sys
import requests, json, io, os
from google.cloud import vision
from google.cloud.vision import types
import threading
api_key = "google cloud api key"
search_engine_id = "google search engine id"
#used to track how much time methods take
start_time = time.time()
a1 = 0
a2 = 0
a3 = 0
b1 = 0
b2 = 0
b3 = 0
c1 = 0
c2 = 0
c3 = 0
a_results = 0
b_results = 0
c_results = 0
def main():
file_name = "/Users/davidp/Desktop/screen_cap.png"
text = detect_text(file_name)
[unfiltered_question, answers] = parse_text(text)
words = word_tokenize(unfiltered_question)
question_list = [w for w in words if not w in stop_words]
question = ""
for word in question_list:
question += word + " "
wordsA = word_tokenize(answers[0])
answerA_list = [w for w in wordsA if not w in stop_words]
ans_A = ""
for word in answerA_list:
ans_A += word + " "
answers[0] = ans_A
wordsB = word_tokenize(answers[1])
answerB_list = [w for w in wordsB if not w in stop_words]
ans_B = ""
for word in answerB_list:
ans_B += word + " "
answers[1] = ans_B
wordsC = word_tokenize(answers[2])
answerC_list = [w for w in wordsC if not w in stop_words]
ans_C = ""
for word in answerC_list:
ans_C += word + " "
answers[2] = ans_C
print("--- %s seconds ---" % (time.time() - get_text ))
print(question)
[a_count, b_count, c_count] = googleQuestion(question, answers)
print(answers[0] ,": ", a_count)
print(answers[1] ,": ", b_count)
print(answers[2] ,": ", c_count)
print("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=")
t1 = threading.Thread(target = tallyResults, args =(0, answers, question))
t2 = threading.Thread(target = tallyResults, args =(1, answers, question))
t3 = threading.Thread(target = tallyResults, args =(2, answers, question))
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()
a_w_question_count = a1 + a2 + a3
b_w_question_count = b1 + b2 + b3
c_w_question_count = c1 + c2 + c3
print(answers[0] + " with question: " + str(a_w_question_count))
print(answers[1] + " with question: " + str(b_w_question_count))
print(answers[2] + " with question: " + str(c_w_question_count))
print("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=")
print(answers[0] + " total results: " + str(a_results))
print(answers[1] + " total results: " + str(b_results))
print(answers[2] + " total results: " + str(c_results))
print("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=")
[a_conf, b_conf, c_conf] = calcConfidence(a_count, b_count, c_count,
a_w_question_count, b_w_question_count,c_w_question_count,
int(a_results), int(b_results), int(c_results))
print(answers[0] + " Confidence: " + str(a_conf) + "%")
print(answers[1] + " Confidence: " + str(b_conf) + "%")
print(answers[2] + " Confidence: " + str(c_conf) + "%")
def detect_text(path):
"""Detects text in the file."""
client = vision.ImageAnnotatorClient()
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
response = client.text_detection(image=image)
texts = response.text_annotations
page = ""
page = texts[0].description
return page
def parse_text(text):
lst = text.split("?")
questionParts = lst[0].splitlines()
question = ""
for i in questionParts:
question += i + " "
question += "?"
#print(question)
wordList = lst[1].splitlines()
answers = []
for i, l in enumerate(wordList):
if len(l) == 0:
continue
elif l == " ":
continue
else:
answers.append(l)
#print(answers)
return question, answers
def googleQuestion(question, answers):
snippet = buildSnippet(question)
[a, b, c] = countChoices(answers, snippet)
return a, b, c
def calcConfidence(ac, bc, cc, awqc, bwqc, cwqc, ar, br, cr):
totalc = ac + bc + cc + 1
totalcwq = awqc + bwqc + cwqc + 1
totalr = ar + br + cr + 1
count_weights = 40
count_with_question_weights = 40
result_weights = 20
a_conf = ((ac/totalc) * count_weights) + ((awqc/totalcwq) * count_with_question_weights) + ((ar/totalr) * result_weights)
b_conf = ((bc/totalc) * count_weights) + ((bwqc/totalcwq) * count_with_question_weights) + ((br/totalr) * result_weights)
c_conf = ((cc/totalc) * count_weights) + ((cwqc/totalcwq) * count_with_question_weights) + ((cr/totalr) * result_weights)
return int(a_conf), int(b_conf), int(c_conf)
def buildSnippet(question):
snippet = ""
questionTick = requests.get('https://www.googleapis.com/customsearch/v1?key=' +api_key+ '&cx=' +search_engine_id+ '&q=' +question)
if (questionTick.status_code == 200):
theJSON = questionTick.json()
if "items" in theJSON:
for i in theJSON["items"]:
if "title" in i:
s1 = i["title"].encode('utf-8')
b_a = str(s1)
[throw1, add1] = b_a.split("'",1)
snippet += "\n" + add1
if "snippet" in i:
s2 = i["snippet"].encode('utf-8')
b_b = str(s2)
[throw2, add2] = b_b.split("'",1)
snippet += "\n" + add2
else:
print("No snippet")
return snippet
else:
print("no items")
else:
print("Recieved an Error from server, cannot retrieve results " + str(questionTick.status_code))
def countChoices(answers, snippet):
a = snippet.upper().count(answers[0].upper())
b = snippet.upper().count(answers[1].upper())
c = snippet.upper().count(answers[2].upper())
return a, b, c
def totalResults(query):
snippet = ""
tick = requests.get('https://www.googleapis.com/customsearch/v1?key=' +api_key+ '&cx=' +search_engine_id+ '&q=' +query)
theJSON = tick.json()
if "queries" in theJSON:
if "request" in theJSON["queries"]:
if "totalResults" in theJSON["queries"]["request"][0]:
results = theJSON["queries"]["request"][0]["totalResults"]
if "items" in theJSON:
for i in theJSON["items"]:
if "title" in i:
s1 = i["title"].encode('utf-8')
b_a = str(s1)
[throw1, add1] = b_a.split("'",1)
snippet += "\n" + add1
if "snippet" in i:
s2 = i["snippet"].encode('utf-8')
b_b = str(s2)
[throw2, add2] = b_b.split("'",1)
snippet += "\n" + add2
return results, snippet
def tallyResults(index, answers, question):
question_and_choice = question + ' "' + answers[index] + '"'
[results, snippet] = totalResults(question_and_choice)
[a, b, c] = countChoices(answers, snippet)
global a1, a2, a3, b1, b2, b3, c1, c2, c3, a_results, b_results, c_results
if index == 0:
a_results = results
a1 = a
b1 = b
c1 = c
elif index == 1:
b_results = results
a2 = a
b2 = b
c2 = c
else:
c_results = results
a3 = a
b3 = b
c3 = c
if __name__ == "__main__": main()