Skip to content

Commit d18d3d6

Browse files
iredpathguruxu
authored andcommitted
RD-2427 related terms endpoint (#84)
* RD-2427 related terms endpoint * RD-2427 missing newline * RD-2427 related_terms -> similar_terms, add semantic_vectors * RD-2427 similar terms unit test, checkstyle
1 parent 97b339a commit d18d3d6

4 files changed

Lines changed: 94 additions & 6 deletions

File tree

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@ 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+
19+
# Set selected API options.
20+
# For more information on the functionality of these
21+
# and other available options, see Rosette Features & Functions
22+
# https://developer.rosette.com/features-and-functions#semantic-vectors
23+
24+
# api.set_option('perToken', 'true')
25+
1826
embeddings_data = "Cambridge, Massachusetts"
1927
params = DocumentParameters()
2028
params["content"] = embeddings_data
2129
try:
22-
return api.text_embedding(params)
30+
return api.semantic_vectors(params)
2331
except RosetteException as exception:
2432
print(exception)
2533

examples/similar_terms.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Example code to call Rosette API to get similar terms for an input.
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+
# Set selected API options.
20+
# For more information on the functionality of these
21+
# and other available options, see Rosette Features & Functions
22+
# https://developer.rosette.com/features-and-functions#similar-terms
23+
24+
api.set_option("resultLanguages", ['spa', 'deu', 'jpn'])
25+
26+
term = "spy"
27+
params = DocumentParameters()
28+
params["content"] = term
29+
try:
30+
return api.similar_terms(params)
31+
except RosetteException as exception:
32+
print(exception)
33+
34+
35+
PARSER = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
36+
description='Calls the ' +
37+
os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')
38+
PARSER.add_argument('-k', '--key', help='Rosette API Key', required=True)
39+
PARSER.add_argument('-u', '--url', help="Alternative API URL",
40+
default='https://api.rosette.com/rest/v1/')
41+
42+
if __name__ == '__main__':
43+
ARGS = PARSER.parse_args()
44+
RESULT = run(ARGS.key, ARGS.url)
45+
print(json.dumps(RESULT, indent=2, ensure_ascii=False,
46+
sort_keys=True).encode("utf8"))

rosette/api.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,10 +560,12 @@ def __init__(
560560
'NAME_DEDUPLICATION': 'name-deduplication',
561561
'PING': 'ping',
562562
'RELATIONSHIPS': 'relationships',
563+
'SEMANTIC_VECTORS': 'semantics/vector',
563564
'SENTENCES': 'sentences',
564565
'SENTIMENT': 'sentiment',
566+
'SIMILAR_TERMS': 'semantics/similar',
565567
'SYNTAX_DEPENDENCIES': 'syntax/dependencies',
566-
'TEXT_EMBEDDING': 'text-embedding',
568+
'TEXT_EMBEDDING': 'semantics/vector',
567569
'TOKENS': 'tokens',
568570
'TOPICS': 'topics',
569571
'TRANSLITERATION': 'transliteration'
@@ -932,6 +934,14 @@ def text_embedding(self, parameters):
932934
@return: A python dictionary containing the results of text embedding."""
933935
return EndpointCaller(self, self.endpoints['TEXT_EMBEDDING']).call(parameters)
934936

937+
def semantic_vectors(self, parameters):
938+
"""
939+
Create an L{EndpointCaller} to identify text vectors found in the texts
940+
to which it is applied and call it.
941+
@type parameters: L{DocumentParameters} or L{str}
942+
@return: A python dictionary containing the results of semantic vectors."""
943+
return EndpointCaller(self, self.endpoints['SEMANTIC_VECTORS']).call(parameters)
944+
935945
def syntax_dependencies(self, parameters):
936946
"""
937947
Create an L{EndpointCaller} to identify the syntactic dependencies in the texts
@@ -954,3 +964,12 @@ def topics(self, parameters):
954964
@type parameters: DocumentParameters
955965
@return; A python dictionary containing the results"""
956966
return EndpointCaller(self, self.endpoints['TOPICS']).call(parameters)
967+
968+
def similar_terms(self, parameters):
969+
"""
970+
Create an L{EndpointCaller} to identify terms most similar to the input in
971+
the requested languages
972+
:param parameters: DocumentParameters
973+
:return: A python dictionary containing the similar terms and their similarity
974+
"""
975+
return EndpointCaller(self, self.endpoints['SIMILAR_TERMS']).call(parameters)

tests/test_rosette_api.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,13 +649,13 @@ def test_for_name_translation_required_parameters(api, json_response):
649649
httpretty.reset()
650650

651651

652-
def test_the_text_embedded_endpoint(api, json_response, doc_params):
653-
"""Test text embedded endpoint"""
652+
def test_the_semantic_vectors_endpoint(api, json_response, doc_params):
653+
"""Test semantic vectors endpoint"""
654654
httpretty.enable()
655-
httpretty.register_uri(httpretty.POST, "https://api.rosette.com/rest/v1/text-embedding",
655+
httpretty.register_uri(httpretty.POST, "https://api.rosette.com/rest/v1/semantics/vector",
656656
body=json_response, status=200, content_type="application/json")
657657

658-
result = api.text_embedding(doc_params)
658+
result = api.semantic_vectors(doc_params)
659659
assert result["name"] == "Rosette API"
660660
httpretty.disable()
661661
httpretty.reset()
@@ -705,3 +705,18 @@ def test_the_topics_endpoint(api, json_response, doc_params):
705705
assert result["name"] == "Rosette API"
706706
httpretty.disable()
707707
httpretty.reset()
708+
709+
710+
# Test the similar-terms endpoint
711+
712+
def test_the_similar_terms_endpoint(api, json_response, doc_params):
713+
"""Test the similar terms endpoint"""
714+
httpretty.enable()
715+
httpretty.register_uri(httpretty.POST, "https://api.rosette.com/rest/v1/semantics/similar",
716+
body=json_response, status=200, content_type="application/json")
717+
718+
api.set_option("resultLanguages", ["spa", "jpn", "deu"])
719+
result = api.similar_terms(doc_params)
720+
assert result["name"] == "Rosette API"
721+
httpretty.disable()
722+
httpretty.reset()

0 commit comments

Comments
 (0)