1
import nltk

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

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

        self.negatives = []
        self.positives = []

        with open("negative-words.txt") as negative:
            for line in negative:
                if line.startswith((";", " ")):
                    continue
                else:
                    self.negatives.extend(line.split())

        with open("positive-words.txt") as positive:
            for line in positive:
                if line.startswith((";", " ")):
                    continue
                else:
                    self.positives.extend(line.split())



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

        tokenizer = nltk.tokenize.TweetTokenizer()
        tokens = tokenizer.tokenize(text)
        score = 0

        for token in tokens:
            token.lower()

            if token in self.positives:
                score += 1
            elif token in self.negatives:
                score -= 1

        return score

I am new to python. This is my code for analyzer.py - pset6 and it works fine but I was wondering if there is any scope to write it more concisely. Any tips would be appreciated.

1 Answer 1

2
  • positives and negatives are file names sent to the function, so instead of hardcoding the file name like this with open("negative-words.txt") as negative:, use the variable name like this with open(negatives) as negative:
  • You could condense the conditionals:

    for line in negative:
        if not line.startswith((";", " ")):
                self.negatives.extend(line.split())
    

Your code is clear, easy to read, easy to understand. Never underestimate the importance of that.

You must log in to answer this question.

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