Last active
February 25, 2023 18:41
-
-
Save ramsrigouthamg/5ab64c56d87019c5693b67f1627b750f to your computer and use it in GitHub Desktop.
Generate MCQ Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import re | |
import random | |
from pywsd.similarity import max_similarity | |
from pywsd.lesk import adapted_lesk | |
from pywsd.lesk import simple_lesk | |
from pywsd.lesk import cosine_lesk | |
from nltk.corpus import wordnet as wn | |
# Distractors from Wordnet | |
def get_distractors_wordnet(syn,word): | |
distractors=[] | |
word= word.lower() | |
orig_word = word | |
if len(word.split())>0: | |
word = word.replace(" ","_") | |
hypernym = syn.hypernyms() | |
if len(hypernym) == 0: | |
return distractors | |
for item in hypernym[0].hyponyms(): | |
name = item.lemmas()[0].name() | |
#print ("name ",name, " word",orig_word) | |
if name == orig_word: | |
continue | |
name = name.replace("_"," ") | |
name = " ".join(w.capitalize() for w in name.split()) | |
if name is not None and name not in distractors: | |
distractors.append(name) | |
return distractors | |
def get_wordsense(sent,word): | |
word= word.lower() | |
if len(word.split())>0: | |
word = word.replace(" ","_") | |
synsets = wn.synsets(word,'n') | |
if synsets: | |
wup = max_similarity(sent, word, 'wup', pos='n') | |
adapted_lesk_output = adapted_lesk(sent, word, pos='n') | |
lowest_index = min (synsets.index(wup),synsets.index(adapted_lesk_output)) | |
return synsets[lowest_index] | |
else: | |
return None | |
# Distractors from http://conceptnet.io/ | |
def get_distractors_conceptnet(word): | |
word = word.lower() | |
original_word= word | |
if (len(word.split())>0): | |
word = word.replace(" ","_") | |
distractor_list = [] | |
url = "http://api.conceptnet.io/query?node=/c/en/%s/n&rel=/r/PartOf&start=/c/en/%s&limit=5"%(word,word) | |
obj = requests.get(url).json() | |
for edge in obj['edges']: | |
link = edge['end']['term'] | |
url2 = "http://api.conceptnet.io/query?node=%s&rel=/r/PartOf&end=%s&limit=10"%(link,link) | |
obj2 = requests.get(url2).json() | |
for edge in obj2['edges']: | |
word2 = edge['start']['label'] | |
if word2 not in distractor_list and original_word.lower() not in word2.lower(): | |
distractor_list.append(word2) | |
return distractor_list | |
key_distractor_list = {} | |
for keyword in keyword_sentence_mapping: | |
wordsense = get_wordsense(keyword_sentence_mapping[keyword][0],keyword) | |
if wordsense: | |
distractors = get_distractors_wordnet(wordsense,keyword) | |
if len(distractors) ==0: | |
distractors = get_distractors_conceptnet(keyword) | |
if len(distractors) != 0: | |
key_distractor_list[keyword] = distractors | |
else: | |
distractors = get_distractors_conceptnet(keyword) | |
if len(distractors) != 0: | |
key_distractor_list[keyword] = distractors | |
index = 1 | |
print ("#############################################################################") | |
print ("NOTE:::::::: Since the algorithm might have errors along the way, wrong answer choices generated might not be correct for some questions. ") | |
print ("#############################################################################\n\n") | |
for each in key_distractor_list: | |
sentence = keyword_sentence_mapping[each][0] | |
pattern = re.compile(each, re.IGNORECASE) | |
output = pattern.sub( " _______ ", sentence) | |
print ("%s)"%(index),output) | |
choices = [each.capitalize()] + key_distractor_list[each] | |
top4choices = choices[:4] | |
random.shuffle(top4choices) | |
optionchoices = ['a','b','c','d'] | |
for idx,choice in enumerate(top4choices): | |
print ("\t",optionchoices[idx],")"," ",choice) | |
print ("\nMore options: ", choices[4:20],"\n\n") | |
index = index + 1 | |
@shreykul Where you able to get a soultion for this?
@nithin13231323 I was able to run it at that time but I don't remember now how i did it... I'll try again and see if it works now.
@shreykul ok let me know if you got the solution for that I think it’s some problem with the limit of keywords on the text or something just let me know if you find it
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hii, I was trying this code for generating mcq but I don't know why it is generating an error and I couldn't find an answer to that. I'll be really glad if you could help me solve it.
IndexError Traceback (most recent call last)
Input In [111], in <cell line: 72>()
key_distractor_list = {}
for keyword in keyword_sentence_mapping:
---> wordsense = get_wordsense(keyword_sentence_mapping[keyword][0],keyword)
if wordsense:
distractors = get_distractors_wordnet(wordsense,keyword)
IndexError: list index out of range