|
| 1 | +# This is the Python client binding for Rosette API. |
| 2 | + |
| 3 | +The Python binding requires Python 2.6 or greater and is available through pip: |
| 4 | + |
| 5 | +`pip install rosette_api` |
| 6 | + |
| 7 | +```python |
| 8 | +# 1. Set utf-8 encoding. |
| 9 | +# -*- coding: utf-8 -*- |
| 10 | + |
| 11 | +# 2. Imports from rosette.api. |
| 12 | +from rosette.api import API, DocumentParameters, MorphologyOutput |
| 13 | + |
| 14 | +# 3. Create API object. |
| 15 | +api = API("[your_api-key]") |
| 16 | + |
| 17 | +# 4. Create parameters object |
| 18 | +params = DocumentParameters() |
| 19 | + |
| 20 | +# 5. Set parameters. |
| 21 | +params["content"] = u"Was ist so böse an der Europäischen Zentralbank?" |
| 22 | + |
| 23 | +# 6. Make a call. |
| 24 | +result = api.morphology(params) |
| 25 | + |
| 26 | +# result is a Python dictionary that contains |
| 27 | + |
| 28 | +{u'lemmas': [{u'text': u'Was', u'lemma': u'was'}, {u'text': u'ist', u'lemma': u'sein'}, {u'text': u'so', u'lemma': u'so'}, {u'text': u'böse', u'lemma': u'böse'}, {u'text': u'an', u'lemma': u'an'}, {u'text': u'der', u'lemma': u'der'}, {u'text': u'Europäischen', u'lemma': u'europäisch'}, {u'text': u'Zentralbank', u'lemma': u'Zentralbank'}, {u'text': u'?', u'lemma': u'?'}]} |
| 29 | +``` |
| 30 | + |
| 31 | +The samples use the following procedure: |
| 32 | + |
| 33 | +1. If the application reads text in, set encoding to utf-8 in the first line of the script. |
| 34 | + |
| 35 | +2. Import the `rosette.api` packages that your application needs. The `rosette.api` packages include |
| 36 | + * `API` |
| 37 | + * `DocumentParameters` |
| 38 | + * `NameMatchingParameters` |
| 39 | + * `NameTranslationParameters` |
| 40 | + * `MorphologyOutput` |
| 41 | + * `DataFormat` |
| 42 | + * `InputUnit` |
| 43 | + |
| 44 | +3. Create an `API` object with the `user_key` parameter. |
| 45 | + |
| 46 | +4. Create a parameters object for your request input: |
| 47 | + |
| 48 | + | Parameter | Endpoint | |
| 49 | + | ----|----| |
| 50 | + | `NameMatchingParameters` | for `/matched-name` | |
| 51 | + | `NameTranslationParameters` | for `/translated-name` | |
| 52 | + | `DocumentParameters` | for all other endpoints | |
| 53 | + |
| 54 | + |
| 55 | +5. Set the parameters required for your operation: "`content`" or "`contentUri`" for `DocumentParameters`; |
| 56 | +"`name`" and "`targetLanguage`" for `NameTranslationParameters`; "`name1.text`" and "`name2.text`" for |
| 57 | + `NameMatchingParameters`; Other parameters are optional. |
| 58 | + |
| 59 | +6. Create an `API` method for the endpoint you are calling. The methods are |
| 60 | + * `entities(linked)` where `linked` is `False` for entity extraction and `True` for entity linking. |
| 61 | + * `categories()` |
| 62 | + * `sentiment()` |
| 63 | + * `language()` |
| 64 | + * `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`. |
| 65 | + * `sentences()` |
| 66 | + * `tokens()` |
| 67 | + * `matched_name()` |
| 68 | + * `translated_name()` |
| 69 | + |
| 70 | +7. The API will return a dictionary with the results. |
0 commit comments