0

My Tweets function can output score and tweet from Tweeter user. However, the output is a bit different to the staff version's output.

I find that some words with first character capitalized are not detected even they exist in the positive-words.txt or negative-words.txt. But I have make all tokens lowercase using the .lower() method.

There may be some other reasons for this problem. I am still thinking. Could anyone help? Thanks in advance.

My tweets code:

#!/usr/bin/env python3

import os
import sys
import helpers
import analyzer
from termcolor import colored

def main():

# ensure proper usage
if len(sys.argv) != 2:
    sys.exit("Usage: ./tweets @screen_name")

# accepts screen name for a user on Twitter without "@"
user = (sys.argv[1]).lstrip("@")

# absolute paths to lists
positives = os.path.join(sys.path[0], "positive-words.txt")
negatives = os.path.join(sys.path[0], "negative-words.txt")

# instantiate analyzer
tweet_analyzer = analyzer.Analyzer(positives, negatives)

# query Twitter’s API for a user’s most recent 50 tweets
tweets = helpers.get_user_timeline(user, 50)

# check if successful
if tweets == None:
    # error message if unsuccessful
    sys.exit("Error! The screen name may not exist or its tweets are private.")

score = 0

# analyze each tweet
for tweet in tweets:
    score = tweet_analyzer.analyze(tweet)
    text = "{0:2d} {1}".format(score, tweet)
    if score > 0.0:
        print(colored(text, "green"))
    elif score < 0.0:
        print(colored(text, "red"))
    else:
        print(colored(text, "yellow"))

    score = 0

if __name__ == "__main__":
    main()

And my analyzer.py code:

import nltk

class Analyzer():
"""Implements sentiment analysis."""

# load positive and negative words
def __init__(self, positives, negatives):
    """Initialize Analyzer."""

    # declare lists for positve and negative words
    self.positives = []
    self.negatives = [] 

    # load words from positive-words.txt
    with open(positives) as positive:
        for line in positive:
            # don't include the comments
            if line.startswith(";") != True:
                # extend self.positives list
                # split on all leading/trailing whitespace
                self.positives.extend(line.split())

    # load words from negative-words.txt
    with open(negatives) as negative:
        for line in negative:
            # don't include the comments
            if line.startswith(";") != True:
                # extend self.negatives list
                # split on all leading/trailing whitespace
                self.negatives.extend(line.split())         

def analyze(self, text):
    """Analyze text for sentiment, returning its score."""

    # split a tweet into a list of words (shorter strings)
    tokenizer = nltk.tokenize.TweetTokenizer()
    tokens = tokenizer.tokenize(text)

    # initial score is neutral
    score = 0

    # iterate over tokens
    for token in tokens:
        # make all tokens lowercase
        token.lower()

        # assign each word in text a value (-1, 0, 1)
        if token in self.positives:
            score += 1
        elif token in self.negatives:
            score -= 1

    # calculate text's total score
    return score

Updated:

Athough I have submitted to cs50.me and score 100/100, I still want to know if I have done anything wrong. I add two examples of my and staff's output below. Any help is appreciated. Thanks!

My output: ~/workspace/pset6/sentiments/ $ python smile Love :| ~/workspace/pset6/sentiments/ $ python smile Hate :|

Staff's output: ~/workspace/pset6/sentiments/ $ ~cs50/pset6/smile Love :) ~/workspace/pset6/sentiments/ $ ~cs50/pset6/smile Hate :(

My output: ~/workspace/pset6/sentiments/ $ python tweets @cs50 0 RT @actoutgames: Boston Travel Day 1! @cs50 @Harvard Professor Interview with @davidjmalan & Walking the Freedom Trail! https://t.co/7ybOIu… 0 RT @HS_QuizShow: Crack the code to this question from @davidjmalan of @CS50 & @HarvardSummer – watch @WGBH this weekend for the answer! htt…

Staff's output: ~/workspace/pset6/sentiments/ $ ~cs50/pset6/tweets @cs50 1 RT @actoutgames: Boston Travel Day 1! @cs50 @Harvard Professor Interview with @davidjmalan & Walking the Freedom Trail! https://t.co/7ybOIu… -1 RT @HS_QuizShow: Crack the code to this question from @davidjmalan of @CS50 & @HarvardSummer – watch @WGBH this weekend for the answer! htt…

3
  • Can you edit the question and add two or three sample tweets that are evaluated differently by staff's version? Mar 5 '17 at 17:16
  • @DinoCoderSaurus I have added sample tweets and sample smile outputs for investigation. Thanks.
    – dreammoon
    Mar 6 '17 at 13:00
  • Since there is no check50 for pset6, suspect it is graded "pass/fail" as in prior years. Mar 6 '17 at 13:59
2

This line token.lower() does not change token to lower case, it returns a copy of token in lower case. You can either set something to that return value (as with word = token.lower()) and then test for word in the arrays, or change the test(s) from if token in self.positives: to if token.lower() in self.positives:

1
  • Thanks, it works!
    – dreammoon
    Mar 6 '17 at 14:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .