-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext_tagging.py
More file actions
39 lines (31 loc) · 1.17 KB
/
text_tagging.py
File metadata and controls
39 lines (31 loc) · 1.17 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
#!/usr/bin/env python
import sys
import requests
import os
import json
calais_url = 'https://api.thomsonreuters.com/permid/calais'
access_token = '<Access token>'
def get_tags_util(input_string, headers):
response = requests.post(calais_url, data=input_string, headers=headers, timeout=80)
# print ('status code: %s' % response.status_code)
content = json.loads(response.text)
topics = []
for key, value in content.items():
if '_typeGroup' in value and value['_typeGroup'] == 'topics':
topics.append(str(value['name']))
if response.status_code == 200:
return topics
#
# Take a text/paragraph as input from the user, and return the relevant topics, the text is related to.
#
# Input: Welcome to Bangalore!
# Output: ['Religion_Belief', 'Technology_Internet', 'Sports', 'Hospitality_Recreation', 'Business_Finance']
#
def get_tags(input_string):
try:
headers = {'X-AG-Access-Token': access_token, 'Content-Type': 'text/raw', 'outputformat': 'application/json'}
tags = get_tags_util(input_string, headers)
return tags
except Exception, e:
print 'Error in connect ', e
print get_tags(raw_input())