Skip to content

Commit a9d400b

Browse files
author
Chris Park
committed
Conversion
- Moved class definitions for MorphologyOptions and Endpoints to Api dictionaries in accordance with Python best practices
1 parent 7c16861 commit a9d400b

File tree

6 files changed

+64
-58
lines changed

6 files changed

+64
-58
lines changed

examples/morphology_compound-components.py

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

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

1313

1414
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
@@ -19,7 +19,7 @@ def run(key, altUrl='https://api.rosette.com/rest/v1/'):
1919
params = DocumentParameters()
2020
params["content"] = morphology_compound_components_data
2121
try:
22-
return api.morphology(params, MorphologyOutput.COMPOUND_COMPONENTS)
22+
return api.morphology(params, api.morphology_output['COMPOUND_COMPONENTS'])
2323
except RosetteException as e:
2424
print(e)
2525

examples/morphology_han-readings.py

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

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

1313

1414
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
@@ -19,7 +19,7 @@ def run(key, altUrl='https://api.rosette.com/rest/v1/'):
1919
params = DocumentParameters()
2020
params["content"] = morphology_han_readings_data
2121
try:
22-
return api.morphology(params, MorphologyOutput.HAN_READINGS)
22+
return api.morphology(params, api.morphology_output['HAN_READINGS'])
2323
except RosetteException as e:
2424
print(e)
2525

examples/morphology_lemmas.py

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

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

1313

1414
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
@@ -19,7 +19,7 @@ def run(key, altUrl='https://api.rosette.com/rest/v1/'):
1919
params = DocumentParameters()
2020
params["content"] = morphology_lemmas_data
2121
try:
22-
return api.morphology(params, MorphologyOutput.LEMMAS)
22+
return api.morphology(params, api.morphology_output['LEMMAS'])
2323
except RosetteException as e:
2424
print(e)
2525

examples/morphology_parts-of-speech.py

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

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

1313

1414
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
@@ -19,7 +19,7 @@ def run(key, altUrl='https://api.rosette.com/rest/v1/'):
1919
params = DocumentParameters()
2020
params["content"] = morphology_parts_of_speech_data
2121
try:
22-
return api.morphology(params, MorphologyOutput.PARTS_OF_SPEECH)
22+
return api.morphology(params, api.morphology_output['PARTS_OF_SPEECH'])
2323
except RosetteException as e:
2424
print(e)
2525

rosette/api.py

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -78,33 +78,6 @@ def __str__(self):
7878
sst = repr(sst)
7979
return sst + ": " + self.message + ":\n " + self.response_message
8080

81-
MORPHOLOGY_OUTPUT = {
82-
'LEMMAS': 'lemmas',
83-
'PARTS_OF_SPEECH': 'parts-of-speech',
84-
'COMPOUND_COMPONENTS': 'compound-components',
85-
'HAN_READINGS': 'han-readings',
86-
'COMPLETE': 'complete'
87-
}
88-
89-
ENDPOINTS = {
90-
'CATEGORIES': 'categories',
91-
'ENTITIES': 'entities',
92-
'INFO': 'info',
93-
'LANGUAGE': 'language',
94-
'MORPHOLOGY': 'morphology',
95-
'NAME_TRANSLATION': 'name-translation',
96-
'NAME_SIMILARITY': 'name-similarity',
97-
'NAME_DEDUPLICATION': 'name-deduplication',
98-
'PING': 'ping',
99-
'RELATIONSHIPS': 'relationships',
100-
'SENTENCES': 'sentences',
101-
'SENTIMENT': 'sentiment',
102-
'SYNTAX_DEPENDENCIES': 'syntax/dependencies',
103-
'TEXT_EMBEDDING': 'text-embedding',
104-
'TOKENS': 'tokens',
105-
'TRANSLITERATION': 'transliteration'
106-
}
107-
10881

10982
class _DocumentParamSetBase(object):
11083

