0

My code:

import nltk
negs = []
poss = []
class Analyzer():
    """Implements sentiment analysis."""

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

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

    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 poss:
                score += 1
            elif token in negs:
                score -= 1




        return score

It always returns :|

2 Answers 2

0

If you want to use the poss from the self function, you need to refer to it.
if token in self.poss: etc.

2
  • Like this?: if token in self.poss: score += 1 elif token in self.negs: score -= 1
    – Ianardo
    Mar 12, 2017 at 17:45
  • When I do that it does this: Traceback (most recent call last): File "./smile", line 32, in <module> main() File "./smile", line 23, in main score = analyzer.analyze(sys.argv[1]) File "/home/ubuntu/workspace/pset6/sentiments/analyzer.py", line 35, in analyze if token in self.poss: AttributeError: 'Analyzer' object has no attribute 'poss'
    – Ianardo
    Mar 12, 2017 at 18:57
0

Instead of declaring self.negatives and then doing neg = self.negatives, just keep it as self.negatives. So when appending words to the list, just do self.negatives.append(word), same with the positive words. And then when you call upon the lists in the analyze function, just call self.positives (or self.negatives) instead of poss/neg

You must log in to answer this question.

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