-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcqutils.py
111 lines (96 loc) · 3.39 KB
/
mcqutils.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
import PyPDF2
import json
import traceback
import re
import numpy as np
import os
def parse_file(input_path):
# if file.name.endswith(".pdf"):
# try:
# with open(file, 'r', encoding='utf-8') as file:
# content = file.read()
# # Use regex to find sentences
# sentences = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s', content)
# return sentences
# finally :
# raise Exception("Error reading the md file.")
# if file.name.endswith(".md"):
# return file.read().decode("utf-8")
if input_path !="":
with open(input_path, 'r',encoding="utf-8") as file:
return file.read()
else:
raise Exception(
"Unsupported file format. Only .md files are supported."
)
def process_directory(input_directory):
for root, dirs, files in os.walk(input_directory):
for file in files:
if file.endswith(".md"):
input_path = os.path.join(root, file)
# Process the .md file
processed_data = parse_file(input_path)
# Output path in the same directory with ".json" extension
output_path = os.path.join(root, f"{os.path.splitext(file)[0]}.json")
# Write the processed data to the output file
with open(output_path, 'w', encoding='utf-8') as output_file:
json.dump(processed_data, output_file, ensure_ascii=False, indent=2)
def get_table_data(quiz_str):
try:
# convert the quiz from a str to dict
quiz_dict = json.loads(quiz_str)
quiz_table_data = []
# Iterate over the quiz dictionary and extract the required information
for key, value in quiz_dict.items():
mcq = value["mcq"]
options = {
f"{option}": f"{option_value}"
for option, option_value in value["options"].items()
}
dictionary = []
# for i in range(len(value["options"].items())):
# dictionary[option[i]] = option_value[i]
# dic=[]
# for option, option_value in value["options"].items():
# dic.append(option : option_value})
correct = value["correct"]
quiz_table_data.append({"MCQ": mcq, "Choices": options, "Correct": correct})
return quiz_table_data
except Exception as e:
traceback.print_exception(type(e), e, e.__traceback__)
return False
RESPONSE_JSON = {
"1": {
"no": "1",
"mcq": "multiple choice question",
"options": {
"a": "choice here",
"b": "choice here",
"c": "choice here",
"d": "choice here",
},
"correct": "correct answer",
},
"2": {
"no": "2",
"mcq": "multiple choice question",
"options": {
"a": "choice here",
"b": "choice here",
"c": "choice here",
"d": "choice here",
},
"correct": "correct answer",
},
"3": {
"no": "3",
"mcq": "multiple choice question",
"options": {
"a": "choice here",
"b": "choice here",
"c": "choice here",
"d": "choice here",
},
"correct": "correct answer",
},
}