-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathanalyzer.py
executable file
·45 lines (36 loc) · 1.24 KB
/
analyzer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import nltk
import os
import sys
class Analyzer():
"""Implements sentiment analysis."""
def __init__(self):
"""Initialize Analyzer."""
# instantiate tokenizer
self.tokenizer = nltk.tokenize.TweetTokenizer()
# load positive words
self.positives = set()
with open(os.path.join(sys.path[0], "positive-words.txt")) as lines:
for line in lines:
line = line.strip()
if line and not line.startswith(";"):
self.positives.add(line)
# load negative words
self.negatives = set()
with open(os.path.join(sys.path[0], "negative-words.txt")) as lines:
for line in lines:
line = line.strip()
if line and not line.startswith(";"):
self.negatives.add(line)
def analyze(self, text):
"""Analyze text for sentiment, returning a score."""
# tokenize text
tokens = self.tokenizer.tokenize(text)
# score text
score = 0
for token in tokens:
token = token.lower()
if token in self.positives:
score += 1
if token in self.negatives:
score -= 1
return score