Skip to content

Commit f4d9395

Browse files
cp2bostonLi Xu
authored andcommitted
RCB-542 1.9 release (#77)
* Added runtime version - Removed deprecated methods and messages - Added method for returning User-Agent string - Added unit test for User-Agent string - Removed deprecated tests * WS-1272 Multilingual example * Example updates * Update multilingual to True
1 parent 294be0b commit f4d9395

File tree

9 files changed

+62
-209
lines changed

9 files changed

+62
-209
lines changed

examples/entities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def run(key, alt_url='https://api.rosette.com/rest/v1/'):
1515
""" Run the example """
1616
# Create an API instance
1717
api = API(user_key=key, service_url=alt_url)
18-
entities_text_data = "Bill Murray will appear in new Ghostbusters film: Dr. Peter Venkman was spotted filming a cameo in Boston this… http://dlvr.it/BnsFfS"
18+
entities_text_data = "The Securities and Exchange Commission today announced the leadership of the agency’s trial unit. Bridget Fitzpatrick has been named Chief Litigation Counsel of the SEC and David Gottesman will continue to serve as the agency’s Deputy Chief Litigation Counsel. Since December 2016, Ms. Fitzpatrick and Mr. Gottesman have served as Co-Acting Chief Litigation Counsel. In that role, they were jointly responsible for supervising the trial unit at the agency’s Washington D.C. headquarters as well as coordinating with litigators in the SEC’s 11 regional offices around the country."
1919
params = DocumentParameters()
2020
params["content"] = entities_text_data
2121
params["genre"] = "social-media"

examples/language_multilingual.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Example code to call Rosette API to determine the language of a piece of text.
4+
"""
5+
from __future__ import print_function
6+
7+
import argparse
8+
import json
9+
import os
10+
11+
from rosette.api import API, DocumentParameters, RosetteException
12+
13+
14+
def run(key, alt_url='https://api.rosette.com/rest/v1/'):
15+
""" Run the example """
16+
# Create an API instance
17+
api = API(user_key=key, service_url=alt_url)
18+
19+
language_multilingual_data = "On Thursday, as protesters gathered in Washington D.C., the United States Federal Communications Commission under Chairman Ajit Pai voted 3-2 to overturn a 2015 decision, commonly called Net Neutrality, that forbade Internet service providers (ISPs) such as Verizon, Comcast, and AT&T from blocking individual websites or charging websites or customers more for faster load times. Quatre femmes ont été nommées au Conseil de rédaction de la loi du Qatar. Jeudi, le décret royal du Qatar a annoncé que 28 nouveaux membres ont été nommés pour le Conseil de la Choura du pays. ذكرت مصادر أمنية يونانية، أن 9 موقوفين من منظمة \"د هـ ك ب ج\" الذين كانت قد أوقفتهم الشرطة اليونانية في وقت سابق كانوا يخططون لاغتيال الرئيس التركي رجب طيب أردوغان."
20+
params = DocumentParameters()
21+
params["content"] = language_multilingual_data
22+
api.set_custom_headers("X-RosetteAPI-App", "python-app")
23+
api.set_option('multilingual', True)
24+
25+
try:
26+
return api.language(params)
27+
except RosetteException as exception:
28+
print(exception)
29+
30+
31+
PARSER = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
32+
description='Calls the ' +
33+
os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')
34+
PARSER.add_argument('-k', '--key', help='Rosette API Key', required=True)
35+
PARSER.add_argument('-u', '--url', help="Alternative API URL",
36+
default='https://api.rosette.com/rest/v1/')
37+
38+
if __name__ == '__main__':
39+
ARGS = PARSER.parse_args()
40+
RESULT = run(ARGS.key, ARGS.url)
41+
print(json.dumps(RESULT, indent=2, ensure_ascii=False, sort_keys=True).encode("utf8"))

examples/morphology_complete.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def run(key, alt_url='https://api.rosette.com/rest/v1/'):
1616
# Create an API instance
1717
api = API(user_key=key, service_url=alt_url)
1818

19-
morphology_complete_data = "The quick brown fox jumped over the lazy dog. Yes he did."
19+
morphology_complete_data = "The quick brown fox jumped over the lazy dog. 👍🏾 Yes he did. B)"
2020
params = DocumentParameters()
2121
params["content"] = morphology_complete_data
2222
try:

examples/name_deduplication.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def run(key, alt_url='https://api.rosette.com/rest/v1/'):
1616
# Create an API instance
1717
api = API(user_key=key, service_url=alt_url)
1818

19-
name_dedupe_data = 'John Smith,Johnathon Smith,Fred Jones'
19+
name_dedupe_data = "Alice Terry,Alice Thierry,Betty Grable,Betty Gable,Norma Shearer,Norm Shearer,Brigitte Helm,Bridget Helem,Judy Holliday,Julie Halliday"
2020
threshold = 0.75
2121
params = NameDeduplicationParameters()
2222
params["names"] = name_dedupe_data.split(',')

examples/relationships.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def run(key, alt_url='https://api.rosette.com/rest/v1/'):
1515
""" Run the example """
1616
# Create an API instance
1717
api = API(user_key=key, service_url=alt_url)
18-
relationships_text_data = "Bill Gates, Microsoft's former CEO, is a philanthropist."
18+
relationships_text_data = "FLIR Systems is headquartered in Oregon and produces thermal imaging, night vision, and infrared cameras and sensor systems. According to the SEC’s order instituting a settled administrative proceeding, FLIR entered into a multi-million dollar contract to provide thermal binoculars to the Saudi government in November 2008. Timms and Ramahi were the primary sales employees responsible for the contract, and also were involved in negotiations to sell FLIR’s security cameras to the same government officials. At the time, Timms was the head of FLIR’s Middle East office in Dubai."
1919
params = DocumentParameters()
2020
params["content"] = relationships_text_data
2121
api.set_option('accuracyMode', 'PRECISION')

examples/sentences.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def run(key, alt_url='https://api.rosette.com/rest/v1/'):
1616
# Create an API instance
1717
api = API(user_key=key, service_url=alt_url)
1818

19-
sentences_data = "This land is your land. This land is my land\nFrom California to the New York island;\nFrom the red wood forest to the Gulf Stream waters\n\nThis land was made for you and Me.\n\nAs I was walking that ribbon of highway,\nI saw above me that endless skyway:\nI saw below me that golden valley:\nThis land was made for you and me."
19+
sentences_data = "This land is your land. This land is my land, from California to the New York island; from the red wood forest to the Gulf Stream waters. This land was made for you and Me. As I was walking that ribbon of highway, I saw above me that endless skyway: I saw below me that golden valley: This land was made for you and me."
2020
params = DocumentParameters()
2121
params["content"] = sentences_data
2222

examples/transliteration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def run(key, alt_url='https://api.rosette.com/rest/v1/'):
1616
# Create an API instance
1717
api = API(user_key=key, service_url=alt_url)
1818

19-
transliteration_data = "Some random text"
19+
transliteration_data = "ana r2ye7 el gam3a el sa3a 3 el 3asr"
2020
params = DocumentParameters()
2121
params["content"] = transliteration_data
2222

rosette/api.py

Lines changed: 7 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import re
2727
import warnings
2828
import requests
29+
import platform
2930

3031
_BINDING_VERSION = '1.8.2'
3132
_GZIP_BYTEARRAY = bytearray([0x1F, 0x8b, 0x08])
@@ -82,50 +83,6 @@ def __str__(self):
8283
return sst + ": " + self.message + ":\n " + self.response_message
8384

8485

85-
class _PseudoEnum(object):
86-
""" Base class for MorphologyOutput """
87-
88-
def __init__(self):
89-
pass
90-
91-
@classmethod
92-
def validate(cls, value, name):
93-
""" validation method """
94-
values = []
95-
for (key, value) in vars(cls).items():
96-
if not key.startswith("__"):
97-
values += [value]
98-
99-
# this is still needed to make sure that the parameter NAMES are known.
100-
# If python didn't allow setting unknown values, this would be a
101-
# language error.
102-
if value not in values:
103-
raise RosetteException(
104-
"unknownVariable",
105-
"The value supplied for " +
106-
name +
107-
" is not one of " +
108-
", ".join(values) +
109-
".",
110-
repr(value))
111-
112-
113-
class MorphologyOutput(_PseudoEnum):
114-
""" Class to provide Morphology sub-endpoints """
115-
116-
def __init__(self):
117-
warnings.warn('MorphologyOutput to be removed in version 1.9.0. '
118-
'Please use API.morphology_output',
119-
DeprecationWarning)
120-
_PseudoEnum.__init__()
121-
122-
LEMMAS = "lemmas"
123-
PARTS_OF_SPEECH = "parts-of-speech"
124-
COMPOUND_COMPONENTS = "compound-components"
125-
HAN_READINGS = "han-readings"
126-
COMPLETE = "complete"
127-
128-
12986
class _DocumentParamSetBase(object):
13087

13188
def __init__(self, repertoire):
@@ -582,6 +539,7 @@ def __init__(
582539
self.url_parameters = {}
583540
self.max_pool_size = 1
584541
self.session = requests.Session()
542+
self.user_agent_string = 'RosetteAPIPython/' + _BINDING_VERSION + '/' + platform.python_version()
585543

586544
self.morphology_output = {
587545
'LEMMAS': 'lemmas',
@@ -617,6 +575,10 @@ def __del__(self):
617575
except ReferenceError:
618576
pass
619577

578+
def get_user_agent_string(self):
579+
""" Return the User-Agent string """
580+
return self.user_agent_string
581+
620582
def _set_pool_size(self):
621583
adapter = requests.adapters.HTTPAdapter(
622584
pool_maxsize=self.max_pool_size)
@@ -632,7 +594,7 @@ def _make_request(self, operation, url, data, headers):
632594
@param data: request data
633595
@param headers: request headers
634596
"""
635-
headers['User-Agent'] = "RosetteAPIPython/" + _BINDING_VERSION
597+
headers['User-Agent'] = self.get_user_agent_string()
636598

637599
message = None
638600
code = "unknownError"
@@ -718,31 +680,12 @@ def post_http(self, url, data, headers):
718680

719681
return _ReturnObject(_my_loads(rdata, response_headers), status)
720682

721-
def getPoolSize(self):
722-
"""
723-
Returns the maximum pool size, which is the returned x-rosetteapi-concurrency value
724-
"""
725-
warnings.warn('Method renamed to API.get_pool_size(). To be removed in version 1.9.0',
726-
DeprecationWarning)
727-
return self.get_pool_size()
728-
729683
def get_pool_size(self):
730684
"""
731685
Returns the maximum pool size, which is the returned x-rosetteapi-concurrency value
732686
"""
733687
return int(self.max_pool_size)
734688

735-
def setOption(self, name, value):
736-
"""
737-
Sets an option
738-
739-
@param name: name of option
740-
@param value: value of option
741-
"""
742-
warnings.warn('Method renamed to API.set_option(). To be removed in version 1.9.0',
743-
DeprecationWarning)
744-
return self.set_option(name, value)
745-
746689
def set_option(self, name, value):
747690
"""
748691
Sets an option
@@ -755,18 +698,6 @@ def set_option(self, name, value):
755698
else:
756699
self.options[name] = value
757700

758-
def getOption(self, name):
759-
"""
760-
Gets an option
761-
762-
@param name: name of option
763-
764-
@return: value of option
765-
"""
766-
warnings.warn('Method renamed to API.get_option(). To be removed in version 1.9.0',
767-
DeprecationWarning)
768-
return self.get_option(name)
769-
770701
def get_option(self, name):
771702
"""
772703
Gets an option
@@ -780,31 +711,12 @@ def get_option(self, name):
780711
else:
781712
return None
782713

783-
def clearOptions(self):
784-
"""
785-
Clears all options
786-
"""
787-
warnings.warn('Method renamed to API.clear_options(). To be removed in version 1.9.0',
788-
DeprecationWarning)
789-
self.clear_options()
790-
791714
def clear_options(self):
792715
"""
793716
Clears all options
794717
"""
795718
self.options.clear()
796719

797-
def setUrlParameter(self, name, value):
798-
"""
799-
Sets a URL parameter
800-
801-
@param name: name of parameter
802-
@param value: value of parameter
803-
"""
804-
warnings.warn('Method renamed to API.set_url_parameter(). To be removed in version 1.9.0',
805-
DeprecationWarning)
806-
return self.set_url_parameter(name, value)
807-
808720
def set_url_parameter(self, name, value):
809721
"""
810722
Sets a URL parameter
@@ -817,18 +729,6 @@ def set_url_parameter(self, name, value):
817729
else:
818730
self.url_parameters[name] = value
819731

820-
def getUrlParameter(self, name):
821-
"""
822-
Gets a URL parameter
823-
824-
@param name: name of parameter
825-
826-
@return: value of parameter
827-
"""
828-
warnings.warn('Method renamed to API.get_url_parameter(). To be removed in version 1.9.0',
829-
DeprecationWarning)
830-
return self.get_url_parameter(name)
831-
832732
def get_url_parameter(self, name):
833733
"""
834734
Gets a URL parameter
@@ -842,31 +742,12 @@ def get_url_parameter(self, name):
842742
else:
843743
return None
844744

845-
def clearUrlParameters(self):
846-
"""
847-
Clears all options
848-
"""
849-
warnings.warn('Method renamed to API.clear_url_parameters(). '
850-
'To be removed in version 1.9.0',
851-
DeprecationWarning)
852-
self.clear_url_parameters()
853-
854745
def clear_url_parameters(self):
855746
"""
856747
Clears all options
857748
"""
858749
self.url_parameters.clear()
859750

860-
def setCustomHeaders(self, name, value):
861-
"""
862-
Sets custom headers
863-
864-
@param headers: array of custom headers to be set
865-
"""
866-
warnings.warn('Method renamed to API.set_custom_headers(). To be removed in version 1.9.0',
867-
DeprecationWarning)
868-
return self.set_custom_headers(name, value)
869-
870751
def set_custom_headers(self, name, value):
871752
"""
872753
Sets custom headers
@@ -878,29 +759,12 @@ def set_custom_headers(self, name, value):
878759
else:
879760
self.custom_headers[name] = value
880761

881-
def getCustomHeaders(self):
882-
"""
883-
Get custom headers
884-
"""
885-
warnings.warn('Method renamed to API.get_custom_headers(). To be removed in version 1.9.0',
886-
DeprecationWarning)
887-
return self.get_custom_headers()
888-
889762
def get_custom_headers(self):
890763
"""
891764
Get custom headers
892765
"""
893766
return self.custom_headers
894767

895-
def clearCustomHeaders(self):
896-
"""
897-
Clears custom headers
898-
"""
899-
warnings.warn('Method renamed to API.clear_custom_headers(). '
900-
'To be removed in version 1.9.0',
901-
DeprecationWarning)
902-
self.clear_custom_headers()
903-
904768
def clear_custom_headers(self):
905769
"""
906770
Clears custom headers

0 commit comments

Comments
 (0)