0
import nltk

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

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

    # TODO
    self.pos_words = set()      #starting set for positive words
    self.neg_words = set()      #starting set for negative words
    positive = open(positives, "r") #oppening file with positive words
    for line in positive:           #starting itarate through lines
        while line.startswith(';') and line.startswith (' '): #if line is a comment - starts with ';' go to next line and don't memorize
            line = next (positive)
        self.pos_words.add(line.rstrip("\n"))   #add word to dictionary (set)
    positive.close()                            # close file 

    negative = open(negatives, "r")     #the same steps as for positive words, just for negative once
    for line in negative:
        while line.startswith(';') and line.startswith (' '):
            line = next (negative)
        self.neg_words.add(line.rstrip("\n"))
    negative.close() 
def analyze(self, text):
    """Analyze text for sentiment, returning its score."""

    # TODO
    # tokenize every tweet
    tokenizer = nltk.tokenize.casual.TweetTokenizer()
    tokens = tokenizer.tokenize(text)
    k = 0
    for token in tokens:

        if token.lower() in self.pos_words:      #checking if word is positive, negative or neutral and giving points according to emocjonal aspekt of word
            k += 1
        elif text.lower() in self.neg_words:
            k -= 1


    return k

That doesn't work. It is adding 1 when the word is in positives but k-= 1 thoesn't work. I mean it works in smile - when there is one negative word it is market with :( but in tweet that doesn't work. I don't know why.

1

elif text.lower() in self.neg_words:

shouldn't that be token.lower()?

1
  • Oh my God!! Thank you! It is so obvious!! I'm blind I think... Thanks once again. I doubt I would ever find this mistake :) – Maciej Ś Jul 14 '17 at 6:53

You must log in to answer this question.

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