forked from rosette-api/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcategories.py
More file actions
51 lines (38 loc) · 1.72 KB
/
categories.py
File metadata and controls
51 lines (38 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
"""
Example code to call Rosette API to get the category of a document (at a given URL).
"""
from __future__ import print_function
import argparse
import json
import os
from rosette.api import API, DocumentParameters, RosetteException
def run(key, alt_url='https://api.rosette.com/rest/v1/'):
""" Run the example """
categories_url_data = "https://onlocationvacations.com/2018/02/06/downton-abbey-exhibition-extended-april-2-nyc/"
url = categories_url_data
# Create an API instance
api = API(user_key=key, service_url=alt_url)
# Set selected API options
# For more information on the functionality of these
# and other available options, see Rosette Features & Functions
# https://developer.rosette.com/features-and-functions#categorization
# api.set_option('singleLabel', 'true')
# api.set_option('scoreThreshold',- 0.20)
params = DocumentParameters()
# Use a URL to input data instead of a string
params["contentUri"] = url
try:
return api.categories(params)
except RosetteException as exception:
print(exception)
PARSER = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description='Calls the ' +
os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')
PARSER.add_argument('-k', '--key', help='Rosette API Key', required=True)
PARSER.add_argument('-u', '--url', help="Alternative API URL",
default='https://api.rosette.com/rest/v1/')
if __name__ == '__main__':
ARGS = PARSER.parse_args()
RESULT = run(ARGS.key, ARGS.url)
print(json.dumps(RESULT, indent=2, ensure_ascii=False, sort_keys=True).encode("utf8"))