-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_parse_file_sentiment_to_make_it_compact.py
209 lines (154 loc) · 8.28 KB
/
1_parse_file_sentiment_to_make_it_compact.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
'''
this file parses the file of sentiment for specific log
it find the matching countries and summarizes the sentiment for each one day
in case of missing values the previous day sentiment is taken
THIS SCRIPT IS USED IF THERE IS NO EXPLICIT LOCATION ASSOCIATED FOR EACH EVENT IN THE LOG
Author: Anton Yeshchenko
'''
import csv
import math
from pathlib import Path
from operator import itemgetter
eventlog = "news-analysis.summary-italian-logs.csv"
eventlog = "news-analysis.summary-2000.csv"
eventlogs = ["news-analysis.summary-2010.csv",
"news-analysis.summary-2011.csv",
"news-analysis.summary-2012.csv",
"news-analysis.summary-2000.csv", "news-analysis.summary-2013.csv",
"news-analysis.summary-2001.csv", "news-analysis.summary-2014.csv",
"news-analysis.summary-2002.csv", "news-analysis.summary-2015.csv",
"news-analysis.summary-2003.csv", "news-analysis.summary-2016.csv",
"news-analysis.summary-2004.csv", "news-analysis.summary-2017.csv",
"news-analysis.summary-2005.csv", "news-analysis.summary-bpic2012-logs.csv",
"news-analysis.summary-2006.csv", "news-analysis.summary-bpic2013-logs.csv",
"news-analysis.summary-2007.csv",
"news-analysis.summary-2008.csv",
"news-analysis.summary-2009.csv",
#"news-analysis.summary-italian-logs.csv"
]
eventlogs = ["news-analysis.summary-2010.csv",
"news-analysis.summary-2011.csv",
"news-analysis.summary-2012.csv",
"news-analysis.summary-2013.csv",
"news-analysis.summary-2000.csv",
"news-analysis.summary-2004.csv",
"news-analysis.summary-2005.csv",
"news-analysis.summary-2006.csv",
"news-analysis.summary-2007.csv",
"news-analysis.summary-2008.csv",
"news-analysis.summary-2009.csv",
"news-analysis.summary-italian-logs.csv"
]
#news-analysis.summary-bpic2013-logs-2.csv
def parse_file(path_to_log, sentiment_file_arg, id_country,
pub_date,
avg_body_sentiment,
avg_body_wink_normalized, countries_to_pick_from):
csvfile = open(path_to_log / sentiment_file_arg, 'r')
logreader = csv.reader(csvfile, delimiter=',', quotechar='"',
quoting=csv.QUOTE_ALL, skipinitialspace=True)
header = next(logreader, None) # skip the headers
writer = csv.writer(open(path_to_log / "processed" / sentiment_file_arg, 'w'))
n_avg_body_sentiment = 2
n_avg_body_wink_normalized = 3
what_i_need_to_get_indeces = [id_country, pub_date, avg_body_sentiment, avg_body_wink_normalized]
i = 0 #index_to_begin_normalization
name_country_to_check = ""
date_to_check = ""
temp_list = list()
first_mark = False
# write to the file headers
writer.writerow([header[ind] for ind in what_i_need_to_get_indeces[1:]])
for row in logreader:
#here we check for the country to be in our list
if not row[id_country] in countries_to_pick_from:
continue
if (date_to_check != row[pub_date]):
print (date_to_check)
# this line if we want to check and group also by country
#if first_mark and (name_country_to_check != row[id_country] or date_to_check != row[pub_date]):
if first_mark and date_to_check != row[pub_date]:
temp_avg_body_sentiment = 0
temp_avg_body_wink_normalized = 0
for i in temp_list:
#print (i[n_avg_body_sentiment])
if not i[n_avg_body_sentiment] == 'NaN' and not i[n_avg_body_sentiment] == '':
print (i)
temp_avg_body_sentiment += float(i[n_avg_body_sentiment])
if not i[n_avg_body_wink_normalized] == 'NaN':
temp_avg_body_wink_normalized += float(i[n_avg_body_wink_normalized])
if math.fabs(temp_avg_body_wink_normalized)< 0.0001:
print ('THERE IS 0 FOR SOME DAY')
temp_avg_body_sentiment /= len(temp_list)
temp_avg_body_wink_normalized /= len(temp_list)
#write to file here
# this line if also by country
#writer.writerow([name_country_to_check, date_to_check, temp_avg_body_sentiment, temp_avg_body_wink_normalized])
writer.writerow(
[date_to_check, temp_avg_body_sentiment, temp_avg_body_wink_normalized])
name_country_to_check = row[id_country]
date_to_check = row[pub_date]
temp_list.clear()
if not first_mark:
name_country_to_check = row[id_country]
date_to_check = row[pub_date]
temp_list.append([row[ind] for ind in what_i_need_to_get_indeces])
first_mark = True
# for eventl in eventlogs:
# print("FILE NAME: " + eventl)
# parse_file(eventl)
#
# ####### BPI 2017
# path_to_log = Path("/home/yesant/Documents/Data/ProcessMining/"
# "RioPaper_ExternalContextBPM/Fernando_9July/")
# sentiment_file = "news-analysis.summary-bpic2012-2017-logs-2.csv"
# id_country = 1
# pub_date = 2
# avg_body_sentiment = 9
# avg_body_wink_normalized = 10
# countries_to_pick_from = { 'France, Metropolitan', 'Hungary', 'Yugoslavia', 'Serbia and Montenegro', 'Albania', 'Denmark',
# 'Greenland', 'Vatican City State', 'Netherlands Antilles', 'Kosovo', 'Italy', 'Luxembourg', 'Bulgaria', 'Malta', 'Canary Islands',
# 'Sweden', 'Ireland', 'Czech Republic', 'Slovakia', 'Colombia', 'European Union', 'Germany', 'Cyprus',
# 'Poland', 'Austria','Netherlands', 'France', 'Finland', 'Belgium', 'Switzerland', 'Lithuania',
# 'United Kingdom','Moldova', 'Greece', 'Norway', 'New Zealand', 'Romania','Iceland',
# 'German Democratic Republic', 'Latvia', 'Turkey', 'Czechoslovakia', 'Portugal',
# 'Liechtenstein' 'Serbia', 'Estonia', 'Slovenia', 'Croatia'}
#
#
#
# ####### BPI 2012
# path_to_log = Path("/home/yesant/Documents/Data/ProcessMining/"
# "RioPaper_ExternalContextBPM/Fernando_17July/")
# sentiment_file = "news-analysis.summary-bpic2012-logs-first-half-of-timerange.csv"
# id_country = 1
# pub_date = 2
# avg_body_sentiment = 9
# avg_body_wink_normalized = 10
# countries_to_pick_from = { 'France, Metropolitan', 'Hungary', 'Yugoslavia', 'Serbia and Montenegro', 'Albania', 'Denmark',
# 'Greenland', 'Vatican City State', 'Netherlands Antilles', 'Kosovo', 'Italy', 'Luxembourg', 'Bulgaria', 'Malta', 'Canary Islands',
# 'Sweden', 'Ireland', 'Czech Republic', 'Slovakia', 'Colombia', 'European Union', 'Germany', 'Cyprus',
# 'Poland', 'Austria','Netherlands', 'France', 'Finland', 'Belgium', 'Switzerland', 'Lithuania',
# 'United Kingdom','Moldova', 'Greece', 'Norway', 'New Zealand', 'Romania','Iceland',
# 'German Democratic Republic', 'Latvia', 'Turkey', 'Czechoslovakia', 'Portugal',
# 'Liechtenstein' 'Serbia', 'Estonia', 'Slovenia', 'Croatia'}
########
## Road traffic
path_to_log = Path("/home/yesant/Documents/Data/ProcessMining/"
"RioPaper_ExternalContextBPM/Fernando_22July/")
sentiment_file = "italian-logs-news-summary.csv"
id_country = 1
pub_date = 2
avg_body_sentiment = 9 #avg_body_sentiment_score
avg_body_wink_normalized = 10 #avg_body_wink_normalized_score
countries_to_pick_from = { 'France, Metropolitan', 'Hungary', 'Yugoslavia', 'Serbia and Montenegro', 'Albania', 'Denmark',
'Greenland', 'Vatican City State', 'Netherlands Antilles', 'Kosovo', 'Italy', 'Luxembourg', 'Bulgaria', 'Malta', 'Canary Islands',
'Sweden', 'Ireland', 'Czech Republic', 'Slovakia', 'Colombia', 'European Union', 'Germany', 'Cyprus',
'Poland', 'Austria','Netherlands', 'France', 'Finland', 'Belgium', 'Switzerland', 'Lithuania',
'United Kingdom','Moldova', 'Greece', 'Norway', 'New Zealand', 'Romania','Iceland',
'German Democratic Republic', 'Latvia', 'Turkey', 'Czechoslovakia', 'Portugal',
'Liechtenstein' 'Serbia', 'Estonia', 'Slovenia', 'Croatia'}
parse_file(path_to_log,sentiment_file, id_country ,
pub_date ,
avg_body_sentiment,
avg_body_wink_normalized,
countries_to_pick_from)