Quinton Jackson
Quinton Jackson

Reputation: 49

Python - Parsing Json Response from IBM Watson Tone Analyzer

I am having an issue parsing my JSON Response from IBM Watson's Tone Analyzer. I understand that I need to enter the dictionary and then move through each key until I get to the information that I need.

Current Code

from __future__ import print_function
import json
import sys
from os.path import join, dirname
from watson_developer_cloud import ToneAnalyzerV3
from musixmatch import Musixmatch

musixmatch = Musixmatch('')

tone_analyzer = ToneAnalyzerV3(
    username = "",
    password = "",
    version = '',
    url = "https://gateway.watsonplatform.net/tone-analyzer/api" )

lyric = lyricwikia.get_lyrics('Dragonforce', 'Through the fire and flames')



fh = open('lyrics.txt', 'w+')

fh.write(lyric)

fh.close()


f = open('lyrics.txt', 'r+')

print("\ntone_chat() Dragonforce - Through the Fire and Flames:\n")

m = []

for line in f.readlines():

    content_type = 'application/json'
    tone = tone_analyzer.tone({"text": line}, content_type)
    jsonText = json.dumps(tone, indent=2)
    jsonParse = json.loads(jsonText)
    m = jsonParse["document_tone"]["tones"]
    print(m)

f.close()

I am trying to figure a way to get back multiple results from this JSON Response and store it in a dictionary as key pairs but I'm having a hard time figuring out how to go about it.

Current Output

tone_chat() Dragonforce - Through the Fire and Flames:

[]
[{'score': 0.511165, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[{'score': 0.697052, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[]
[{'score': 0.511165, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[{'score': 0.697052, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[{'score': 0.816009, 'tone_id': 'joy', 'tone_name': 'Joy'}, {'score': 
0.822231, 'tone_id': 'tentative', 'tone_name': 'Tentative'}]
[]
[{'score': 0.518879, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[{'score': 0.619897, 'tone_id': 'anger', 'tone_name': 'Anger'}]
[{'score': 0.608597, 'tone_id': 'anger', 'tone_name': 'Anger'}]
[{'score': 0.865077, 'tone_id': 'sadness', 'tone_name': 'Sadness'}, 
{'score': 0.724236, 'tone_id': 'analytical', 'tone_name': 'Analytical'}]
[]
[]
[]
[]
[{'score': 0.557844, 'tone_id': 'joy', 'tone_name': 'Joy'}, {'score': 
0.898327, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[]
[{'score': 0.695561, 'tone_id': 'sadness', 'tone_name': 'Sadness'}, 
{'score': 0.895415, 'tone_id': 'analytical', 'tone_name': 'Analytical'}]
[{'score': 0.616244, 'tone_id': 'sadness', 'tone_name': 'Sadness'}, 
{'score': 0.898327, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[{'score': 0.859366, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[]
[]
[{'score': 0.621329, 'tone_id': 'joy', 'tone_name': 'Joy'}]
[{'score': 0.743111, 'tone_id': 'joy', 'tone_name': 'Joy'}]
[]
[{'score': 0.612119, 'tone_id': 'joy', 'tone_name': 'Joy'}]
[]
[{'score': 0.524931, 'tone_id': 'joy', 'tone_name': 'Joy'}, {'score': 
0.786991, 'tone_id': 'tentative', 'tone_name': 'Tentative'}]
[{'score': 0.92125, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[]
[{'score': 0.8152, 'tone_id': 'analytical', 'tone_name': 'Analytical'}, 
{'score': 0.618451, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[{'score': 0.638092, 'tone_id': 'sadness', 'tone_name': 'Sadness'}, 
{'score': 0.874372, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[{'score': 0.707601, 'tone_id': 'sadness', 'tone_name': 'Sadness'}, 
{'score': 0.687768, 'tone_id': 'analytical', 'tone_name': 'Analytical'}, 
{'score': 0.618451, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[{'score': 0.704642, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[]
[{'score': 0.695561, 'tone_id': 'sadness', 'tone_name': 'Sadness'}, 
{'score': 0.895415, 'tone_id': 'analytical', 'tone_name': 'Analytical'}]
[{'score': 0.616244, 'tone_id': 'sadness', 'tone_name': 'Sadness'}, 
{'score': 0.898327, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[{'score': 0.859366, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[]
[]
[{'score': 0.516628, 'tone_id': 'fear', 'tone_name': 'Fear'}]
[{'score': 0.524235, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[{'score': 0.52202, 'tone_id': 'sadness', 'tone_name': 'Sadness'}, {'score': 
0.786991, 'tone_id': 'tentative', 'tone_name': 'Tentative'}]
[{'score': 0.961633, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[]
[{'score': 0.695561, 'tone_id': 'sadness', 'tone_name': 'Sadness'}, 
{'score': 0.895415, 'tone_id': 'analytical', 'tone_name': 'Analytical'}]
[{'score': 0.616244, 'tone_id': 'sadness', 'tone_name': 'Sadness'}, 
{'score': 0.898327, 'tone_id': 'confident', 'tone_name': 'Confident'}]
[{'score': 0.859366, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[][{'score': 0.816009, 'tone_id': 'joy', 'tone_name': 'Joy'}, {'score': 
0.822231, 'tone_id': 'tentative', 'tone_name': 'Tentative'}]
[]
[{'score': 0.518879, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[]
[{'score': 0.511165, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[{'score': 0.697052, 'tone_id': 'sadness', 'tone_name': 'Sadness'}]
[{'score': 0.816009, 'tone_id': 'joy', 'tone_name': 'Joy'}, {'score': 
0.822231, 'tone_id': 'tentative', 'tone_name': 'Tentative'}]
[]

I created a dictionary called "emotions" that I was trying to store the 2 values into but it wouldn't work so I tried to at least get the values to print. Would I need to go about this a different way to get the Tone Name and Score stored in a dictionary in key pairs? Thank you for any help!

Upvotes: 2

Views: 631

Answers (1)

Simon O'Doherty
Simon O'Doherty

Reputation: 9349

You have a typo. Also the JSON object is an array, so you need to specify the index.

Example:

for row in jsonText:
    for tones in row['document_tone']['tones']:
        print(' Name: {} . Score: {}'.format(tone['tone_name'], tone['score']))

Upvotes: 0

Related Questions