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()


def load(self, WordFile):
    for Word in WordFile:
        if Word.startswith(';'):
            if WordFile == self.PositiveDict:
            elif WordFile == self.NegativeDict:

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

1 Answer 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)

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.

  • 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, 2017 at 11:41
  • answer updated for new code Jun 12, 2017 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, 2017 at 15:48
  • Sending over separate words to the check function did solve my issue. Thanks!
    – Syed Hasan
    Jun 12, 2017 at 15:51

You must log in to answer this question.

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