-
Notifications
You must be signed in to change notification settings - Fork 16
/
solver.py
199 lines (157 loc) · 7.89 KB
/
solver.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
from config import readFromConfig
from solvers.naive import Naive
from solvers.gsearch import Google
from solvers.wiki import Wikipedia
from queue import Queue
from shutil import get_terminal_size
from threading import Thread
class Solver(object):
def __init__(self):
self.debug = readFromConfig("General", "debug_mode")
self.google_api_key = readFromConfig("Solver", "google_api_key")
self.google_cse_id = readFromConfig("Solver", "google_cse_id")
self.show_advancing_players = readFromConfig("Solver", "show_advancing_players")
self.show_answers = readFromConfig("Solver", "show_answers")
self.show_answerids = readFromConfig("Solver", "show_answerids")
self.show_category = readFromConfig("Solver", "show_category")
self.show_eliminated_players = readFromConfig("Solver", "show_eliminated_players")
self.show_summary = readFromConfig("Solver", "show_summary")
self.show_players_answers = readFromConfig("Solver", "show_players_answers")
self.show_question = readFromConfig("Solver", "show_question")
self.show_questionid = readFromConfig("Solver", "show_questionid")
self.show_question_count = readFromConfig("Solver", "show_question_count")
self.show_question_number = readFromConfig("Solver", "show_question_number")
self.use_naive = readFromConfig("Solver", 'use_naive')
self.use_google = readFromConfig("Solver", "use_google")
self.use_wiki = readFromConfig("Solver", "use_wiki")
if self.use_naive:
self.naive = Naive(self.google_api_key, self.google_cse_id, self.debug)
if self.use_google:
self.google = Google(self.debug)
if self.use_wiki:
self.wiki = Wikipedia(self.debug)
def solve(self, message):
self.question = message
if self.question["type"] == "question":
self.showQuestion()
answers = self.question["answers"]
category = self.question["category"]
question = self.question["question"]
print(" Solvers ".center(get_terminal_size()[0], "="))
print()
solverThreads = []
queue = Queue()
if self.use_naive:
naiveThread = Thread(target=self.naive.solve, args=(question, answers, queue))
naiveThread.start()
solverThreads.append(naiveThread)
if self.use_wiki:
wikiThread = Thread(target=self.wiki.solve, args=(question, answers, category, queue))
wikiThread.start()
solverThreads.append(wikiThread)
if self.use_google:
googleThread = Thread(target=self.google.solve, args=(question, answers, queue))
googleThread.start()
solverThreads.append(googleThread)
# Join all the threads
for thread in solverThreads:
thread.join()
predictions = []
while not queue.empty():
predictions.append(queue.get())
predictionsCounter = [predictions.count(0), predictions.count(1), predictions.count(2)]
print()
if predictionsCounter.count(0) == 3:
print(" None of the solvers gave an answer! ".center(get_terminal_size()[0], "*"))
else:
mostPropably = predictionsCounter.index(max(predictionsCounter))
answer = answers[mostPropably]['text']
print((" The most propable answer is: \33[34m" + answer + " \33[0m").center(get_terminal_size()[0], "*"))
print("".center(get_terminal_size()[0], "="))
elif self.question["type"] == "questionSummary" and self.show_summary:
self.showSummary()
return True
def showQuestion(self):
print(" Question ".center(get_terminal_size()[0], "="))
print()
if self.show_question_count or self.show_question_number:
tempPrint = ""
if self.show_question_count:
tempPrint += "Question " + str(self.question["questionNumber"])
if self.show_question_count and self.show_question_number:
tempPrint += "/" + str(self.question["questionCount"])
elif self.show_question_number:
tempPrint += "Questions: " + str(self.question["questionCount"])
print(tempPrint.center(get_terminal_size()[0]))
if self.show_category:
print(("Category: " + str(self.question["category"])).rjust(get_terminal_size()[0]))
print()
if self.show_question or self.show_questionid:
tempPrint = ""
if self.show_question:
tempPrint += str(self.question["question"])
if self.show_question:
tempPrint += " (" + str(self.question["questionId"]) + ")"
elif self.show_questionid:
tempPrint += str(self.question["questionId"])
print(tempPrint.center(get_terminal_size()[0]))
if self.show_answers or self.show_answerids:
answers = self.question["answers"]
answersCount = len(answers)
for answer in range(answersCount):
tempPrint = ""
if self.show_answers:
tempPrint += str(answers[answer]["text"])
if self.show_answers and self.show_answerids:
tempPrint += " (" + str(answers[answer]["answerId"]) + ")"
elif self.show_answerids:
tempPrint += str(answers[answer]["answerId"])
print()
print(tempPrint.center(get_terminal_size()[0]))
print("".center(get_terminal_size()[0], "="))
return True
def showSummary(self):
print(" Question Summary ".center(get_terminal_size()[0], "="))
print()
if self.show_question or self.show_questionid:
tempPrint = ""
if self.show_question:
tempPrint += str(self.question["question"])
if self.show_question and self.show_questionid:
tempPrint += " (" + str(self.question["questionId"]) + ")"
elif self.show_questionid:
tempPrint += str(self.question["questionId"])
print(tempPrint.center(get_terminal_size()[0]))
print()
if self.show_answers or self.show_answerids or self.show_players_answers:
answers = self.question["answerCounts"]
answersCount = len(answers)
for answer in range(answersCount):
tempPrint = ""
answerId = str(answers[answer]["answerId"])
answerText = str(answers[answer]["answer"])
isCorrect = answers[answer]["correct"]
count = str(answers[answer]["count"])
if isCorrect:
tempPrint += "\33[32m"
else:
tempPrint += "\033[91m"
if self.show_answers:
tempPrint += answerText
if self.show_answers and self.show_answerids:
tempPrint += " (" + answerId + ")"
elif self.show_answerids:
tempPrint += answerId
tempPrint += "\33[0m"
print(tempPrint.center(get_terminal_size()[0]))
if self.show_players_answers:
print(("Players who marked this answer: " + count).center(get_terminal_size()[0]))
print()
if self.show_advancing_players:
print(("Advancing Players: " + str(self.question["advancingPlayersCount"])).center(
get_terminal_size()[0]))
if self.show_eliminated_players:
print(("Eliminated Players: " + str(self.question["eliminatedPlayersCount"])).center(
get_terminal_size()[0]))
print("".center(get_terminal_size()[0], "="))
return True