forked from rosette-api/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentiment.py
More file actions
46 lines (33 loc) · 1.69 KB
/
sentiment.py
File metadata and controls
46 lines (33 loc) · 1.69 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
# -*- coding: utf-8 -*-
"""
Example code to call Rosette API to get the sentiment of a local file.
"""
import argparse
import json
import os
import tempfile
from rosette.api import API, DocumentParameters
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
# Create default file to read from
f = tempfile.NamedTemporaryFile(suffix=".html")
sentiment_file_data = "<html><head><title>New Ghostbusters Film</title></head><body><p>Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, “The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.”</p></body></html>"
message = sentiment_file_data
f.write(message)
f.seek(0)
# Create an API instance
api = API(user_key=key, service_url=altUrl)
params = DocumentParameters()
params["language"] = "eng"
# Use an HTML file to load data instead of a string
params.load_document_file(f.name)
result = api.sentiment(params)
# Clean up the file
f.close()
return result
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"))