-
Notifications
You must be signed in to change notification settings - Fork 1
/
datamuse.py
50 lines (39 loc) · 1.16 KB
/
datamuse.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
import requests
url = 'http://api.datamuse.com'
maxWords = 100
#vocab = 'en' #maybe use 'enwiki' instead?
def spaceToPlus(w):
return w.replace(' ','+')
def meansLike(w):
return 'ml='+spaceToPlus(w)
def soundsLike(w):
return 'sl='+spaceToPlus(w)
def spelledLike(w):
return 'sp='+spaceToPlus(w)
#rel={jja, jjb, syn, trg, ant, spc, gen, com, par, bga, bgb, rhy, nry, hom, cns} see API
def related(rel,w):
return 'rel_'+rel+'='+spaceToPlus(w)
#At most 5 words can be specified. Space or comma delimited. Nouns work best.
def topics(t):
return 'topics='+t
def leftContext(w):
return 'lc='+spaceToPlus(w)
def rightContext(w):
return 'rc='+spaceToPlus(w)
#any combo of d,p,s,r,f
def metadata(m):
return 'md='+m
#returns array for word/metadata tuple in the order the API returned them (usu. sorted by score)
def query(*args):
ret = []
u = url+'/words?max='+str(maxWords)+'&' + '&'.join(str(a) for a in args)
r = requests.get(u)
if r.status_code != 200:
print 'DATA MUSE RETURNED',r.status_code
return ret
obj = r.json()
for o in obj:
if 'score' not in o:
o['score']=0
ret.append((o['word'],o))
return ret