-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate_distractors_conceptnet.py
35 lines (29 loc) · 1.09 KB
/
generate_distractors_conceptnet.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
# -*- coding: utf-8 -*-
"""Generate_Distractors_Conceptnet.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1VmMbK0FxYOJULWKJapr0cOYVuBrBbZoe
"""
import requests
import json
import re
import random
import pprint
# 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