This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTSA.py
186 lines (156 loc) · 6.34 KB
/
TSA.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python
'''
Created on Mar 29, 2017
@author: Joseph
'''
import sys #for system stuff
#import time #for wait() fn
import io
from access import TwyAccess
import gui
import counties
import collections
from CleanAnalysis import (POS_tagging,StopWordsFilter,tagger,expandContractions)
from nltk.stem import WordNetLemmatizer #Multiple options
from nltk.corpus import wordnet
from ratings import csvd
#import csv
from logging import raiseExceptions
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot, QObject, Qt
#from nltk.app.wordnet_app import lemma_property
class worker (QObject):
update_Label = pyqtSignal(str,'PyQt_PyObject','PyQt_PyObject')
def __init__(self, twya, lem, conv, Q = 'a OR e OR i OR o OR u',
radius= 5.02, unit='mi'):
QThread.__init__(self)
self.twya = twya
self.Q = Q
self.lem = lem
self.conv = conv
self.radius = radius
self.unit = unit
self.Vavg = 0
self.Aavg = 0
self.Davg = 0
self.i = 0
def _get_mood_per_county(self,GEO):
Vavg, Aavg, Davg, i = 0,0,0,0
results = self.twya.query(self.Q, GEO)
for result in results['statuses']:
tagged =(POS_tagging(StopWordsFilter(tagger(
expandContractions(result['text'].lower())))))
for tag in tagged:
tag[0] = (self.lem.lemmatize(tag[0][0], pos=self.conv(tag[0][1])))
if tag[0] in csvd:
V,A,D = csvd[tag[0]]
Vavg+=V
Aavg+=A
Davg+=D
i+=1
#print(i)
self.Vavg+=Vavg
self.Aavg+=Aavg
self.Davg+=Davg
self.i+=i
return Vavg/i,Aavg/i,Davg/i
def run(self):
while True:
for key in counties.od:
lat = counties.od[key][3]
long = counties.od[key][4]
GEO = str(lat)+','+str(long)+','+str(self.radius)+self.unit
moodVal = self._get_mood_per_county(GEO)
#print(moodVal[0])
self.update_Label.emit(counties.od[key][0],moodVal,
(self.Vavg/self.i,self.Aavg/self.i,self.Davg/self.i))
QThread.sleep(1)
class TSAprogram(QtWidgets.QMainWindow, gui.Ui_MainWindow):
twya = TwyAccess(2)
def __init__(self, twya, lem, conv, Q, radius, unit):
super(self.__class__, self).__init__()
self.Q = Q
self.lem = lem
self.conv = conv
self.radius = radius
self.unit = unit
self.setupUi(self)
self.thread = QThread()
self.w = worker(twya, lem, conv, Q, radius, unit)
self.w.update_Label.connect(self.setLabel)
self.w.moveToThread(self.thread)
self.thread.started.connect(self.w.run)
self.thread.start()
def keyPressEvent(self, event): #function to allow prog exit by ESC
key = event.key()
if key == Qt.Key_Escape:
#save file
sys.exit()
super(TSAprogram,self).keyPressEvent(event)
def moody(self,VAD,VAD_AVG):
#print('moody')
#return ":/Images/1f60c.relaxedsvg.png"
v,a,d=VAD
vc,ac,dc=VAD_AVG
if(v>=vc): #positive emotion (high valence)
if(a>=ac): #excited (high arousal)
if(d>=dc):
return ":/Images/1f601exuberant.png"
elif(d<dc): #"frequently seeks the sympathy, protection,love,advice,and reasuurance of other people
return ":/Images/1f605dependent.png"
elif(a<ac):
if(d>=dc):
return ":/Images/1f60c.relaxedsvg.png"
elif(d<dc):
return ":/Images/1f610docile.png"
elif(v<vc):
if(a>=ac):
if(d>=dc):
return ":/Images/1f621hostile.png"
elif(d<dc):
return ":/Images/1f616anxious.png"
elif(a<ac):
if(d>=dc):
return ":/Images/1f61edisdainful.png"
elif(d<dc):
return ":/Images/1f634bored.png"
return 'ERROR'
#@pyqtSlot(str,'PyQt_PyObject','PyQt_PyObject')
def setLabel(self, name, moodVal,avgVal):
for label in self.centralwidget.children():
#print(name)
#print(label.objectName())
if label.objectName()==name:
#print('made it inside')
new = self.moody(moodVal,avgVal)
#print(new)
#label.setPixmap(self.moody(moodVal,avgVal))
label.setPixmap(QtGui.QPixmap(new))
#print('success')
return
def conv(treebank_tag):
if treebank_tag.startswith('J'):
return wordnet.ADJ
elif treebank_tag.startswith('V'):
return wordnet.VERB
elif treebank_tag.startswith('N'):
return wordnet.NOUN
elif treebank_tag.startswith('R'):
return wordnet.ADV
elif treebank_tag.startswith('S'):
return wordnet.ADJ_SAT
else:
return wordnet.NOUN
def main():
twya = TwyAccess(2) #TwyAccess class init
lem = WordNetLemmatizer() #Lemmatizer class init
radius = 5.02 #avg radius of AR county
unit = 'mi' #unit of miles
Q = 'a OR e OR i OR o OR u'
#GEO = str(35.4406)+','+str(-93.0176)+','+str(radius)+unit
app = QtWidgets.QApplication(sys.argv)
form = TSAprogram(twya, lem, conv, Q, radius, unit)
form.showFullScreen()
app.exec_()
if __name__ == '__main__':
main()