-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubjective.py
More file actions
63 lines (58 loc) · 2.23 KB
/
Copy pathsubjective.py
File metadata and controls
63 lines (58 loc) · 2.23 KB
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
import numpy as np
import nltk as nlp
class SubjectiveTest:
def __init__(self, filepath, noOfQues):
self.question_pattern = [
"Explain in detail ",
"Define ",
"Write a short note on ",
"What do you mean by "
]
self.grammar = r"""
CHUNK: {<NN>+<IN|DT>*<NN>+}
{<NN>+<IN|DT>*<NNP>+}
{<NNP>+<NNS>*}
"""
self.summary = filepath
self.noOfQues = noOfQues
@staticmethod
def word_tokenizer(sequence):
word_tokens = list()
for sent in nlp.sent_tokenize(sequence):
for w in nlp.word_tokenize(sent):
word_tokens.append(w)
return word_tokens
def generate_test(self):
sentences = nlp.sent_tokenize(self.summary)
cp = nlp.RegexpParser(self.grammar)
question_answer_dict = dict()
for sentence in sentences:
tagged_words = nlp.pos_tag(nlp.word_tokenize(sentence))
tree = cp.parse(tagged_words)
for subtree in tree.subtrees():
if subtree.label() == "CHUNK":
temp = ""
for sub in subtree:
temp += sub[0]
temp += " "
temp = temp.strip()
temp = temp.upper()
if temp not in question_answer_dict:
if len(nlp.word_tokenize(sentence)) > 20:
question_answer_dict[temp] = sentence
else:
question_answer_dict[temp] += sentence
keyword_list = list(question_answer_dict.keys())
num_questions = min(int(self.noOfQues), len(keyword_list))
que = list()
ans = list()
while len(que) < num_questions:
rand_num = np.random.randint(0, len(keyword_list))
selected_key = keyword_list[rand_num]
answer = question_answer_dict[selected_key]
question_pattern_idx = rand_num % 4
question = self.question_pattern[question_pattern_idx] + selected_key + "."
if question not in que:
que.append(question)
ans.append(answer)
return que, ans