Skip to content

Commit fede73f

Browse files
cp2bostonLi Xu
authored andcommitted
RCB-527 topics (#71)
* RCB-527 Topics - Added topics to Api.py - Created topics example - Code cleanup on all examples - Passed against jugmaster * Updated README - cleaned up obsolete instructions - added list of available endpoints - added release notes reference * Updated examples README * Added topics to unit tests
1 parent 4a9953e commit fede73f

25 files changed

+409
-310
lines changed

README.md

Lines changed: 31 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
[![Build Status](https://travis-ci.org/rosette-api/python.svg?branch=master)](https://travis-ci.org/rosette-api/python)
22

3-
# This is the Python client binding for Rosette API.
4-
See the wiki for more information.
3+
## This is the Python client binding for Rosette API.
4+
Please check out the [wiki](https://github.com/rosette-api/python/wiki) for additional information
55

6-
Installation
7-
------------
6+
### Installation
87

98
The Python binding requires Python 2.6 or greater and is available through pip:
109

@@ -19,79 +18,30 @@ To check your installed version:
1918

2019
`pip show rosette_api`
2120

22-
Basic Usage
23-
-----------
24-
25-
```python
26-
# 1. Set utf-8 encoding.
27-
# -*- coding: utf-8 -*-
28-
29-
# 2. Imports from rosette.api.
30-
from rosette.api import API, DocumentParameters, MorphologyOutput
31-
32-
# 3. Create API object.
33-
api = API("[your_api-key]")
34-
35-
# 4. Create parameters object
36-
params = DocumentParameters()
37-
38-
# 5. Set parameters.
39-
params["content"] = "The quick brown fox jumped over the lazy dog. Yes he did."
40-
41-
# 6. Make a call.
42-
result = api.morphology(params)
43-
44-
# result is a Python dictionary that contains
45-
46-
{u'tokens': [u'The', u'quick', u'brown', u'fox', u'jumped', u'over', u'the', u'lazy', u'dog', u'.', u'Yes', u'he', u'did', u'.'], u'posTags': [u'DET', u'ADJ', u'ADJ', u'NOUN', u'VERB', u'ADP', u'DET', u'ADJ', u'NOUN', u'PUNCT', u'VERB', u'PRON', u'VERB', u'PUNCT'], u'compoundComponents': [None, None, None, None, None, None, None, None, None, None, None, None, None, None], u'lemmas': [u'the', u'quick', u'brown', u'fox', u'jump', u'over', u'the', u'lazy', u'dog', u'.', u'yes', u'he', u'do', u'.'], u'hanReadings': [None, None, None, None, None, None, None, None, None, None, None, None, None, None]}
47-
```
48-
49-
The samples use the following procedure:
50-
51-
1. If the application reads text in, set encoding to utf-8 in the first line of the script.
52-
53-
2. Import the `rosette.api` packages that your application needs. The `rosette.api` packages include
54-
* `API`
55-
* `DocumentParameters`
56-
* `NameSimilarityParameters`
57-
* `NameTranslationParameters`
58-
* `MorphologyOutput`
59-
* `DataFormat`
60-
61-
3. Create an `API` object with the `user_key` parameter.
62-
63-
4. Create a parameters object for your request input:
64-
65-
| Parameter | Endpoint |
66-
| ----|----|
67-
| `NameSimilarityParameters` | for `/name-similarity` |
68-
| `NameTranslationParameters` | for `/translated-name` |
69-
| `DocumentParameters` | for all other endpoints |
70-
71-
72-
5. Set the parameters required for your operation: "`content`" or "`contentUri`" for `DocumentParameters`;
73-
"`name`" and "`targetLanguage`" for `NameTranslationParameters`; "`name1.text`" and "`name2.text`" for
74-
`NameSimilarityParameters`; Other parameters are optional.
75-
76-
6. Invoke the `API` method for the endpoint you are calling. The methods are
77-
* `entities()`
78-
* `categories()`
79-
* `sentiment()`
80-
* `language()`
81-
* `morphology(tag)` where tag is a member of `MorphologyOutput`: `LEMMAS`, `PARTS_OF_SPEECH`, `COMPOUND_COMPONENTS`, `HAN_READINGS`, or `COMPLETE`. An empty tag is equivalent to `COMPLETE`.
82-
* `sentences()`
83-
* `tokens()`
84-
* `relationships()`
85-
* `name_translation()`
86-
* `name_similarity()`
87-
* `matched_name()` *deprecated
88-
* `translated_name()` *deprecated
89-
90-
7. The API will return a dictionary with the results.
91-
92-
See [examples](examples) for more request samples.
93-
94-
## Docker ##
21+
### Basic Usage
22+
23+
For help in how to call the various endpoints, please refer to the [examples](https://github.com/rosette-api/python/tree/develop/examples).
24+
25+
### Supported Endpoints
26+
- categories
27+
- entities
28+
- info
29+
- language
30+
- morphology (complete, compound components, han readings, lemmas, parts of speech)
31+
- name deduplication
32+
- name similarity
33+
- name translation
34+
- ping
35+
- relationships
36+
- sentences
37+
- sentiment
38+
- syntax dependencies
39+
- text embedding
40+
- tokens
41+
- topics
42+
- transliteration
43+
44+
### Docker
9545
A Docker image for running the examples against the compiled source library is available on Docker Hub.
9646

9747
Command: `docker run -e API_KEY=api-key -v "<binding root directory>:/source" rosetteapi/docker-python`
@@ -100,12 +50,11 @@ Additional environment settings:
10050
`-e ALT_URL=<alternative URL>`
10151
`-e FILENAME=<single filename>`
10252

103-
API Documentation
104-
-----------------
105-
53+
### API Documentation
10654
See [documentation](http://rosette-api.github.io/python)
10755

108-
Additional Information
109-
----------------------
56+
### Release Notes
57+
See [wiki](https://github.com/rosette-api/python/wiki/Release-Notes)
11058

59+
### Additional Information
11160
Visit [Rosette API site](https://developer.rosette.com)

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Each example, when run, prints its output to the console.
3838
| sentences.py | Gets the sentences from a piece of text |
3939
| sentiment.py | Gets the sentiment of a local file |
4040
| tokens.py | Gets the tokens (words) from a piece of text |
41+
| topics.py | Returns key phrases and concepts from provided content |
4142
| translated-name.py | Translates a name from one language to another |
4243
| transliteration.py | Transliterates the given text |
4344

examples/categories.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,40 @@
33
"""
44
Example code to call Rosette API to get the category of a document (at a given URL).
55
"""
6+
from __future__ import print_function
67

78
import argparse
89
import json
910
import os
1011

12+
1113
from rosette.api import API, DocumentParameters, RosetteException
1214

1315

14-
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
16+
def run(key, alt_url='https://api.rosette.com/rest/v1/'):
17+
""" Run the example """
1518
categories_url_data = "http://www.onlocationvacations.com/2015/03/05/the-new-ghostbusters-movie-begins-filming-in-boston-in-june/"
1619
url = categories_url_data
1720
# Create an API instance
18-
api = API(user_key=key, service_url=altUrl)
21+
api = API(user_key=key, service_url=alt_url)
1922
params = DocumentParameters()
2023

2124
# Use a URL to input data instead of a string
2225
params["contentUri"] = url
2326
try:
2427
return api.categories(params)
25-
except RosetteException as e:
26-
print(e)
28+
except RosetteException as exception:
29+
print(exception)
2730

2831

29-
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')
30-
parser.add_argument('-k', '--key', help='Rosette API Key', required=True)
31-
parser.add_argument('-u', '--url', help="Alternative API URL", default='https://api.rosette.com/rest/v1/')
32+
PARSER = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
33+
description='Calls the ' +
34+
os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')
35+
PARSER.add_argument('-k', '--key', help='Rosette API Key', required=True)
36+
PARSER.add_argument('-u', '--url', help="Alternative API URL",
37+
default='https://api.rosette.com/rest/v1/')
3238

3339
if __name__ == '__main__':
34-
args = parser.parse_args()
35-
result = run(args.key, args.url)
36-
print(json.dumps(result, indent=2, ensure_ascii=False, sort_keys=True).encode("utf8"))
40+
ARGS = PARSER.parse_args()
41+
RESULT = run(ARGS.key, ARGS.url)
42+
print(json.dumps(RESULT, indent=2, ensure_ascii=False, sort_keys=True).encode("utf8"))

examples/entities.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# -*- coding: utf-8 -*-
2-
32
"""
43
Example code to call Rosette API to get entities from a piece of text.
54
"""
5+
from __future__ import print_function
66

77
import argparse
88
import json
@@ -11,23 +11,27 @@
1111
from rosette.api import API, DocumentParameters, RosetteException
1212

1313

14-
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
14+
def run(key, alt_url='https://api.rosette.com/rest/v1/'):
15+
""" Run the example """
1516
# Create an API instance
16-
api = API(user_key=key, service_url=altUrl)
17+
api = API(user_key=key, service_url=alt_url)
1718
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"
1819
params = DocumentParameters()
1920
params["content"] = entities_text_data
2021
params["genre"] = "social-media"
2122
try:
2223
return api.entities(params)
23-
except RosetteException as e:
24-
print(e)
24+
except RosetteException as exception:
25+
print(exception)
2526

26-
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')
27-
parser.add_argument('-k', '--key', help='Rosette API Key', required=True)
28-
parser.add_argument('-u', '--url', help="Alternative API URL", default='https://api.rosette.com/rest/v1/')
27+
PARSER = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
28+
description='Calls the ' +
29+
os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')
30+
PARSER.add_argument('-k', '--key', help='Rosette API Key', required=True)
31+
PARSER.add_argument('-u', '--url', help="Alternative API URL",
32+
default='https://api.rosette.com/rest/v1/')
2933

3034
if __name__ == '__main__':
31-
args = parser.parse_args()
32-
result = run(args.key, args.url)
33-
print(json.dumps(result, indent=2, ensure_ascii=False, sort_keys=True).encode("utf8"))
35+
ARGS = PARSER.parse_args()
36+
RESULT = run(ARGS.key, ARGS.url)
37+
print(json.dumps(RESULT, indent=2, ensure_ascii=False, sort_keys=True).encode("utf8"))

examples/info.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# -*- coding: utf-8 -*-
2-
32
"""
43
Example code to call Rosette API to get information such as version and build
54
"""
5+
from __future__ import print_function
66

77
import argparse
88
import json
@@ -11,21 +11,25 @@
1111
from rosette.api import API, RosetteException
1212

1313

14-
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
14+
def run(key, alt_url='https://api.rosette.com/rest/v1/'):
15+
""" Run the example """
1516
# Create an API instance
16-
api = API(user_key=key, service_url=altUrl)
17+
api = API(user_key=key, service_url=alt_url)
1718

1819
try:
1920
return api.info()
20-
except RosetteException as e:
21-
print(e)
21+
except RosetteException as exception:
22+
print(exception)
2223

2324

24-
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')
25-
parser.add_argument('-k', '--key', help='Rosette API Key', required=True)
26-
parser.add_argument('-u', '--url', help="Alternative API URL", default='https://api.rosette.com/rest/v1/')
25+
PARSER = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
26+
description='Calls the ' +
27+
os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')
28+
PARSER.add_argument('-k', '--key', help='Rosette API Key', required=True)
29+
PARSER.add_argument('-u', '--url', help="Alternative API URL",
30+
default='https://api.rosette.com/rest/v1/')
2731

2832
if __name__ == '__main__':
29-
args = parser.parse_args()
30-
result = run(args.key, args.url)
31-
print(json.dumps(result, indent=2, ensure_ascii=False, sort_keys=True).encode("utf8"))
33+
ARGS = PARSER.parse_args()
34+
RESULT = run(ARGS.key, ARGS.url)
35+
print(json.dumps(RESULT, indent=2, ensure_ascii=False, sort_keys=True).encode("utf8"))

examples/language.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# -*- coding: utf-8 -*-
2-
32
"""
43
Example code to call Rosette API to determine the language of a piece of text.
54
"""
5+
from __future__ import print_function
66

77
import argparse
88
import json
@@ -11,25 +11,29 @@
1111
from rosette.api import API, DocumentParameters, RosetteException
1212

1313

14-
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
14+
def run(key, alt_url='https://api.rosette.com/rest/v1/'):
15+
""" Run the example """
1516
# Create an API instance
16-
api = API(user_key=key, service_url=altUrl)
17+
api = API(user_key=key, service_url=alt_url)
1718

1819
language_data = "Por favor Señorita, says the man."
1920
params = DocumentParameters()
2021
params["content"] = language_data
2122
api.set_custom_headers("X-RosetteAPI-App", "python-app")
2223
try:
2324
return api.language(params)
24-
except RosetteException as e:
25-
print(e)
25+
except RosetteException as exception:
26+
print(exception)
2627

2728

28-
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')
29-
parser.add_argument('-k', '--key', help='Rosette API Key', required=True)
30-
parser.add_argument('-u', '--url', help="Alternative API URL", default='https://api.rosette.com/rest/v1/')
29+
PARSER = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
30+
description='Calls the ' +
31+
os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')
32+
PARSER.add_argument('-k', '--key', help='Rosette API Key', required=True)
33+
PARSER.add_argument('-u', '--url', help="Alternative API URL",
34+
default='https://api.rosette.com/rest/v1/')
3135

3236
if __name__ == '__main__':
33-
args = parser.parse_args()
34-
result = run(args.key, args.url)
35-
print(json.dumps(result, indent=2, ensure_ascii=False, sort_keys=True).encode("utf8"))
37+
ARGS = PARSER.parse_args()
38+
RESULT = run(ARGS.key, ARGS.url)
39+
print(json.dumps(RESULT, indent=2, ensure_ascii=False, sort_keys=True).encode("utf8"))

examples/morphology_complete.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# -*- coding: utf-8 -*-
2-
32
"""
43
Example code to call Rosette API to get the complete morphological analysis of a piece of text.
54
"""
5+
from __future__ import print_function
66

77
import argparse
88
import json
@@ -11,24 +11,28 @@
1111
from rosette.api import API, DocumentParameters, RosetteException
1212

1313

14-
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
14+
def run(key, alt_url='https://api.rosette.com/rest/v1/'):
15+
""" Run the example """
1516
# Create an API instance
16-
api = API(user_key=key, service_url=altUrl)
17+
api = API(user_key=key, service_url=alt_url)
1718

1819
morphology_complete_data = "The quick brown fox jumped over the lazy dog. Yes he did."
1920
params = DocumentParameters()
2021
params["content"] = morphology_complete_data
2122
try:
2223
return api.morphology(params)
23-
except RosetteException as e:
24-
print(e)
24+
except RosetteException as exception:
25+
print(exception)
2526

2627

27-
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')
28-
parser.add_argument('-k', '--key', help='Rosette API Key', required=True)
29-
parser.add_argument('-u', '--url', help="Alternative API URL", default='https://api.rosette.com/rest/v1/')
28+
PARSER = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
29+
description='Calls the ' +
30+
os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')
31+
PARSER.add_argument('-k', '--key', help='Rosette API Key', required=True)
32+
PARSER.add_argument('-u', '--url', help="Alternative API URL",
33+
default='https://api.rosette.com/rest/v1/')
3034

3135
if __name__ == '__main__':
32-
args = parser.parse_args()
33-
result = run(args.key, args.url)
34-
print(json.dumps(result, indent=2, ensure_ascii=False, sort_keys=True).encode("utf8"))
36+
ARGS = PARSER.parse_args()
37+
RESULT = run(ARGS.key, ARGS.url)
38+
print(json.dumps(RESULT, indent=2, ensure_ascii=False, sort_keys=True).encode("utf8"))

0 commit comments

Comments
 (0)