-
Notifications
You must be signed in to change notification settings - Fork 29
/
incorrect_answer_generation.py
61 lines (50 loc) · 1.89 KB
/
incorrect_answer_generation.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
''' This module contains the class
for generating incorrect alternative
answers for a given answer
'''
import gensim
import gensim.downloader as api
from gensim.models import Word2Vec
from nltk.tokenize import sent_tokenize, word_tokenize
import random
import numpy as np
class IncorrectAnswerGenerator:
''' This class contains the methods
for generating the incorrect answers
given an answer
'''
def __init__(self, document):
# model required to fetch similar words
self.model = api.load("glove-wiki-gigaword-100")
self.all_words = []
for sent in sent_tokenize(document):
self.all_words.extend(word_tokenize(sent))
self.all_words = list(set(self.all_words))
def get_all_options_dict(self, answer, num_options):
''' This method returns a dict
of 'num_options' options out of
which one is correct and is the answer
'''
options_dict = dict()
try:
similar_words = self.model.similar_by_word(answer, topn=15)[::-1]
for i in range(1, num_options + 1):
options_dict[i] = similar_words[i - 1][0]
except BaseException:
self.all_sim = []
for word in self.all_words:
if word not in answer:
try:
self.all_sim.append(
(self.model.similarity(answer, word), word))
except BaseException:
self.all_sim.append(
(0.0, word))
else:
self.all_sim.append((-1.0, word))
self.all_sim.sort(reverse=True)
for i in range(1, num_options + 1):
options_dict[i] = self.all_sim[i - 1][1]
replacement_idx = random.randint(1, num_options)
options_dict[replacement_idx] = answer
return options_dict