Hello stack overflow I am really having trouble with sentiments as I feel like I put the write functions down but it is not working. A question that I have is can someone help me understand how the tokens work and if I am even close to implementing tokens correctly?
import nltk
class Analyzer():
"""Implements sentiment analysis."""
def __init__(self, positives, negatives):
"""Initialize Analyzer."""
# TODO Load positive text file and negative text file
with open('positive-words.txt', 'r') as f:
self.positives = [line.strip() for line in f]
with open('negative-words.txt', 'r') as f:
self.negatives = [line.strip() for line in f]
def analyze(self, text):
"""Analyze text for sentiment, returning its score."""
# TODO
tokenizer = nltk.tokenize.TweetTokenizer()
tokens = tokenizer.tokenize(text)
if tokens in self.positives:
score = 1
return score
if tokens in self.negatives:
score = -1
return score
return 0