Created
April 16, 2020 16:16
-
-
Save zwurgl/ac2103a671089f6fe99d85e60537726c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def call_sherpa(text,model,annotator,server,token) | |
url = server+"/projects/"+model+"/annotators/"+annotator+"/_annotate" | |
results = {} | |
text = text.encode(encoding='utf-8') | |
headers = {"Accept": "application/json", | |
"Content-Type": "text/plain", | |
"Authorization": "Bearer " + token} | |
response = requests.post(url,data=text, headers=headers) | |
if (response.status_code != 200): | |
print("error from server: %s" % response.status_code) | |
return | |
json_response = json.loads(response.text) | |
documenttext = json_response['text'] | |
annotations = json_response['annotations'] | |
for annotation in annotations: | |
start = annotation['start'] | |
end = annotation['end'] | |
term = documenttext[start:end] | |
type = annotation['labelName'] | |
if type in results: | |
results[type].extend([term]) | |
else: | |
results[type] = [term] | |
for key in results.keys(): | |
list_set = set(results[key]) | |
results[key] = list(list_set) | |
return results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment