0

I am running a python script and I am trying to extract a part of it and convert it to JSON file so that I can pass it to Google Visualization.

I have included my python script and output and detailed info.

What I am doing:

I am trying to run this Python script from AlchemyAPI

https://github.com/AlchemyAPI/alchemyapi-twitter-python

My output is/as follows: I am getting the stats from the output.

##########################################################
#    The Tweets                                          #
##########################################################

@uDiZnoGouD
Date: Mon Apr 07 05:07:19 +0000 2014
To enjoy in case you win!
To help you sulk in case you loose!
#IndiavsSriLanka #T20final http://t.co/hRAsIa19zD
Document Sentiment: positive (Score: 0.261738)


##########################################################
#    The Stats                                           #
##########################################################
Document-Level Sentiment:
Positive: 3 (60.00%)
Negative: 1 (20.00%)
Neutral: 1 (20.00%)
Total: 5 (100.00%)

This is the sample code for the stats:

def stats(tweets):
    """
    Calculate and print out some basic summary statistics

    INPUT:
    tweets -> an array containing the analyzed tweets

    """

    #init
    data = {}
    data['doc'] = {}
    data['doc']['positive'] = 0
    data['doc']['negative'] = 0
    data['doc']['neutral'] = 0
    data['doc']['total'] = 0

    data['entity'] = {}
    data['entity']['positive'] = 0
    data['entity']['negative'] = 0
    data['entity']['neutral'] = 0
    data['entity']['total'] = 0

    #loop through the tweets and count up the positive, negatives and neutrals
    for tweet in tweets:
        if 'entity' in tweet['sentiment']:
            data['entity'][tweet['sentiment']['entity']['type']] += 1
            data['entity']['total'] += 1

        if 'doc' in tweet['sentiment']:
            data['doc'][tweet['sentiment']['doc']['type']] += 1
            data['doc']['total'] += 1

    #Make sure there are some analyzed tweets
    if data['doc']['total'] == 0 and data['entity']['total'] == 0:
        print 'No analysis found for the Tweets'
        sys.exit()

    #print the stats
    print ''
    print ''
    print '##########################################################'
    print '#    The Stats                                           #'
    print '##########################################################'
    print ''
    print ''

    if data['entity']['total'] > 0:
        print 'Entity-Level Sentiment:'
        print 'Positive: %d (%.2f%%)' % (data['entity']['positive'], 100.0*data['entity']['positive']/data['entity']['total'])
        print 'Negative: %d (%.2f%%)' % (data['entity']['negative'], 100.0*data['entity']['negative']/data['entity']['total'])
        print 'Neutral: %d (%.2f%%)' % (data['entity']['neutral'], 100.0*data['entity']['neutral']/data['entity']['total'])
        print 'Total: %d (%.2f%%)' % (data['entity']['total'], 100.0*data['entity']['total']/data['entity']['total'])
        print ''
        print ''

    if data['doc']['total'] > 0:
        print 'Document-Level Sentiment:'
        print 'Positive: %d (%.2f%%)' % (data['doc']['positive'], 100.0*data['doc']['positive']/data['doc']['total'])
        print 'Negative: %d (%.2f%%)' % (data['doc']['negative'], 100.0*data['doc']['negative']/data['doc']['total'])
        print 'Neutral: %d (%.2f%%)' % (data['doc']['neutral'], 100.0*data['doc']['neutral']/data['doc']['total'])
        print 'Total: %d (%.2f%%)' % (data['doc']['total'], 100.0*data['doc']['total']/data['doc']['total'])

Problem Statement:

I would like to get the positive, negitive, neutral sentiment in JSON format so that I can pass them to google visualization. How can I make a JSON file which contains my final stats. (Positive, Negitive and Neutral)?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Dark Knight
  • 503
  • 2
  • 12
  • 25
  • put them in a dict and use `json.dumps(dict)` – Tim Apr 10 '14 at 19:44
  • Do you know that Python has a [builtin module](https://docs.python.org/2/library/json.html) to convert back and forth between Python and JSON? As long as you have dicts and lists (content amenable to translation) you can convert back and forth all day long. – Two-Bit Alchemist Apr 10 '14 at 19:44

1 Answers1

1
import json

json_data = json.dumps(data)

if there is something like date time object make sure to convert it into str so that it can be json serializable.

FYI : json.loads used to load it back from json_object.

here make a dict, list or python object.

data_list = []
temp = 'Positive: %d (%.2f%%)' % (data['entity']['positive'], 100.0*data['entity']['positive']/data['entity']['total'])
data_list.append(temp)

temp = print 'Total: %d (%.2f%%)' % (data['entity']['total'], 100.0*data['entity']['total']/data['entity']['total'])
data_list.append(temp)

same as for other data_ques. now you can dump data. json.dumps(data_list)

Note - I think you can do all these stuff after respond in json format. here you have data_dict that contains all information you are just formatting them here instead you can this on clint side of response.

as dict object data contains all info you can dump that only and do formatting on client side which receiving json response.

json.dumps(data)
Roshan
  • 1,459
  • 1
  • 14
  • 20
  • Be sure to use `JSON.stringify` on whatever you are passing back to `json.loads`. – Two-Bit Alchemist Apr 10 '14 at 19:45
  • Excuse my questions. But I am a beginner in python. How can I use json_dumps in my case? `json_data = json.dumps('Positive: %d (%.2f%%)')` .. How can I just get "Positive, Negitive, neutral" document sentiment? – Dark Knight Apr 10 '14 at 20:00