-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathfalcon_call.py
103 lines (94 loc) · 3.97 KB
/
falcon_call.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
import csv
import requests
headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}
def falcon2_call(text,mode='short'):
try:
text=text.replace('"','')
text=text.replace("'","")
if mode=='short':
url = 'https://labs.tib.eu/falcon/falcon2/api?mode=short&db=1'
entities_wikidata=[]
entities_db=[]
payload = '{"text":"'+text+'"}'
r = requests.post(url, data=payload.encode('utf-8'), headers=headers)
if r.status_code == 200:
response=r.json()
#print(response)
for result in response['entities_wikidata']:
entities_wikidata.append(result[0])
for result in response['entities_dbpedia']:
entities_db.append(result[0])
else:
r = requests.post(url, data=payload.encode('utf-8'), headers=headers)
if r.status_code == 200:
response=r.json()
for result in response['entities_wikidata']:
entities_wikidata.append(result[0])
for result in response['entities_dbpedia']:
entities_db.append(result[0])
if len(entities_wikidata)>0:
entities_wikidata=entities_wikidata[0].replace('<','').replace('>','')
if len(entities_db)>0:
entities_db=entities_db[0]
return entities_wikidata,entities_db
else:
url = 'https://labs.tib.eu/falcon/falcon2/api?mode=long&db=1'
entities_wikidata=[]
entities_db=[]
payload = '{"text":"'+text+'"}'
r = requests.post(url, data=payload.encode('utf-8'), headers=headers)
if r.status_code == 200:
response=r.json()
#print(response)
return response
for result in response['entities_wikidata']:
entities_wikidata.append(result)
for result in response['entities_dbpedia']:
entities_db.append(result)
else:
r = requests.post(url, data=payload.encode('utf-8'), headers=headers)
if r.status_code == 200:
response=r.json()
return response
for result in response['entities_wikidata']:
entities_wikidata.append(result)
for result in response['entities_dbpedia']:
entities_db.append(result)
if len(entities_wikidata)>0:
entities_wikidata[0][1]=entities_wikidata[0][1].replace('<','').replace('>','')
if len(entities_db)>0:
entities_db=entities_db
return entities_wikidata,entities_db
except:
raise
return -1
def bioFalcon_call(text, mode='short'):
text=text.replace('"','')
text=text.replace("'","")
if mode=='short':
url = 'https://labs.tib.eu/sdm/biofalcon/api?mode='+mode
payload = '{"text":"'+text+'"}'
try:
r = requests.post(url, data=payload.encode('utf-8'), headers=headers)
if r.status_code == 200:
response=r.json()
if len(response['entities']) > 1:
return response['entities'][1][0]
else:
return ""
else:
return ""
except:
raise
return ""
else:
url = 'https://labs.tib.eu/sdm/biofalcon/api?mode='+mode
payload = '{"text":"'+text+'"}'
try:
r = requests.post(url, data=payload.encode('utf-8'), headers=headers)
if r.status_code == 200:
response=r.json()
return response
except:
raise
return ""