-
Notifications
You must be signed in to change notification settings - Fork 0
/
lamapi.py
173 lines (143 loc) · 4.98 KB
/
lamapi.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""LamAPI Wrapper File"""
import os
import requests
class FormatException(Exception):
"""Invalid Format Exception"""
def __str__(self) -> str:
return "Invalid Format"
class URL:
"""URLs Builder Class"""
def __init__(self, base_url, response_format="json"):
self.format = response_format
self.base_url = base_url
# lookup
self.lookup = "lookup/entity-retrieval"
# entity
self.entities_labels = "entity/labels"
self.entities_objects = "entity/objects"
self.entities_predicates = "entity/predicates"
self.entities_types = "entity/types"
self.entities_literals = "entity/literals"
# classify
self._literal_recognizer = "classify/literal-recognizer"
# sti
self._column_analysis = "sti/column-analysis"
def lookup_url(self):
"""Return Lookup URL"""
return self.base_url + self.lookup
def entities_labels_url(self):
"""Return Labels URL"""
return self.base_url + self.entities_labels
def column_analysis_url(self):
"""Return Column Analysis URL"""
return self.base_url + self._column_analysis
def predicates_url(self):
"""Return predicates URL"""
return self.base_url + self.entities_predicates
def types_url(self):
"""Return types URL"""
return self.base_url + self.entities_types
class LamAPI():
"""LamAPI Wrapper Class"""
def __init__(self, response_format="json", kg="wikidata") -> None:
self.format = response_format
base_url = os.getenv('LAMAPI_ROOT')
self._url = URL(base_url, response_format=response_format)
self.client_key = os.getenv('LAMAPI_TOKEN')
self.kg = kg
self.headers = {
'accept': 'application/json'
}
def _exec_post(self, params, json_data, url, kg):
response = requests.post(url,
params=params,
headers=self.headers,
json=json_data,
timeout=15)
result = response.json()
if kg in result:
result = result[kg]
return result
def __to_format(self, response):
if self.format == "json":
result = response.json()
for kg in ["wikidata", "dbpedia", "crunchbase"]:
if kg in result:
result = result[kg]
break
return result
else:
raise FormatException
def __submit_get(self, url, params):
return self.__to_format(requests.get(url, headers=self.headers, params=params, timeout=15))
def __submit_post(self, url, params, headers, json):
return self.__to_format(requests.post(url,
headers=headers,
params=params,
json=json,
timeout=15))
def column_analysis(self, columns):
"""LamAPI Column Analysis Method"""
json_data = {
'json': columns
}
params = {
'token': self.client_key
}
result = self.__submit_post(
self._url.column_analysis_url(), params, self.headers, json_data)
return result
def labels(self, entitites, lang="en"):
"""LamAPI Labels Method"""
params = {
'token': self.client_key,
'kg': self.kg,
'lang': lang
}
json_data = {
'json': entitites
}
result = self.__submit_post(
self._url.entities_labels_url(), params, self.headers, json_data)
return result
def lookup(self, string, ngrams=False, fuzzy=False, types=None, limit=10):
"""LamAPI Lookup Method"""
params = {
'token': os.environ.get('LAMAPI_TOKEN'),
'name': string,
'ngrams': ngrams,
'fuzzy': fuzzy,
'types': types,
'kg': self.kg,
'limit': limit
}
result = self.__submit_get(self._url.lookup_url(), params)
if len(result) > 1:
result = {"wikidata": result}
return result
def types(self, entities):
"""LamAPI types"""
params = {
'token': self.client_key,
'kg': self.kg,
}
json_data = {
'json': entities
}
result = self.__submit_post(
self._url.types_url(), params, self.headers, json_data)
return result
def predicates(self, entities):
"""LamAPI predicates"""
params = {
'token': self.client_key,
'kg': self.kg,
}
json_data = {
'json': entities
}
result = self.__submit_post(
self._url.predicates_url(), params, self.headers, json_data)
return result
def test(self) -> str:
return "test"