-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze.py
More file actions
105 lines (81 loc) · 2.85 KB
/
analyze.py
File metadata and controls
105 lines (81 loc) · 2.85 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import sys
from twython import Twython
from nltk.tokenize import TweetTokenizer
#from nltk.tokenize import tokenize
class Analyzer():
"""Implements tokenizer and sentiment analysis."""
# Tracking Datum
_pos_words_n = 0
_neg_words_n = 0
_pos_words = []
_neg_words = []
def __init__(self, positive, negative, stats):
# This will store 2 lists / all_p and all_n words
self.dict_words = {"pos" : [], "neg" : []}
self.tokenizer = TweetTokenizer()
#Building all_words dictionary
with open(positive, "r") as fp:
try:
for item in fp:
if not item.startswith(";"):
self.dict_words["pos"].append(item.lower().strip())
except RuntimeError:
self.f_Err = 1
stats.game_active = False
"""
If game_active && f_Err -> start other game, perhaps
"""
with open(negative, "r") as fp:
try:
for item in fp:
if not item.startswith(";"):
self.dict_words["neg"].append(item.lower().strip())
except RuntimeError:
self.f_Err = 1
stats.game_active = False
# ["I", "am", "a", "tweet"]
#self.words = []
self.pResult = []
self.nResult = []
def analyze(self, tweet):
""" Sentiment Analyzer """
# Proper tokenizing
self.words = self.tokenizer.tokenize(tweet)
#print("WORDS == {}".format(words))
# Positive analysis
for word in self.words:
self.pResult = [item for item in self.dict_words["pos"] if item.strip() == word.lower().strip()]
# Cache Data
if self.pResult:
self.track_words(p=self.pResult)
# Negative analysis
for word in self.words:
self.nResult = [item for item in self.dict_words["neg"] if item.strip() == word.lower().strip()]
# Cache Data
if self.nResult:
self.track_words(n=self.nResult)
# Clean up the dict
if Analyzer._pos_words_n > 0 or Analyzer._neg_words_n > 0:
dict_words = {"pos" : [], "neg" : []}
return self.words
@classmethod
def track_words(cls, p=[], n=[]):
""" Center for Opinion Research """
cls._pos_words_n += len(p)
cls._neg_words_n += len(n)
if p:
for word in p:
cls._pos_words.append(word)
elif n:
for word in n:
cls._neg_words.append(word)
@classmethod
def get_difficulty(c):
""" Otherwise, asshat meter """
x = c._pos_words_n
y = c._neg_words_n
z = x + y
try:
return round(100 * (y / z))
except ZeroDivisionError:
return 1