@@ -399,7 +372,7 @@ def info(self):
399372
"""Issues an "info" request to the L{EndpointCaller}'s specific endpoint.
400373
@return: A dictionary telling server version and other
401374
identifying data."""
402-
url = self.service_url + ENDPOINTS["INFO"]
375+
url = self.service_url + self.api.endpoints["INFO"]
403376
headers = {'Accept': 'application/json', 'X-RosetteAPI-Binding': 'python',
404377
'X-RosetteAPI-Binding-Version': _BINDING_VERSION}
405378

@@ -429,7 +402,7 @@ def ping(self):
429402
or is not the right server or some other error occurs, it will be
430403
signalled."""
431404

432-
url = self.service_url + ENDPOINTS['PING']
405+
url = self.service_url + self.api.endpoints['PING']
433406
headers = {'Accept': 'application/json', 'X-RosetteAPI-Binding': 'python',
434407
'X-RosetteAPI-Binding-Version': _BINDING_VERSION}
435408

@@ -476,9 +449,9 @@ def call(self, parameters):
476449
"""
477450

478451
if not isinstance(parameters, _DocumentParamSetBase):
479-
if self.suburl != ENDPOINTS['NAME_SIMILARITY'] \
480-
and self.suburl != ENDPOINTS['NAME_TRANSLATION'] \
481-
and self.suburl != ENDPOINTS['NAME_DEDUPLICATION']:
452+
if self.suburl != self.api.endpoints['NAME_SIMILARITY'] \
453+
and self.suburl != self.api.self.api.endpoints['NAME_TRANSLATION'] \
454+
and self.suburl != self.api.self.api.endpoints['NAME_DEDUPLICATION']:
482455
text = parameters
483456
parameters = DocumentParameters()
484457
parameters['content'] = text
@@ -537,7 +510,8 @@ def call(self, parameters):
537510
rdata = resp.content
538511
response_headers = {"responseHeaders": dict(resp.headers)}
539512
status = resp.status_code
540-
response = _ReturnObject(_my_loads(rdata, response_headers), status)
513+
response = _ReturnObject(
514+
_my_loads(rdata, response_headers), status)
541515
else:
542516
if self.debug:
543517
headers['X-RosetteAPI-Devel'] = True
@@ -588,8 +562,36 @@ def __init__(
588562
self.max_pool_size = 1
589563
self.session = requests.Session()
590564

565+
self.morphology_output = {
566+
'LEMMAS': 'lemmas',
567+
'PARTS_OF_SPEECH': 'parts-of-speech',
568+
'COMPOUND_COMPONENTS': 'compound-components',
569+
'HAN_READINGS': 'han-readings',
570+
'COMPLETE': 'complete'
571+
}
572+
573+
self.endpoints = {
574+
'CATEGORIES': 'categories',
575+
'ENTITIES': 'entities',
576+
'INFO': 'info',
577+
'LANGUAGE': 'language',
578+
'MORPHOLOGY': 'morphology',
579+
'NAME_TRANSLATION': 'name-translation',
580+
'NAME_SIMILARITY': 'name-similarity',
581+
'NAME_DEDUPLICATION': 'name-deduplication',
582+
'PING': 'ping',
583+
'RELATIONSHIPS': 'relationships',
584+
'SENTENCES': 'sentences',
585+
'SENTIMENT': 'sentiment',
586+
'SYNTAX_DEPENDENCIES': 'syntax/dependencies',
587+
'TEXT_EMBEDDING': 'text-embedding',
588+
'TOKENS': 'tokens',
589+
'TRANSLITERATION': 'transliteration'
590+
}
591+
591592
def _set_pool_size(self):
592-
adapter = requests.adapters.HTTPAdapter(pool_maxsize=self.max_pool_size)
593+
adapter = requests.adapters.HTTPAdapter(
594+
pool_maxsize=self.max_pool_size)
593595
if 'https:' in self.service_url:
594596
self.session.mount('https://', adapter)
595597
else:
@@ -613,7 +615,8 @@ def _make_request(self, operation, url, data, headers):
613615
if self.url_parameters:
614616
payload = self.url_parameters
615617

616-
request = requests.Request(operation, url, data=data, headers=headers, params=payload)
618+
request = requests.Request(
619+
operation, url, data=data, headers=headers, params=payload)
617620
session = requests.Session()
618621
prepared_request = session.prepare_request(request)
619622

@@ -800,7 +803,7 @@ def language(self, parameters):
800803
@type parameters: L{DocumentParameters} or L{str}
801804
@return: A python dictionary containing the results of language
802805
identification."""
803-
return EndpointCaller(self, ENDPOINTS['LANGUAGE']).call(parameters)
806+
return EndpointCaller(self, self.endpoints['LANGUAGE']).call(parameters)
804807

805808
def sentences(self, parameters):
806809
"""
@@ -809,7 +812,7 @@ def sentences(self, parameters):
809812
and possible metadata, to be processed by the sentence identifier.
810813
@type parameters: L{DocumentParameters} or L{str}
811814
@return: A python dictionary containing the results of sentence identification."""
812-
return EndpointCaller(self, ENDPOINTS['SENTENCES']).call(parameters)
815+
return EndpointCaller(self, self.endpoints['SENTENCES']).call(parameters)
813816

814817
def tokens(self, parameters):
815818
"""
@@ -818,9 +821,9 @@ def tokens(self, parameters):
818821
and possible metadata, to be processed by the tokens identifier.
819822
@type parameters: L{DocumentParameters} or L{str}
820823
@return: A python dictionary containing the results of tokenization."""
821-
return EndpointCaller(self, ENDPOINTS['TOKENS']).call(parameters)
824+
return EndpointCaller(self, self.endpoints['TOKENS']).call(parameters)
822825

823-
def morphology(self, parameters, facet=MORPHOLOGY_OUTPUT['COMPLETE']):
826+
def morphology(self, parameters, facet=""):
824827
"""
825828
Create an L{EndpointCaller} to returns a specific facet
826829
of the morphological analyses of texts to which it is applied and call it.
@@ -830,7 +833,9 @@ def morphology(self, parameters, facet=MORPHOLOGY_OUTPUT['COMPLETE']):
830833
@param facet: The facet desired, to be returned by the created L{EndpointCaller}.
831834
@type facet: An element of L{MorphologyOutput}.
832835
@return: A python dictionary containing the results of morphological analysis."""
833-
return EndpointCaller(self, ENDPOINTS['MORPHOLOGY'] + "/" + facet).call(parameters)
836+
if facet == "":
837+
facet = self.morphology_output['COMPLETE']
838+
return EndpointCaller(self, self.endpoints['MORPHOLOGY'] + "/" + facet).call(parameters)
834839

835840
def entities(self, parameters):
836841
"""
@@ -841,7 +846,7 @@ def entities(self, parameters):
841846
@type parameters: L{DocumentParameters} or L{str}
842847
@return: A python dictionary containing the results of entity extraction."""
843848

844-
return EndpointCaller(self, ENDPOINTS['ENTITIES']).call(parameters)
849+
return EndpointCaller(self, self.endpoints['ENTITIES']).call(parameters)
845850

846851
def categories(self, parameters):
847852
"""
@@ -851,7 +856,7 @@ def categories(self, parameters):
851856
and possible metadata, to be processed by the category identifier.
852857
@type parameters: L{DocumentParameters} or L{str}
853858
@return: A python dictionary containing the results of categorization."""
854-
return EndpointCaller(self, ENDPOINTS['CATEGORIES']).call(parameters)
859+
return EndpointCaller(self, self.endpoints['CATEGORIES']).call(parameters)
855860

856861
def sentiment(self, parameters):
857862
"""
@@ -865,7 +870,7 @@ def sentiment(self, parameters):
865870
to which is applied.
866871
@return: An L{EndpointCaller} object which can return sentiments
867872
of texts to which it is applied."""
868-
return EndpointCaller(self, ENDPOINTS['SENTIMENT']).call(parameters)
873+
return EndpointCaller(self, self.endpoints['SENTIMENT']).call(parameters)
869874

870875
def relationships(self, parameters):
871876
"""
@@ -875,7 +880,7 @@ def relationships(self, parameters):
875880
and possible metadata, to be processed by the relationships identifier.
876881
@type parameters: L{DocumentParameters} or L{str}
877882
@return: A python dictionary containing the results of relationship extraction."""
878-
return EndpointCaller(self, ENDPOINTS['RELATIONSHIPS']).call(parameters)
883+
return EndpointCaller(self, self.endpoints['RELATIONSHIPS']).call(parameters)
879884

880885
def name_translation(self, parameters):
881886
"""
@@ -885,7 +890,7 @@ def name_translation(self, parameters):
885890
and possible metadata, to be processed by the name translator.
886891
@type parameters: L{NameTranslationParameters}
887892
@return: A python dictionary containing the results of name translation."""
888-
return EndpointCaller(self, ENDPOINTS['NAME_TRANSLATION']).call(parameters)
893+
return EndpointCaller(self, self.endpoints['NAME_TRANSLATION']).call(parameters)
889894

890895
def translated_name(self, parameters):
891896
""" deprecated
@@ -904,7 +909,7 @@ def name_similarity(self, parameters):
904909
and possible metadata, to be processed by the name matcher.
905910
@type parameters: L{NameSimilarityParameters}
906911
@return: A python dictionary containing the results of name matching."""
907-
return EndpointCaller(self, ENDPOINTS['NAME_SIMILARITY']).call(parameters)
912+
return EndpointCaller(self, self.endpoints['NAME_SIMILARITY']).call(parameters)
908913

909914
def matched_name(self, parameters):
910915
""" deprecated
@@ -922,15 +927,15 @@ def name_deduplication(self, parameters):
922927
as a threshold
923928
@type parameters: L{NameDeduplicationParameters}
924929
@return: A python dictionary containing the results of de-duplication"""
925-
return EndpointCaller(self, ENDPOINTS['NAME_DEDUPLICATION']).call(parameters)
930+
return EndpointCaller(self, self.endpoints['NAME_DEDUPLICATION']).call(parameters)
926931

927932
def text_embedding(self, parameters):
928933
"""
929934
Create an L{EndpointCaller} to identify text vectors found in the texts
930935
to which it is applied and call it.
931936
@type parameters: L{DocumentParameters} or L{str}
932937
@return: A python dictionary containing the results of text embedding."""
933-
return EndpointCaller(self, ENDPOINTS['TEXT_EMBEDDING']).call(parameters)
938+
return EndpointCaller(self, self.endpoints['TEXT_EMBEDDING']).call(parameters)
934939

935940
def syntax_dependencies(self, parameters):
936941
"""
@@ -939,11 +944,11 @@ def syntax_dependencies(self, parameters):
939944
@type parameters: L{DocumentParameters} or L{str}
940945
@return: A python dictionary containing the results of syntactic dependencies
941946
identification"""
942-
return EndpointCaller(self, ENDPOINTS['SYNTAX_DEPENDENCIES']).call(parameters)
947+
return EndpointCaller(self, self.endpoints['SYNTAX_DEPENDENCIES']).call(parameters)
943948

944949
def transliteration(self, parameters):
945950
"""
946951
Transliterate given context
947952
@type parameters: L{TransliterationParameters}
948953
@return: A python dictionary containing the results of the transliteration"""
949-
return EndpointCaller(self, ENDPOINTS['TRANSLITERATION']).call(parameters)
954+
return EndpointCaller(self, self.endpoints['TRANSLITERATION']).call(parameters)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def read(*filenames, **kwargs):
2525
buf.append(the_file.read())
2626
return sep.join(buf)
2727

28+
2829
LONG_DESCRIPTION = read('README.md')
2930

3031
setup(name=NAME,

0 commit comments

Comments
 (0)