0

Everything runs properly (no syntax errors) but when running ./smile it always displays a yellow nuetral face.

And I am a bit confused about the strip() function. What is the point of removing spaces from the positive or negative text files when there aren't none after all of the ;;;;

import nltk

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

    def __init__(self, positives, negatives):
        #allocated lists to load info into
        self.positivelist = []
        self.negativelist = []

        #load positive and negative words into lists
        with open("positive-words.txt") as positives:
            for line in positives:
                if line.startswith(";") != True:
                    self.positivelist.append(line.strip(" "))

        with open("negative-words.txt") as negatives:
            for line in negatives:
                if line.startswith(";") != True:
                    self.negativelist.append(line.strip(" "))



    def analyze(self, text):
        #split input command line text into tokens

        self.token = nltk.word_tokenize(text)

        #set score regarding the words in the token and return its value
        score = 0

        for words in self.token:
            if words.lower() in self.positivelist:
                score +=1
            elif words.lower() in self.negativelist:
                score -=1
            else:
                continue


        return score
1

if words.lower is not actually doing anything.

Be very careful when you call a method that you don't forget the ():

if words.lower()

will work.

1
  • Updated code, still does not work?
    – R_C
    Nov 11 '17 at 18:22
0

Made these changes inside the the analyze function. This worked:

def analyze(self, text):
        #split input command line text into tokens

        self.tokenizer = nltk.tokenize.TweetTokenizer()
        self.token = self.tokenizer.tokenize(text)

You must log in to answer this question.

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