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
|
'''
Updated:
- Better ways to check syntax
- Let user judge whether answer is correct
- Zero / single / plural in final output
To be updated:
- Learn answers
- Make orders not matter
- Remove unnecessary spaces or line breaks in input file
- Better ways to handle each line
- Answer judged correct if contains keyword
- Use something better than a dictionary to store incorrect answers
- Title also printed if answer in the section is incorrect
- Consider to use json input file
- Graphic user interface
- Another program to help create related input file
- Optimize
'''
try:
import re
import difflib
def check_string(line):
return bool(re.match("([-~] [\w\d\s\?\.\"\(\)\[\]\{\}!,;'/]*\Z)|(! version: \d+\.?\d*\s?\Z)|(# [\w\d\s\?\.\"\(\)\[\]\{\}!,;'/]*: [\w\d\s\?\.\"\(\)\[\]\{\}!,;'/]*\Z)", line))
def compare_string(answer, response):
diff = list(difflib.ndiff(answer, response))
diff_count = 0
for line in diff:
if line[0] != " ":
diff_count += 1
return round(1 - (diff_count / len(diff)), 4)*100
file_name = input("Enter file name (with postfix) >> ")
if file_name == "":
file_name = "test_1.2.rvwt"
with open(file_name) as f:
data = f.readlines()
count_prompts = 0
count_correct = 0
correct_param = 0.8
incorrect = []
if len(data) == 0:
raise Exception("Error: file empty")
if data.pop(0).split(".")[0].replace("\n", "") != "! version: 1":
raise Exception("Error: file version not provided or does not match")
for line_index in range(len(data)):
if not check_string(data[line_index]):
raise Exception(f"Error: invalid syntax at line {line_index+2} of file '{file_name}'")
for line_index in range(len(data)):
line = data[line_index]
if line[0] == "-":
line = line[2:].replace("\n", "")
print("\n\n- " + line + " -\n")
if line[0] == "~":
line = line[2:].replace("\n", "")
print("\n" + line + "\n")
elif line[0] == "#":
count_prompts += 1
line = line[2:].split(":")
prompt = line[0]
answer = line[1][1:-1]
response = input(prompt + ">> ")
if response.lower() == answer.lower():
print("Correct")
count_correct += 1
elif compare_string(response.lower(), answer.lower()) >= correct_param:
print(f"Probably correct: answer is '{answer}', similarity {round(compare_string(response.lower(), answer.lower()), 4)}%")
judge = input("Is your answer correct? [y/n] ").lower()
if judge == 'n':
incorrect.append((prompt, answer))
else:
count_correct += 1
if judge != "y":
print("Warning: invalid input, answer judged as correct")
else:
print(f"Probably incorrect: answer is '{answer}', similarity {round(compare_string(response.lower(), answer.lower()), 4)}%")
judge = input("Is your answer correct? [y/n] ").lower()
if judge == 'y':
count_correct += 1
else:
incorrect.append((prompt, answer))
if judge != "n":
print("Warning: invalid input, answer judged as incorrect")
else:
continue
if count_prompts <= 1:
print(f"\n\nOut of {count_prompts} question, ", end="")
else:
print(f"\n\nOut of {count_prompts} questions, ", end="")
if count_correct <= 1:
print(f"{count_correct} was answered correctly, ", end="")
else:
print(f"{count_correct} were answered correctly, ", end="")
print(f"which is {round(count_correct / count_prompts, 4)*100}%. ")
if count_prompts == count_correct:
print("Congratulations for finishing perfectly! ")
else:
if count_prompts - count_correct <= 1:
print(f"The question with incorrect response is listed below: \n")
else:
print(f"The questions with incorrect responses are listed below: \n")
for line in incorrect:
(prompt, answer) = line
print(prompt + "-> " + answer)
except Exception as e:
print("\nException: " + str(e))
except KeyboardInterrupt:
print("\nProcess quitted")
|