0

I need some help with tweets. Or just help me get what inspiration to get from smile for it, as per the specs.

Here's my code:

#!/usr/bin/env python3

import os
import sys
import nltk

from analyzer import Analyzer
from termcolor import colored
from helpers import get_user_timeline

def main():
    if len(sys.argv) != 2:
        sys.exit("Usage: ./tweets twitterhandle")

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

    analyzer = Analyzer(positives, negatives)

    final_score = analyzer.analyze(sys.argv[1])

    for i in range(0, final_score):
        score = final_score[i]
        print(score[0], end="")
        if score[i] > 0.0:
            print(colored(score, "green"))
        elif score[i] < 0.0:
            print(colored(score, "red"))
        else:
            print(colored(score, "yellow"))

if __name__ == "__main__":
    main()


def tweet_analyze(self, text):
    tweets = get_user_timeline(text, 50)

    if tweets == None:
        sys.exit("Error: problem getting tweets")

    print(tweets)
    final_score = []
    for tweet in tweets:
        score = 0
        tokenizer = nltk.tokenize.TweetTokenizer()
        tokens = tokenizer.tokenize(tweet)
        if tweet in self.positives:
            score += 1
        elif tweet in self.negatives:
            score -= 1
        final_score[tweet] = score

    return final_score

And since I'm not calling tweet_analyze anywhere, is it better to delete that and call get_user_timeline() in main()? What do I pass in to get_user_timeline(), though? And if I have to call it somewhere, then where?

1 Answer 1

0

pseudocode for smile might look like this:

 get user input, ensure proper usage
 instantiate an Analyzer object
analyze the input (word)
  output the result

Zamyla's walkthrough in pseudocode might look like this:

 get user input (a twitter handle), ensure proper usage → get a hint from smile

 get tweets for that twitter handle → use get_user_timeline (WARNING: strip '@' from handle)

 instantiate an Analyzer object → get a hint from smile

 iterate over the list of tweets →use TweetTokenizer to split a tweet into a list of words

  iterate over the list of words
     "score" each word (analyze)
    accumulate the "score" for each tweet

  output the result for each tweet

You should not need a function to analyze tweets. It's really the same structure as smile just more "getting" and "iterating".

  • smile takes one input (text) and analyzes it.
    tweets takes one input (twitter handle) then gets the associated tweets (get_user_timeline(twitter handle)). Two steps instead of one.
  • smile calls analyze with one word to get its score.
    tweets needs to do two iterations to get individual words to score. The first to tokenize the tweet (TweetTokenizer), the second to "score" (analyze) individual words (tokens).
  • smile outputs the result of analyze.
    tweets accumulates scores and then produces output.

It might be a good investment to watch the Walkthrough again (it's under 2 minutes!), and develop your own pseudocode before you start hammering away at the keyboard. Thoroughly understanding the problem before you tackle it will usually lead to better code and will surely prevent wasted efforts and frustration.

8
  • I need to declare a variable initialized with the return value of get_user_timeline(), right? But where do I get the text to pass into it? I'll take out that function to analyze tweets and call get_user_timeline() in main(), but I need to understand these few things first. Commented Mar 7, 2017 at 18:32
  • Think about it. The program takes a "twitter handle" (also called "screen name") as an argument. From helpers in the spec Given a screen name, meanwhile, get_user_timeline returns a list of tweets (each as a str). . Answer has been updated with clarifications. Commented Mar 7, 2017 at 19:05
  • I've made some changes. Please look at it here: pastebin.com/3TTKDtwF, and tell me what I need to change if anything. Commented Mar 7, 2017 at 19:51
  • Okay, here's the latest updated version: pastebin.com/3TTKDtwF Commented Mar 7, 2017 at 21:05
  • Okay, I did some more changes (kept final_score as the variable being checked, rather than score, and took out the square brackets from it). The interpreter's giving me an error about "helpers" being undefined, though. Line 19, "tweets = helpers.get_user_timeline(text, 50)". Here's the updated code: pastebin.com/3TTKDtwF Commented Mar 7, 2017 at 23:56

You must log in to answer this question.

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