0

Hello stack overflow I am really having trouble with sentiments as I feel like I put the write functions down but it is not working. A question that I have is can someone help me understand how the tokens work and if I am even close to implementing tokens correctly?

import nltk

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

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

        # TODO Load positive text file and negative text file
        with open('positive-words.txt', 'r') as f:
            self.positives = [line.strip() for line in f]
        with open('negative-words.txt', 'r') as f:
            self.negatives = [line.strip() for line in f]

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

        # TODO
        tokenizer = nltk.tokenize.TweetTokenizer()
        tokens = tokenizer.tokenize(text)
        if tokens in self.positives:
            score = 1
            return score
        if tokens in self.negatives:
            score = -1
            return score
        return 0
1

As per the spec [emphasis added]:

...among whose features is a tokenizer that you can use to split a tweet (which is maximally a 140-character str object) into a list of words (i.e., shorter str objects).

Program is trying to find a list of words in a list of words here if tokens in self.positives:. You want to test each word in tokens individually; you'll need an iterator.

1
  • Oh snap nice yea i added a for loop for tokens and now I get smiles and sad faces awesome thank you! – Roundabout Jun 22 '17 at 4:32

You must log in to answer this question.

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