Skip to content

Commit ce51439

Browse files
authored
Merge pull request #53 from hannah-gaudet/RCB-444
RCB 444
2 parents 3c7ec51 + aa8ed15 commit ce51439

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

examples/entities_linked.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
Example code to call Rosette API to get linked (against Wikipedia) entities from a piece of text.
5+
"""
6+
7+
import argparse
8+
import json
9+
import os
10+
11+
from rosette.api import API, DocumentParameters
12+
13+
14+
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
15+
# Create an API instance
16+
api = API(user_key=key, service_url=altUrl)
17+
18+
entities_linked_text_data = "Last month director Paul Feig announced the movie will have an all-star female cast including Kristen Wiig, Melissa McCarthy, Leslie Jones and Kate McKinnon."
19+
params = DocumentParameters()
20+
params["content"] = entities_linked_text_data
21+
params["genre"] = "social-media"
22+
# This syntax is deprecated, call api.entities(params)
23+
return api.entities(params, True)
24+
25+
26+
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')
27+
parser.add_argument('-k', '--key', help='Rosette API Key', required=True)
28+
parser.add_argument('-u', '--url', help="Alternative API URL", default='https://api.rosette.com/rest/v1/')
29+
30+
if __name__ == '__main__':
31+
args = parser.parse_args()
32+
result = run(args.key, args.url)
33+
print(json.dumps(result, indent=2, ensure_ascii=False, sort_keys=True).encode("utf8"))

rosette/api.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from socket import gaierror
2828
import requests
2929
import re
30+
import warnings
3031

3132
_BINDING_VERSION = '1.1.1'
3233
_GZIP_BYTEARRAY = bytearray([0x1F, 0x8b, 0x08])
@@ -752,16 +753,23 @@ def morphology(self, parameters, facet=MorphologyOutput.COMPLETE):
752753
@return: A python dictionary containing the results of morphological analysis."""
753754
return EndpointCaller(self, "morphology/" + facet).call(parameters)
754755

755-
def entities(self, parameters):
756+
def entities(self, parameters, resolve_entities=False):
756757
"""
757758
Create an L{EndpointCaller} to identify named entities found in the texts
758759
to which it is applied and call it. Linked entity information is optional, and
759760
its need must be specified at the time the operator is created.
760761
@param parameters: An object specifying the data,
761762
and possible metadata, to be processed by the entity identifier.
762763
@type parameters: L{DocumentParameters} or L{str}
764+
@param resolve_entities: Specifies whether or not linked entity information will
765+
be wanted.
766+
@type resolve_entities: Boolean
763767
@return: A python dictionary containing the results of entity extraction."""
764-
return EndpointCaller(self, "entities").call(parameters)
768+
if resolve_entities:
769+
warnings.warn("entities(params,resolve_entities) is deprecated and replaced by entities(params).", DeprecationWarning)
770+
return EndpointCaller(self, "entities/linked").call(parameters)
771+
else:
772+
return EndpointCaller(self, "entities").call(parameters)
765773

766774
def categories(self, parameters):
767775
"""

tests/test_rosette_api.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,21 @@ def test_the_entities_endpoint(api, json_response, doc_params):
307307
httpretty.disable()
308308
httpretty.reset()
309309

310+
# Test the entities/linked endpoint
311+
312+
313+
def test_the_entities_linked_endpoint(api, json_response, doc_params):
314+
httpretty.enable()
315+
httpretty.register_uri(httpretty.POST, "https://api.rosette.com/rest/v1/info",
316+
body=json_response, status=200, content_type="application/json")
317+
httpretty.register_uri(httpretty.POST, "https://api.rosette.com/rest/v1/entities/linked",
318+
body=json_response, status=200, content_type="application/json")
319+
320+
result = api.entities(doc_params, True)
321+
assert result["name"] == "Rosette API"
322+
httpretty.disable()
323+
httpretty.reset()
324+
310325
# Test the categories endpoint
311326

312327

0 commit comments

Comments
 (0)