It always shows yellow :|

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

    def __init__(self, positives, negatives):
        """Initialize Analyzer."""

        self.negatives = []
        with open("negative-words.txt") as lines:
            for line in lines:
                if line.startswith(";"):
                    word = line.strip(" ").lower
        self.positives = []
        with open("positive-words.txt") as lines1:
            for line1 in lines1:
                if line1.startswith(";"):
                    word1 = line1.strip(" ").lower

    def analyze(self, text):
        """Analyze text for sentiment, returning its score."""
        score = 0
        tokenizer = nltk.tokenize.TweetTokenizer()
        tokens = tokenizer.tokenize(text)
        for token in tokens:
            if token in self.positives:
                score += 1
            elif token in self.negatives:
                score -= 1

        return score

poss and negs are not instance variables, so they are not available to analyze.

From the Hints section of the spec:

If you would like a variable to be accessible from both __init__ and analyze, be sure to define it as an "instance variable" inside of [__init__]. For instance, if you were to define

self.n = 42

inside of __init__, then self.n would also be accessible inside of analyze.

self.negatives and self.positives are already defined as instance variables inside of __init__. poss and negs are unnecessary. And since each line in the text file is terminated with a newline, you'll want to strip that too.

If you look at the word texts, you'll notice all the words are lower case. Another hint from the spec:

Odds are you’ll find str.lower of interest.

  • I edited my code, but it still only shows :|
    – Ianardo
    Mar 13 '17 at 1:11
  • answer edited. strip the newline too. Mar 13 '17 at 3:04
  • Change it to this? word1 = line1.strip(" \n").lower
    – Ianardo
    Mar 13 '17 at 18:17
  • Close, you need to terminate the .lower with () like so: word1 = line1.strip(" \n").lower(), to actually run the method, otherwise it just returns the method. Mar 13 '17 at 20:55
  • It works! Thanks a lot!
    – Ianardo
    Mar 13 '17 at 21:18

You must log in to answer this question.

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