import json
import requests
def call_botong_entity_small(query, k=5, url="http://gateway.botong.woa.com/ner"):
query = str(query).replace(' ', '')
json_data = {
"text": query,
"link_option": {"k": k},
}
payload = json.dumps(json_data, ensure_ascii=False).encode('utf-8')
headers = {
'content-type': "application/json",
'business-id': 'mmsearchentitiylinking'
}
resp_json = {}
try:
response = requests.request("POST", url, data=payload, headers=headers)
try:
resp_json = json.loads(response.text)
except Exception as e1:
resp_json['trans_json_error'] = str(e1)
except Exception as e2:
resp_json['call_api_error'] = str(e2)
resp = resp_json
ner_words = []
if 'trans_json_error' in resp.keys():
return resp['trans_json_error']
if 'call_api_error' in resp.keys():
return resp['call_api_error']
if 'mention' in resp.keys():
if len(resp['mention']) >= 1:
for tmp in resp['mention']:
if 'mention' in tmp.keys():
ner_words.append(tmp['mention'])
return ner_words, resp['mention']
return ner_words, []
|