Skip to content

Commit 5a4155d

Browse files
authored
Merge pull request #55 from cp2boston/RCB-464_text-embedding
text-embedding added
2 parents ba080b3 + f9e7586 commit 5a4155d

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

examples/text_embedding.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
Example code to call Rosette API to get text vectors 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+
embeddings_data = "Cambridge, Massachusetts"
18+
params = DocumentParameters()
19+
params["content"] = embeddings_data
20+
return api.text_embedding(params)
21+
22+
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')
23+
parser.add_argument('-k', '--key', help='Rosette API Key', required=True)
24+
parser.add_argument('-u', '--url', help="Alternative API URL", default='https://api.rosette.com/rest/v1/')
25+
26+
if __name__ == '__main__':
27+
args = parser.parse_args()
28+
result = run(args.key, args.url)
29+
print(json.dumps(result, indent=2, ensure_ascii=False, sort_keys=True).encode("utf8"))

rosette/api.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import sys
2525
import time
2626
import os
27-
from socket import gaierror
2827
import requests
2928
import re
3029
import warnings
@@ -35,15 +34,6 @@
3534
_IsPy3 = sys.version_info[0] == 3
3635

3736

38-
try:
39-
import urlparse
40-
except ImportError:
41-
import urllib.parse as urlparse
42-
try:
43-
import httplib
44-
except ImportError:
45-
import http.client as httplib
46-
4737
if _IsPy3:
4838
_GZIP_SIGNATURE = _GZIP_BYTEARRAY
4939
else:
@@ -843,3 +833,11 @@ def matched_name(self, parameters):
843833
@type parameters: L{NameSimilarityParameters}
844834
@return: A python dictionary containing the results of name matching."""
845835
return self.name_similarity(parameters)
836+
837+
def text_embedding(self, parameters):
838+
"""
839+
Create an L{EndpointCaller} to identify text vectors found in the texts
840+
to which it is applied and call it.
841+
@type parameters: L{DocumentParameters} or L{str}
842+
@return: A python dictionary containing the results of text embedding."""
843+
return EndpointCaller(self, "text-embedding").call(parameters)

tests/test_rosette_api.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,3 +579,14 @@ def test_for_name_translation_required_parameters(api, json_response):
579579

580580
httpretty.disable()
581581
httpretty.reset()
582+
583+
584+
def test_the_text_embedded_endpoint(api, json_response, doc_params):
585+
httpretty.enable()
586+
httpretty.register_uri(httpretty.POST, "https://api.rosette.com/rest/v1/text-embedding",
587+
body=json_response, status=200, content_type="application/json")
588+
589+
result = api.text_embedding(doc_params)
590+
assert result["name"] == "Rosette API"
591+
httpretty.disable()
592+
httpretty.reset()

0 commit comments

Comments
 (0)