Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import nltk
- import sys
- import string
- from nltk.tokenize import TweetTokenizer
- class Analyzer():
- """Implements sentiment analysis."""
- def __init__(self, positives, negatives):
- """Initialize Analyzer."""
- self.positives = []
- self.negatives = []
- with open(negatives) as negative:
- for line in negative:
- if line.startswith(";"):
- continue
- else:
- self.negatives.extend(line.strip())
- with open(positives) as positive:
- for line in positive:
- if line.startswith(";"):
- continue
- else:
- self.positives.extend(line.strip())
- 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
- else:
- score = 0
- return score
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement