-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_network_v.rb
314 lines (273 loc) · 16 KB
/
build_network_v.rb
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
require_relative "date_tools.rb"
PATH = "D:\\DGU\\CassandraMy\\SMCorpora\\"
forum = "flashback"
countinteractions = false
countusers = false
countactual = true
addendum = "_nodator"
if addendum == "_resor"
subforums = ["resor"]
elsif addendum == "_nodator"
subforums = ["droger", "ekonomi", "flashback", "fordon", "hem", "kultur", "livsstil", "mat", "ovrigt", "politik", "resor", "samhalle", "sex", "sport", "vetenskap"]
else
subforums = ["dator", "droger", "ekonomi", "flashback", "fordon", "hem", "kultur", "livsstil", "mat", "ovrigt", "politik", "resor", "samhalle", "sex", "sport", "vetenskap"]
end
threshold_time_distance = 5
threshold_post_distance = ARGV[0].to_i
authorhash = Hash.new{|hash,key| hash[key] = Hash.new}
#STDOUT.puts "type\tnext_post\tprev_post\texposed_user\tuser_status\texposed_to_c\texposed_to_i"
authorfile = File.open("results\\flashback_hbt(q)2#{addendum}_t0.tsv","r:utf-8")
#authorfile = File.open("results\\flashback_hbt(q).tsv","r:utf-8")
authors = {}
yearhash = Hash.new{|hash,key| hash[key] = Hash.new(0.0)}
innovativity_year_author = Hash.new{|hash,key| hash[key] = Hash.new}
exposure_year_author_c = Hash.new{|hash,key| hash[key] = Hash.new(0.0)}
exposure_year_author_i = Hash.new{|hash,key| hash[key] = Hash.new(0.0)}
authorfile.each_line.with_index do |line,index|
if index > 0
line2 = line.strip.split("\t")
year = line2[0].to_i
username = line2[1]
authors[username] = true
v1rel = line2[-2].to_f
v2rel = line2[-1].to_f
innovativity_year_author[year][username] = v2rel
if v2rel > v1rel
authorhash[year][username] = "innovative"
yearhash[year]["innovative"] += 1
else
authorhash[year][username] = "conservative"
yearhash[year]["conservative"] += 1
end
end
end
postfile = File.open("results\\flashback_posts_hbt(q)2_t0#{addendum}.tsv","r:utf-8")
#postfile = File.open("results\\flashback_posts_hbt(q)_t0.tsv","r:utf-8")
c_posts = Hash.new(0.0)
i_posts = Hash.new(0.0)
postfile.each_line.with_index do |line,index|
if index > 0
line2 = line.strip.split("\t")
post_id = line2[0]
c = line2[1].to_f
i = line2[2].to_f
c_posts[post_id] = c
i_posts[post_id] = i
end
end
=begin
#STDOUT.puts "year\tc\ti"
authorhash.each_pair do |year,users|
c = 0
i = 0
users.each_pair do |user,status|
if status == "conservative"
c += 1
elsif status == "innovative"
i += 1
end
end
#STDOUT.puts "#{year}\t#{c}\t#{i}"
end
__END__
=end
c_to_c = Hash.new(0.0)
c_to_i = Hash.new(0.0)
i_to_i = Hash.new(0.0)
i_to_n = Hash.new(0.0)
c_to_n = Hash.new(0.0)
n_to_n = Hash.new(0.0)
n_to_i = Hash.new(0.0)
n_to_c = Hash.new(0.0)
i_to_c = Hash.new(0.0)
cusers_to_iusers = Hash.new{|hash,key| hash[key] = Hash.new}
cusers_to_cusers = Hash.new{|hash,key| hash[key] = Hash.new}
cusers_to_nusers = Hash.new{|hash,key| hash[key] = Hash.new}
subforums.each do |subforum|
f = File.open("#{PATH}#{forum}-#{subforum}_post.conllu","r:utf-8")
current_user = ""
prev_thread = ""
current_thread = ""
users_in_thread = {}
#post_in_thread = 0
current_post = ""
current_year = ""
current_date = ""
prev_year = ""
prev_posts = []
STDERR.puts subforum
f.each_line.with_index do |line,index|
line1 = line.strip
if line1 != ""
if line1.include?("# username")
#STDERR.puts line
#STDERR.puts index
#STDERR.puts "#{current_post}\t#{prev_posts}"
#hold = gets
if !line1.split("=")[1].nil?
#post_in_thread += 1
current_user = line1.split("=")[1].strip
###if authorhash[current_year][current_user] != nil #authors[current_user]
prev_posts.each do |prev_post|
###if authorhash[current_year][prev_post[0]] != nil
if (abs_day(date_to_array(current_date, "-")) - abs_day(date_to_array(prev_post[1], "-")) <= threshold_time_distance) and (current_user != prev_post[0])
#userlabel = [current_user,prev_post[0]].sort
#proximity_hash[userlabel] += 1
#interactions[subforum][userlabel] << abs_day(date_to_array(current_date, "-"))
if countinteractions
if authorhash[current_year][current_user] == authorhash[current_year][prev_post[0]]
if authorhash[current_year][current_user] == "conservative"
c_to_c[current_year] += 1
elsif authorhash[current_year][current_user] == "innovative"
i_to_i[current_year] += 1
else
n_to_n[current_year] += 1
end
else
if authorhash[current_year][prev_post[0]] == nil
if authorhash[current_year][current_user] == "conservative"
c_to_n[current_year] += 1
else
i_to_n[current_year] += 1
end
elsif authorhash[current_year][current_user] == nil
if authorhash[current_year][prev_post[0]] == "conservative"
c_to_n[current_year] += 1
else
i_to_n[current_year] += 1
end
else
c_to_i[current_year] += 1
end
end
end
if countusers
if authorhash[current_year][current_user] == "conservative"
if authorhash[current_year][prev_post[0]] == "conservative"
cusers_to_cusers[current_year][current_user] = true
cusers_to_cusers[current_year][prev_post[0]] = true
elsif authorhash[current_year][prev_post[0]] == "innovative"
cusers_to_iusers[current_year][current_user] = true
#cusers_to_cusers[current_year][prev_post[0]] = true
else
cusers_to_nusers[current_year][current_user] = true
end
elsif authorhash[current_year][prev_post[0]] == "conservative"
if authorhash[current_year][current_user] == "innovative"
cusers_to_iusers[current_year][prev_post[0]] = true
#cusers_to_cusers[current_year][prev_post[0]] = true
else
cusers_to_nusers[current_year][prev_post[0]] = true
end
end
end
if countactual
#count (different) users as well?
#if c_posts[prev_post[2]] != 0 or i_posts[prev_post[2]] != 0
# STDOUT.puts "prev\t#{current_post}\t#{prev_post[2]}\t#{current_user}\t#{authorhash[current_year][current_user]}\t#{c_posts[prev_post[2]]}\t#{i_posts[prev_post[2]]}"
#end
#if c_posts[current_post] != 0 or i_posts[current_post] != 0
# STDOUT.puts "next\t#{current_post}\t#{prev_post[2]}\t#{prev_post[0]}\t#{authorhash[current_year][prev_post[0]]}\t#{c_posts[current_post]}\t#{i_posts[current_post]}"
#end
#end
if authorhash[current_year][current_user] == "conservative"
c_to_c[current_year] += c_posts[prev_post[2]]
c_to_i[current_year] += i_posts[prev_post[2]]
elsif authorhash[current_year][current_user] == "innovative"
i_to_c[current_year] += c_posts[prev_post[2]]
i_to_i[current_year] += i_posts[prev_post[2]]
else
n_to_c[current_year] += c_posts[prev_post[2]]
n_to_i[current_year] += i_posts[prev_post[2]]
end
if authorhash[current_year][prev_post[0]] == "conservative"
c_to_c[current_year] += c_posts[current_post]
c_to_i[current_year] += i_posts[current_post]
elsif authorhash[current_year][prev_post[0]] == "innovative"
i_to_c[current_year] += c_posts[current_post]
i_to_i[current_year] += i_posts[current_post]
else
n_to_c[current_year] += c_posts[current_post]
n_to_i[current_year] += i_posts[current_post]
end
if authors[current_user]
exposure_year_author_c[current_year][current_user] += c_posts[prev_post[2]]
exposure_year_author_i[current_year][current_user] += i_posts[prev_post[2]]
end
if authors[prev_post[0]]
exposure_year_author_c[current_year][prev_post[0]] += c_posts[current_post]
exposure_year_author_i[current_year][prev_post[0]] += i_posts[current_post]
end
end
end
#end
end
if !(prev_posts.length < threshold_post_distance)
prev_posts.shift
end
prev_posts << [current_user, current_date, current_post]
#end
end
elsif line1.include?("# post_id")
current_post = line1.split(" = ")[1].split("-")[0]
elsif line1.include?("# post_date")
current_date = line1.split("=")[1].strip.split(" ")[0]
current_year = line1.split(" = ")[1].split("-")[0].to_i
elsif line1.include?("# thread_id")
current_thread = line1.split("=")[1].strip
if current_thread != prev_thread
if prev_thread != ""
prev_posts = []
end
if prev_thread != ""
prev_thread = current_thread
end
end
end
end
end
end
#add direction?
if countinteractions
o1 = File.open("flashback_hbtq_interactions_n.tsv","w:utf8")
o1.puts "year\tc_to_n_by_c\tc_to_c_by_c\tc_to_i_by_c\tc_total\tc_to_i_by_i\ti_to_n_by_i\ti_to_i_by_i\ti_total\tn_to_n"
authorhash.keys.sort.each do |year|
c_total = c_to_c[year] + c_to_i[year] + c_to_n[year]
i_total = i_to_i[year] + c_to_i[year] + i_to_n[year]
#total = c_to_c[year] + c_to_i[year] + i_to_i[year] + c_to_n[year] + i_to_n[year]
#STDOUT.puts "#{year}\t#{c_to_c[year]/total}\t#{c_to_i[year]/total}\t#{i_to_i[year]/total}\t#{c_to_n[year]/total}\t#{i_to_n[year]/total}\t#{total}"
o1.puts "#{year}\t#{c_to_n[year]/c_total}\t#{c_to_c[year]/c_total}\t#{c_to_i[year]/c_total}\t#{c_total}\t#{c_to_i[year]/i_total}\t#{i_to_n[year]/i_total}\t#{i_to_i[year]/i_total}\t#{i_total}\t#{n_to_n[year]}"
end
o1.close
o2 = File.open("flashback_hbtq_interactions.tsv","w:utf8")
o2.puts "year\tc_to_c\tc_to_i\ti_to_i\tc_users\ti_users\c_to_c_by_cusers\tc_to_i_by_cursers\ti_to_i_by_c\tc_total\tc_to_i_by_i\ti_to_n_by_i\ti_to_i_by_i\ti_total\tn_to_n"
authorhash.keys.sort.each do |year|
c_total = c_to_c[year] + c_to_i[year] + c_to_n[year]
i_total = i_to_i[year] + c_to_i[year] + i_to_n[year]
#total = c_to_c[year] + c_to_i[year] + i_to_i[year] + c_to_n[year] + i_to_n[year]
#STDOUT.puts "#{year}\t#{c_to_c[year]/total}\t#{c_to_i[year]/total}\t#{i_to_i[year]/total}\t#{c_to_n[year]/total}\t#{i_to_n[year]/total}\t#{total}"
o1.puts "#{year}\t#{c_to_n[year]/c_total}\t#{c_to_c[year]/c_total}\t#{c_to_i[year]/c_total}\t#{c_total}\t#{c_to_i[year]/i_total}\t#{i_to_n[year]/i_total}\t#{i_to_i[year]/i_total}\t#{i_total}\t#{n_to_n[year]}"
end
o1.close
end
if countusers
STDOUT.puts "year\tcusers_to_cusers\tcusers_to_iusers\tcusers_to_nusers\ttotal"
authorhash.keys.sort.each do |current_year|
total = (cusers_to_cusers[current_year].keys.length + cusers_to_iusers[current_year].keys.length + cusers_to_nusers[current_year].keys.length).to_f
STDOUT.puts "#{current_year}\t#{cusers_to_cusers[current_year].keys.length/total}\t#{cusers_to_iusers[current_year].keys.length/total}\t#{cusers_to_nusers[current_year].keys.length/total}\t#{total}"
end
end
if countactual
o1 = File.open("flashback_hbtq_actual_per_speaker_aggregated#{addendum}.tsv","w:utf-8")
o1.puts "year\tc_to_c\tc_to_i\ti_to_c\ti_to_i\tn_to_c\tn_to_i"
authorhash.keys.sort.each do |current_year|
o1.puts "#{current_year}\t#{c_to_c[current_year]}\t#{c_to_i[current_year]}\t#{i_to_c[current_year]}\t#{i_to_i[current_year]}\t#{n_to_c[current_year]}\t#{n_to_i[current_year]}"
end
o2 = File.open("flashback_hbtq_actual_per_speaker_individual#{addendum}.tsv","w:utf-8")
o2.puts "year\tspeaker\tinnovativity\texposure_to_c\texposure_to_i"
authors.keys.each do |author|
authorhash.keys.sort.each do |current_year|
o2.puts "#{current_year}\t#{author}\t#{innovativity_year_author[current_year][author]}\t#{exposure_year_author_c[current_year][author]}\t#{exposure_year_author_i[current_year][author]}"
end
end
end