Skip to content

Commit 97eb2c3

Browse files
committed
Merge pull request #45 from cp2boston/RCB-399_options
RCB-399 options
2 parents 71332e5 + 33b3660 commit 97eb2c3

32 files changed

+67
-1920
lines changed

examples/relationships.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
import json
99
import os
1010

11-
from rosette.api import API, RelationshipsParameters
11+
from rosette.api import API, DocumentParameters
1212

1313

1414
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
1515
# Create an API instance
1616
api = API(user_key=key, service_url=altUrl)
1717
relationships_text_data = "The Ghostbusters movie was filmed in Boston."
18-
params = RelationshipsParameters()
18+
params = DocumentParameters()
1919
params["content"] = relationships_text_data
20-
params["options"] = {"accuracyMode": "PRECISION"}
20+
api.setOption('accuracyMode', 'PRECISION')
2121
return api.relationships(params)
2222

2323

rosette/api.py

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,18 @@ def __getitem__(self, key):
143143
def validate(self):
144144
pass
145145

146-
def serialize(self):
146+
def serialize(self, options):
147147
self.validate()
148148
v = {}
149149
for (key, val) in self.__params.items():
150150
if val is None:
151151
pass
152152
else:
153153
v[key] = val
154+
155+
if options is not None and len(options) > 0:
156+
v['options'] = options
157+
154158
return v
155159

156160

@@ -201,10 +205,10 @@ def validate(self):
201205
"Cannot supply both Content and ContentUri",
202206
"bad arguments")
203207

204-
def serialize(self):
208+
def serialize(self, options):
205209
"""Internal. Do not use."""
206210
self.validate()
207-
slz = super(DocumentParameters, self).serialize()
211+
slz = super(DocumentParameters, self).serialize(options)
208212
return slz
209213

210214
def load_document_file(self, path):
@@ -227,18 +231,6 @@ def load_document_string(self, s):
227231
self["content"] = s
228232

229233

230-
class RelationshipsParameters(DocumentParameters):
231-
232-
"""Parameter object for relationships endpoint. Inherits from L(DocumentParameters), but allows the user
233-
to specify the relationships-unique options parameter."""
234-
235-
def __init__(self):
236-
"""Create a L{RelationshipsParameters} object."""
237-
self.useMultipart = False
238-
_DocumentParamSetBase.__init__(
239-
self, ("content", "contentUri", "language", "options", "genre"))
240-
241-
242234
class NameTranslationParameters(_DocumentParamSetBase):
243235
"""Parameter object for C{name-translation} endpoint.
244236
The following values may be set by the indexing (i.e.,C{ parms["name"]}) operator. The values are all
@@ -405,7 +397,7 @@ def call(self, parameters):
405397
endpoints except C{name-translation} and C{name-similarity}, it must be a L{DocumentParameters}
406398
object or a string; for C{name-translation}, it must be an L{NameTranslationParameters} object;
407399
for C{name-similarity}, it must be an L{NameSimilarityParameters} object. For relationships,
408-
it may be an L(DocumentParameters) or an L(RelationshipsParameters).
400+
it may be an L(DocumentParameters).
409401
410402
In all cases, the result is returned as a python dictionary
411403
conforming to the JSON object described in the endpoint's entry
@@ -431,7 +423,7 @@ def call(self, parameters):
431423

432424
self.useMultipart = parameters.useMultipart
433425
url = self.service_url + self.suburl
434-
params_to_serialize = parameters.serialize()
426+
params_to_serialize = parameters.serialize(self.api.options)
435427
headers = {}
436428
if self.user_key is not None:
437429
headers["X-RosetteAPI-Key"] = self.user_key
@@ -509,6 +501,7 @@ def __init__(
509501
self.reuse_connection = reuse_connection
510502
self.connection_refresh_duration = refresh_duration
511503
self.http_connection = None
504+
self.options = {}
512505

513506
def _connect(self, parsedUrl):
514507
""" Simple connection method
@@ -614,6 +607,37 @@ def _post_http(self, url, data, headers):
614607

615608
return _ReturnObject(_my_loads(rdata, response_headers), status)
616609

610+
def setOption(self, name, value):
611+
"""
612+
Sets an option
613+
614+
@param name: name of option
615+
@param value: value of option
616+
"""
617+
if value is None:
618+
self.options.pop(name, None)
619+
else:
620+
self.options[name] = value
621+
622+
def getOption(self, name):
623+
"""
624+
Gets an option
625+
626+
@param name: name of option
627+
628+
@return: value of option
629+
"""
630+
if name in self.options.keys():
631+
return self.options[name]
632+
else:
633+
return None
634+
635+
def clearOptions(self):
636+
"""
637+
Clears all options
638+
"""
639+
self.options.clear()
640+
617641
def ping(self):
618642
"""
619643
Create a ping L{EndpointCaller} for the server and ping it.
@@ -715,7 +739,7 @@ def relationships(self, parameters):
715739
which it is applied and call it.
716740
@param parameters: An object specifying the data,
717741
and possible metadata, to be processed by the relationships identifier.
718-
@type parameters: L{DocumentParameters}, L(RelationshipsParameters), or L{str}
742+
@type parameters: L{DocumentParameters} or L{str}
719743
@return: A python dictionary containing the results of relationship extraction."""
720744
return EndpointCaller(self, "relationships").call(parameters)
721745

tests/mock-data/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/mock-data/request/ara-doc-entities.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/mock-data/request/eng-doc-entities.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/mock-data/request/fra-doc-entities.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/mock-data/request/jpn-doc-entities.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)