forked from Nockiro/Duolingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_answer_check.py
59 lines (51 loc) · 2.52 KB
/
user_answer_check.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
import re
class UserAnswerCheck():
def __init__(self, user_input):
self.choices_regex = re.compile("\[(.*?)\]")
self.punct_regex = re.compile("[\.\?\!\,\;\-]")
self.user_input = re.sub(self.punct_regex, "", user_input).lower().split()
def checkAnswer(self, solutions):
for possible_solution in solutions:
possible_solution = re.sub(self.punct_regex, "", possible_solution).lower()
choices = re.findall(self.choices_regex, possible_solution)
answer, temp_choice = [], []
position = 0
correct_answer_flag = False
for choicePoisition in range(len(choices)):
possible_solution = re.sub(self.choices_regex, str(choicePoisition), possible_solution, 1)
possible_solution = possible_solution.split()
for word in possible_solution:
if word.isdigit():
for choice in choices[int(word)].split("/"):
words_in_choice = choice.split()
correct_choice_flag = True
correct_answer_flag = True
for word_in_choice in words_in_choice:
try:
if word_in_choice == self.user_input[position+words_in_choice.index(word_in_choice)] or word_in_choice == "":
temp_choice.append(word_in_choice)
else:
temp_choice = []
correct_choice_flag = False
except IndexError:
correct_answer_flag = False
if correct_choice_flag:
answer.extend(temp_choice)
position += len(temp_choice)
temp_choice = []
break
else:
correct_answer_flag = False
else:
try:
if word == self.user_input[position]:
answer.append(word)
position += 1
correct_answer_flag = True
else:
correct_answer_flag = False
except IndexError:
correct_answer_flag = False
if correct_answer_flag:
return True
return False