-
Notifications
You must be signed in to change notification settings - Fork 1
/
CitizenSatisfactionAnalysis.py
executable file
·181 lines (128 loc) · 5.2 KB
/
CitizenSatisfactionAnalysis.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
import copy
import itertools
class CitizenSatisfactionAnalysis:
#global correlation_cell
#correlation_cell = Correlation_cell()
matrix_prediction_ready = []
def getAsJson(self):
jsonresponde = {}
# jsonresponde["_id"] = self.DatasetId.encode('hex')
# jsonresponde["_id"] = self.DatasetId
#jsonresponde["DatasetId"] = self.DatasetId
jsonresponde["matrix_prediction_ready"] = self.matrix_prediction_ready
return jsonresponde
def clear(self):
del self.matrix_prediction_ready [:]
def calculate_predict(self, matrix_analysis_ready):
print "Depricated!"
def calculate_predict_satisfaction(self, matrix_analysis_ready):
for cell in matrix_analysis_ready:
cell.calculate_satisfaction_at_coocorance_cell_level()
self.matrix_prediction_ready.append(copy.deepcopy(cell))
cell.clear()
#self.matrix_prediction_ready
class Correlation_cell:
#correlation_cell = {}
#row = {}
term_A = ""
term_B = ""
related_tweets_ids = []
positive_tweets_ids = []
negative_tweets_ids = []
neutral_tweets_ids = []
total_positive = 0
total_negative = 0
total_neutral = 0
total_reactions = 0
satisfaction_index = 0.0
def getAsJson(self):
jsonresponde = {}
# jsonresponde["_id"] = self.DatasetId.encode('hex')
# jsonresponde["_id"] = self.DatasetId
#jsonresponde["DatasetId"] = self.DatasetId
jsonresponde["term_A"] = self.term_A
jsonresponde["term_B"] = self.term_B
jsonresponde["related_tweets_ids"] = self.related_tweets_ids
jsonresponde["positive_tweets_ids"] = self.positive_tweets_ids
jsonresponde["negative_tweets_ids"] = self.negative_tweets_ids
jsonresponde["neutral_tweets_ids"] = self.neutral_tweets_ids
jsonresponde["total_positive"] = self.total_positive
jsonresponde["total_negative"] = self.total_negative
jsonresponde["total_neutral"] = self.total_neutral
jsonresponde["total_reactions"] = self.total_reactions
jsonresponde["satisfaction_index"] = self.satisfaction_index
return jsonresponde
def clear(self):
self.term_A = ""
self.term_B = ""
del self.related_tweets_ids [:]
del self.positive_tweets_ids[:]
del self.negative_tweets_ids[:]
del self.neutral_tweets_ids[:]
self.total_positive = 0
self.total_negative = 0
self.total_neutral = 0
self.total_reactions = 0
self.satisfaction_index = 0.0
def build_correlation_matrix(self, policy_topic_terms):
#print policySpace_LDA
correlation_matrix = []
#correlation_cell = {}
#row = {}
#related_tweets_ids = []
#positive_tweets_ids = []
#negative_tweets_ids = []
#neutral_tweets_ids = []
for topic in policy_topic_terms:
terms = []
for bi in topic["topic_terms"]:
terms.append(bi["term"])
#uniqes
uniqe_terms = set(terms)
#nchoosek
Choose2ForTerms= self.nchoosek(uniqe_terms)
for bi in Choose2ForTerms:
self.term_A = bi[0]
self.term_B = bi[1]
#correlation_cell["terms"] = copy.deepcopy(row)
#row.clear()
correlation_matrix.append(copy.deepcopy(self))
self.clear()
#print terms
return correlation_matrix
def nchoosek(self, uniqe_terms):
Choose2ForTerms = list(itertools.combinations(uniqe_terms, 2))
#print Choose2ForTerms
return Choose2ForTerms
"""
def correlation_matrix_to_csv(self, folder, correlation_matrix):
header = ['term_A', 'term_B', 'related_tweets_ids', 'positive_tweets_ids', 'negative_tweets_ids', 'neutral_tweets_ids']
cell_row = []
with open(folder + "_matrix" + ".csv", 'w+') as output:
outputwriter = csv.writer(output, delimiter=',', quotechar='\"')
# putting the header
outputwriter.writerow(header)
row = []
for r in correlation_matrix:
row.append(r["terms"]["term_A"])
row.append(r["terms"]["term_B"])
row.append(r["related_tweets_ids"])
row.append(r["positive_tweets_ids"])
row.append(r["negative_tweets_ids"])
row.append(r["neutral_tweets_ids"])
outputwriter.writerow(copy.deepcopy(row))
del row[:]
"""
def calculate_satisfaction_at_coocorance_cell_level(self):
self.total_positive += len(self.positive_tweets_ids)
self.total_negative += len(self.negative_tweets_ids)
self.total_neutral += len(self.neutral_tweets_ids)
self.total_reactions += len(self.related_tweets_ids)
if self.total_reactions >0:
self.satisfaction_index = self.total_positive/self.total_reactions
def get_related_tweets_query(self):
#building mongo query for related tweets
#text = "\"" + self.term_A +"\" " +"\""+self.term_B + "\""
#q= {"search": text}
q2= "\"a\" \"b\""
return q2 #text