-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnew_comprehension.parser.py
86 lines (70 loc) · 2.53 KB
/
new_comprehension.parser.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
import csv
def loadMetaFile(location):
meta = []
with open(location, 'r') as f:
reader = csv.reader(f, delimiter=',')
for x, row in enumerate(reader):
if (x > 0):
correct_ans = row[4]
meta.append([correct_ans])
f.close()
print("meta file loaded")
return meta
def getCorrAns(questionNum):
return meta[questionNum-1]
def handleCompData(questionNum, question_ids, comp_dict):
Corr_Ans = getCorrAns(questionNum)
correct_ans = Corr_Ans[0]
comp_dict["QUESTION_NBR"] = questionNum
comp_dict["QUESTION_ID"] = question_ids
comp_dict["CORR_ANS"] = correct_ans
comp_dict["RESPONSES"] = comp_dict
return comp_dict
def getCompData(answer):
if "Eat" in answer:
return "RESPONSE"
elif "None" in answer:
return "NONE"
def loadComprehensionFile(location):
with open(location, 'r') as f:
reader = csv.reader(f, delimiter=',')
answers = []
responses = []
for x, row in enumerate(reader):
if (x == 0):
question_ids = row
elif (x > 0):
responseTotal = 0
questionNum = 0
answers = row
comp_dict = {}
for i, item in enumerate(row):
if (i != 0):
questionNum = questionNum + 1
if(comp_dict != {}):
comp_dict = handleCompData(
questionNum, question_ids, comp_dict)
responses.append(comp_dict)
comp_dict = {}
if (item != ''):
answer = answers[i]
comp_dictType = getCompData(answer)
comp_dict[comp_dictType] = item
responseTotal = responseTotal + 1
print("file formatted")
# print(f'Total responses for subject {x} : ', responseTotal)
return responses
def writeCompDict(responses):
fieldNames = ["QUESTION_NBR", "CORR_ANS", "RESPONSE", "NONE"]
with open('comprehension_test.csv', 'w') as f:
writer = csv.DictWriter(
f, fieldnames=fieldNames, extrasaction='ignore')
writer.writeheader()
for response in responses:
if (response != ''):
writer.writerow(response)
f.close()
print("file written")
meta = loadMetaFile("meta.csv")
responses = loadComprehensionFile("Comprehension_copy.csv")
writeCompDict(responses)