Example #1
0
# Go through each file, open it, and add its content to the list
for myFile in filesList:
    with open(os.path.join(directory, myFile), 'r') as f:
        # Here, the JSON is converted back to a Python object
        transcript = json.load(f)
    transcripts.append(transcript)

# Create lists for the years and the length of the text for each year.
years = []
lengths = []

# Go through each transcript
for transcript in transcripts:

    # Get the date - converting the ISO date back into a datetime.date object
    date = cf.iso_to_datetime(transcript['date'])
    # Convert the year into a campaign year
    year = cf.campaign_year_from_year(date.year)

    years.append(year)

    # Create a string for all of the text in the debate
    allText = ""

    # Add all the text spoken by speakers to that string
    for speaker in transcript['text_by_speakers']:

        allText += (" " + speaker['text'])

    # removes punctuation, digits, splits text into words
    # remove words shorter than 3 characters and suffixes
Example #2
0
    with open(os.path.join(directory, myFile), 'r') as f:
        # Here, the JSON is converted back to a Python object
        transcript = json.load(f)
    transcripts.append(transcript)

all_list = []

# Loop through the three 'parties': 'r' - republican; 'd' - democrat; 't' - anything else
for winnerloser in ['w', 'l']:

    transcript_results = []
    # Go through each transcript
    for transcript in transcripts:

        # Get the date - converting the ISO date back into a datetime.date object
        year = cf.iso_to_datetime(transcript['date']).year

        # Create a string for all of the text in the debate
        allText = ""

        # Add all the text spoken by speakers to that string
        for speaker in transcript['text_by_speakers']:
            if 'winnerloser' in speaker:
                if speaker['winnerloser'] == winnerloser or (
                    (speaker['winnerloser'] == 'n' or speaker['winnerloser']
                     == 'd' or speaker['winnerloser'] == 'r')
                        and winnerloser == 'l'):
                    print winnerloser
                    print cf.unicode_to_ascii(transcript['description'])
                    allText += (" " + speaker['text'])
        username = '******'
    else:
        import_filename = 'realDonaldTrumpTweetsResults.json'
        export_filename = 'images/twitter-analysis-sentiment-time-trump.svg'
        username = '******'

    export_filename = os.path.join(root_directory, export_filename)

    title = 'Positive and negative words in tweets - @' + username

    with open(import_filename, 'r') as f:
        statuses = json.load(f)

    results = {}
    for status in statuses:
        month = cf.iso_to_datetime(status['date']).month
        year = cf.iso_to_datetime(status['date']).year
        if year == 2016:
            if month not in results:
                results[month] = [status]
            else:
                results[month].append(status)

    positive_results = [
        (month_no,
         cf.mean([status['total_pos_words'] for status in results[month_no]]))
        for month_no in results
    ]
    negative_results = [
        (month_no,
         cf.mean([status['total_neg_words'] for status in results[month_no]]))