Skip to content

Commit c9e3b04

Browse files
author
Steve Herschleb
committed
pretty print JSON object and enable sentiment analysis on keywords and entities
1 parent c7f851a commit c9e3b04

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

example.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import alchemyapi
2+
import json
23

34
demo_text = 'Yesterday dumb Bob destroyed my fancy iPhone in beautiful Denver, Colorado. I guess I will have to head over to the Apple Store and buy a new one.'
45
demo_url = 'http://blog.programmableweb.com/2011/09/16/new-api-billionaire-text-extractor-alchemy/'
@@ -41,11 +42,11 @@
4142
print 'Processing text: ', demo_text
4243
print ''
4344

44-
response = alchemyapi.entities('text',demo_text)
45+
response = alchemyapi.entities('text',demo_text, { 'sentiment':1 })
4546

4647
if response['status'] == 'OK':
4748
print '## Response Object ##'
48-
print response
49+
print json.dumps(response, indent=4)
4950

5051

5152
print ''
@@ -54,6 +55,7 @@
5455
print 'text: ', entity['text']
5556
print 'type: ', entity['type']
5657
print 'relevance: ', entity['relevance']
58+
print 'sentiment: ', entity['sentiment']['type'] + ' (' + entity['sentiment']['score'] + ')'
5759
print ''
5860
else:
5961
print 'Error in entity extraction call: ', response['statusInfo']
@@ -76,7 +78,7 @@
7678

7779
if response['status'] == 'OK':
7880
print '## Response Object ##'
79-
print response
81+
print json.dumps(response, indent=4)
8082

8183
print ''
8284
print '## Document Sentiment ##'
@@ -103,14 +105,15 @@
103105

104106
if response['status'] == 'OK':
105107
print '## Response Object ##'
106-
print response
108+
print json.dumps(response, indent=4)
107109

108110

109111
print ''
110112
print '## Keywords ##'
111113
for keyword in response['keywords']:
112114
print 'text: ', keyword['text']
113115
print 'relevance: ', keyword['relevance']
116+
print 'sentiment: ', keyword['sentiment']['type'] + ' (' + keyword['sentiment']['score'] + ')'
114117
print ''
115118
else:
116119
print 'Error in keyword extaction call: ', response['statusInfo']
@@ -133,7 +136,7 @@
133136

134137
if response['status'] == 'OK':
135138
print '## Object ##'
136-
print response
139+
print json.dumps(response, indent=4)
137140

138141

139142
print ''
@@ -163,7 +166,7 @@
163166

164167
if response['status'] == 'OK':
165168
print "## Object ##"
166-
print response
169+
print json.dumps(response, indent=4)
167170

168171

169172
print ''
@@ -200,7 +203,7 @@
200203

201204
if response['status'] == 'OK':
202205
print '## Response Object ##'
203-
print response
206+
print json.dumps(response, indent=4)
204207

205208

206209
print ''
@@ -229,7 +232,7 @@
229232

230233
if response['status'] == 'OK':
231234
print '## Response Object ##'
232-
print response
235+
print json.dumps(response, indent=4)
233236

234237

235238
print ''
@@ -259,7 +262,7 @@
259262

260263
if response['status'] == 'OK':
261264
print '## Response Object ##'
262-
print response
265+
print json.dumps(response, indent=4)
263266

264267
print ''
265268
print '## Author ##'
@@ -286,7 +289,7 @@
286289

287290
if response['status'] == 'OK':
288291
print '## Response Object ##'
289-
print response
292+
print json.dumps(response, indent=4)
290293

291294
print ''
292295
print '## Feeds ##'

0 commit comments

Comments
 (0)