-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatamuse.py
57 lines (52 loc) · 1.47 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
51
52
53
54
55
56
57
import json
import requests
class Datamuse():
def __init__(self):
self.api_root = 'https://api.datamuse.com'
self.word_params = {
'ml',
'sl',
'sp',
'rel_jja',
'rel_jjb',
'rel_syn',
'rel_ant',
'rel_spc',
'rel_gen',
'rel_com',
'rel_par',
'rel_bga',
'rel_bgb',
'rel_rhy',
'rel_nry',
'rel_hom',
'rel_cns',
'v',
'topics',
'lc',
'rc',
'max'
}
self.suggest_params = {
's',
'max',
'v'
}
def validate_args(self, args, param_set):
for arg in args:
if arg not in param_set:
raise ValueError('{0} is not a valid parameter for this endpoint.'.format(arg))
def get_resource(self, endpoint, **kwargs):
# I feel like this should have some kind of error handling...
url = self.api_root + endpoint
response = requests.get(url, params=kwargs)
data = response.json()
return data
def words(self, **kwargs):
self.validate_args(kwargs, self.word_params)
words = '/words'
return self.get_resource(words, **kwargs)
def suggest(self, **kwargs):
self.validate_args(kwargs, self.suggest_params)
sug = '/sug'
return self.get_resource(sug, **kwargs)