-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer.py
More file actions
45 lines (35 loc) · 1.19 KB
/
Copy pathanalyzer.py
File metadata and controls
45 lines (35 loc) · 1.19 KB
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
class Analyzer():
"""Implements sentiment analysis."""
def __init__(self, positives, negatives):
"""Initialize Analyzer."""
self.positives = []
with open("positive-words.txt", "r") as word1:
for line in word1:
if line.startswith(";") or line.startswith(" "):
pass
else:
self.positives.extend(line.split())
self.negatives = []
with open("negative-words.txt", "r") as word2:
for line in word2:
if line.startswith(";") or line.startswith(" "):
pass
else:
self.negatives.extend(line.split())
def analyze(self, text):
"""Analyze text for sentiment, returning its score."""
c = 0
tokenizer = nltk.tokenize.TweetTokenizer()
tokens = tokenizer.tokenize(text)
for token in tokens:
if token in self.positives:
c += 1
elif token in self.negatives:
c += (-1)
else:
c += 0
return c
def show(self):
print(self.positives)
print(self.negatives)