-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseparation_quest_ans.py
46 lines (37 loc) · 1.38 KB
/
separation_quest_ans.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
#---Using Regex to retrieve questions and answers
import re
import json
def getQuestionAndOption():
hand = open('Scraped_Text.txt',"r")
# #print(hand.readlines())
# quest = [1,2,3,4,5,6,7]
# rows, cols = (50, 3)
qno = dict()
answers = dict()
i=0
for line in hand:
line = line.rstrip()
if re.search('^[1-9]\. .*', line) or re.search('^[1-9][0-9]\. .*', line):
i=i+1
ques = "<b>"+line[3:].strip()+"</b>"
qno[ques] = list()
answers[ques] = ""
if re.search('^[a-d]\).*', line):
qno[ques].append(line)
if re.search('^View Answer Answer:.*', line) or re.search('^View AnswerAnswer:.*', line):
answers[ques] = re.sub('[^a-zA-Z]+', '', line[19:22])
def filterTheDict(dictObj, callback):
newDict = dict()
for (key, value) in dictObj.items():
if callback((key, value)):
newDict[key] = value
return newDict
new_qno = filterTheDict(qno, lambda elem : "following" not in elem[0])
new_answers = filterTheDict(answers, lambda elem : "following" not in elem[0])
my_list = []
my_list.append(new_qno)
my_list.append(new_answers)
return my_list
#print(json.dumps(new_qno, indent=1))
#print(json.dumps(new_answers, indent=1))
#getQuestionAndOption()