0

I've been on this for a while and can't figure out the mistakes. Please correct me if i am mistaken. On running the code, the returned value is always 0. (2) When I use the 'startwith' function i'm getting an error AttributeError: 'str' object has no attribute 'startwith'

import nltk

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

"""Initialize Analyzer."""

def __init__(self, positives, negatives):

    self.PositiveDict = open(positives, "r")
    self.NegativeDict = open(negatives, "r")

    #Make two lists/set/dicts.
    self.Pos = list()
    self.Neg = list()

    self.load(self.PositiveDict)
    self.load(self.NegativeDict)

def load(self, WordFile):
    for Word in WordFile:
        if Word.startswith(';'):
            continue
        else:
            #print(Word)
            if WordFile == self.PositiveDict:
                self.Pos.append(Word.rstrip("\n"))
            elif WordFile == self.NegativeDict:
                self.Neg.append(Word.rstrip("\n"))

"""Analyze text for sentiment, returning its score."""
def analyze(self, text):
    sum=0
    tokenizer = nltk.tokenize.TweetTokenizer()
    tokens = tokenizer.tokenize(text)
    #print(tokens)
    x = len(self.Pos)
    y = len(self.Neg)
    print(y)
    print(x)
    if(True):
        for A in range(x):
            if tokens==self.Pos[A]:
                sum=1
        for B in range(y):
            if tokens==self.Neg[B]:
                sum=-1.0
    else:
        sum=0.0
    print(sum)
    return sum
1

tokenize returns a list, therefore tokens is a list from tokens = tokenizer.tokenize(text). Even if text is only one word. If text is 'cat', tokens is ['cat'].

This if tokens==self.Pos[A]: can never be true (nor the analogous test on sef.Neg[B]) because you are comparing a list (tokens) to one member of a list (ie a single word Pos[A], Neg[B]).

You do not need to iterate through the word lists. You need to iterate through the words in tokens. The previous version was closer:

for word in text:
        tokens = tokenizer.tokenize(text)
        y=self.check(tokens)
        sum+=y

The bug in that code is you want to check(word) not check(tokens). Without reintroducing the check function, you could similarly test if word in self.Pos.


Answer for original code:

There is no str method named startwith. Review the python doc to check the correct spelling, you're missing an s.

Did you really mean text here? tokens = tokenizer.tokenize(text). Because if text is a string of words, then tokens is a list of words, and check will always return 0.

4
  • I've updated my code. Please take another look. It still returns 0. I can iterate and print through my list and it successfully prints all words but still when I compare my token with the list. The returned value is 0.
    – Syed Hasan
    Jun 12 '17 at 11:41
  • answer updated for new code Jun 12 '17 at 13:01
  • Can I iterate over the list of "Tokens" in the same way? eg. tokens[i] == self.Pos[A] (Provided that I do manage a variable 'i' with correct length of tokens) . Should this be good enough for a comparison? Also if i keep the " for word in text " loop, is word here considered a string or a character?.
    – Syed Hasan
    Jun 12 '17 at 15:48
  • Sending over separate words to the check function did solve my issue. Thanks!
    – Syed Hasan
    Jun 12 '17 at 15:51

You must log in to answer this question.

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