-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer.py
More file actions
39 lines (29 loc) · 1.09 KB
/
analyzer.py
File metadata and controls
39 lines (29 loc) · 1.09 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
import nltk
class Analyzer():
"""Implements sentiment analysis."""
def __init__(self, positives, negatives):
"""Initialize Analyzer."""
self.positives = []
self.negatives = []
pos = open('positive-words.txt', 'r')
neg = open('negative-words.txt', 'r')
for line in pos:
if line and line[0].isalpha():
self.positives.append(line.strip("\n"))
for line in neg:
if line and line[0].isalpha():
self.negatives.append(line.strip("\n"))
def analyze(self, text):
"""Analyze text for sentiment, returning its score."""
score = 0
texttok = nltk.wordpunct_tokenize(text)
for i in range(len(texttok)):
for j in range(len(self.positives)):
pick = self.positives[j]
if pick == texttok[i]:
score += 1
for j in range(len(self.negatives)):
pick = self.negatives[j]
if pick == texttok[i]:
score -= 1
return score