-
Notifications
You must be signed in to change notification settings - Fork 1
/
vizzstat.py
executable file
·332 lines (298 loc) · 14.3 KB
/
vizzstat.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This is vizzstat, a program to analyze netvizz-files.
# Copyright (C) 2014 DAMIAN TRILLING
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Please contact me via d dot c dot trilling at uva dot nl
from __future__ import division
import codecs, csv, cStringIO
import sys
from numpy import mean, std, amin, amax, median
from collections import Counter
from scipy.stats import skew, kurtosis
from collections import defaultdict
import argparse
from string import punctuation
from itertools import combinations
class UTF8Recoder(object):
def __init__(self, f, encoding):
self.reader = codecs.getreader(encoding)(f)
def __iter__(self):
return self
def next(self):
return self.reader.next().encode("utf-8")
class CsvUnicodeReader(object):
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwargs):
f = UTF8Recoder(f, encoding)
self.reader = csv.reader(f, dialect=dialect, **kwargs)
def next(self):
row = self.reader.next()
return [unicode(s, "utf-8") for s in row]
def __iter__(self):
return self
class TsvUnicodeReader(object):
def __init__(self, f, dialect=csv.excel_tab, encoding="utf-8", **kwargs):
f = UTF8Recoder(f, encoding)
self.reader = csv.reader(f, dialect=dialect, **kwargs)
def next(self):
row = self.reader.next()
return [unicode(s, "utf-8") for s in row]
def __iter__(self):
return self
def unique_list(l):
ulist = []
[ulist.append(x) for x in l if x not in ulist]
return ulist
def descriptives_users (filename):
i=0
nousers=0
locale=defaultdict(int)
gender=defaultdict(int)
likecount=[]
commentcount=[]
reader=CsvUnicodeReader(open(filename,"rb"))
for row in reader:
if i>=1:
# only read the first part of the GDF file (the nodes), stop when edges-section begins
if row[0]=="edgedef>node1 VARCHAR":
break
# only process USER nodes, niet posts
if row[2]=="user":
nousers+=1
locale[row[6]] +=1
gender[row[7]] +=1
likecount.append(int(row[8]))
commentcount.append(int(row[10]))
# for the moment ignoring the other values. we could read, e.g., the number of likes to regress gender on it or the like. could be added later.
i+=1
print "\nThis is your report for the datafile %s. It is assumed to be a GDF file created by Netvizz. \n" %(filename)
print "- There are {0} users who engaged in any form with the debate".format(nousers)
print "- Their gender:"
for k in sorted(gender.iteritems(),key=lambda (k,v):v,reverse=True):
print k[0]+"\t"+str(k[1])
print "- Their locale settings:"
for k in sorted(locale.iteritems(),key=lambda (k,v):v,reverse=True):
print k[0]+"\t"+str(k[1])
print "- The number of likes a user made is M = {0:.2f} (SD = {1:.2f}) times; min = {2}, max = {3}; skewnewss = {4:.2f}, kurtosis = {5:.2f}".format(mean(likecount), std(likecount), amin(likecount), amax(likecount), skew(likecount), kurtosis(likecount))
print "- The number of comments a user made is M = {0:.2f} (SD = {1:.2f}) times; min = {2}, max = {3}; skewnewss = {4:.2f}, kurtosis = {5:.2f}".format(mean(commentcount), std(commentcount), amin(commentcount), amax(commentcount), skew(commentcount), kurtosis(commentcount))
def descriptives_comments (filename):
isreply=0
likecount=[]
reader=TsvUnicodeReader(open(filename,"rb"))
i=0
for row in reader:
if i>=1:
isreply=isreply+int(row[6])
likecount.append(int(row[9]))
i+=1
print "\nThis is your report for the datafile %s. It is assumed to be a netvizz-file containing data on facebook-COMMENTS. \n" %(filename)
print "- In total, there are {0} comments in the dataset. {1} are replies, {2} are not.".format(len(likecount), isreply, len(likecount)-isreply)
print "- The comments are liked M = {0:.2f} (SD = {1:.2f}) times; min = {2}, max = {3}; skewnewss = {4:.2f}, kurtosis = {5:.2f}".format(mean(likecount), std(likecount), amin(likecount), amax(likecount), skew(likecount), kurtosis(likecount))
print "\n"
def descriptives_posts (filename):
nocomments=[]
nocommentsbase=[]
nocommentsreply=[]
noshares=[]
nocomment_likes=[]
noengagement=[]
nolikes=[]
photo=0
video=0
link=0
status=0
reader=TsvUnicodeReader(open(filename,"rb"))
i=0
for row in reader:
if i>=1:
try:
nolikes.append(int(row[8]))
except:
nolikes.append(0)
try:
nocomments.append(int(row[10]))
except:
nocomments.append(0)
try:
nocommentsbase.append(int(row[11]))
except:
nocommentsbase.append(0)
try:
nocommentsreply.append(int(row[12]))
except:
nocommentsreply.append(0)
try:
noshares.append(int(row[13]))
except:
noshares.append(0)
try:
nocomment_likes.append(int(row[14]))
except:
nocomment_likes.append(0)
try:
noengagement.append(int(row[15]))
except:
noengagement.append(0)
if row[0]=="video":
video+=1
if row[0]=="photo":
photo+=1
if row[0]=="status":
status+=1
if row[0]=="link":
link+=1
i+=1
print "\nThis is your report for the datafile %s. It is assumed to be a netvizz-file containing data on facebook-POSTS. \n" %(filename)
print "- In total, there are {0} posts in the dataset, containing {1} status updates, {2} links, {3} photos and {4} videos.".format(len(nolikes), status, link, photo, video)
print "- The posts are liked M = {0:.2f} (SD = {1:.2f}) times; min = {2}, max = {3}; skewnewss = {4:.2f}, kurtosis = {5:.2f}".format(mean(nolikes), std(nolikes), amin(nolikes), amax(nolikes), skew(nolikes), kurtosis(nolikes))
print "- The posts have been shared M = {0:.2f} (SD = {1:.2f}) times; min = {2}, max = {3}; skewnewss = {4:.2f}, kurtosis = {5:.2f}".format(mean(noshares), std(noshares), amin(noshares), amax(noshares), skew(noshares), kurtosis(noshares))
print "- There are M = {0:.2f} (SD = {1:.2f}) comments on these posts; min = {2}, max = {3}; skewnewss = {4:.2f}, kurtosis = {5:.2f}".format(mean(nocomments), std(nocomments), amin(nocomments), amax(nocomments), skew(nocomments), kurtosis(nocomments))
print "- There are M = {0:.2f} (SD = {1:.2f}) direct comments on these posts; min = {2}, max = {3}; skewnewss = {4:.2f}, kurtosis = {5:.2f}".format(mean(nocommentsbase), std(nocommentsbase), amin(nocommentsbase), amax(nocommentsbase), skew(nocommentsbase), kurtosis(nocommentsbase))
# engagement is(users that comment or like the same post are connected
print "- There are M = {0:.2f} (SD = {1:.2f}) comments which are replies to other comments; min = {2}, max = {3}; skewnewss = {4:.2f}, kurtosis = {5:.2f}".format(mean(nocommentsreply), std(nocommentsreply), amin(nocommentsreply), amax(nocommentsreply), skew(nocommentsreply), kurtosis(nocommentsreply))
print "- The number of aggregated comment likes per post is M = {0:.2f} (SD = {1:.2f}) times; min = {2}, max = {3}; skewnewss = {4:.2f}, kurtosis = {5:.2f}".format(mean(nocomment_likes), std(nocomment_likes), amin(nocomment_likes), amax(nocomment_likes), skew(nocomment_likes), kurtosis(nocomment_likes))
print "- The enagement (= shares + likes + comments + comment likes) per post is M = {0:.2f} (SD = {1:.2f}) times; min = {2}, max = {3}; skewnewss = {4:.2f}, kurtosis = {5:.2f}".format(mean(noengagement), std(noengagement), amin(noengagement), amax(noengagement), skew(noengagement), kurtosis(noengagement))
print "\n"
print "As a table:\n"
print " \tM\tSD\tmin\tmax\tskew\tkurtosis"
print "Likes \t{0:.2f}\t{1:.2f}\t{2}\t{3}\t{4:.2f}\t{5:.2f}".format(mean(nolikes), std(nolikes), amin(nolikes), amax(nolikes), skew(nolikes), kurtosis(nolikes))
print "Shares \t{0:.2f}\t{1:.2f}\t{2}\t{3}\t{4:.2f}\t{5:.2f}".format(mean(noshares), std(noshares), amin(noshares), amax(noshares), skew(noshares), kurtosis(noshares))
print "Comments\t{0:.2f}\t{1:.2f}\t{2}\t{3}\t{4:.2f}\t{5:.2f}".format(mean(nocomments), std(nocomments), amin(nocomments), amax(nocomments), skew(nocomments), kurtosis(nocomments))
print "– direct\t{0:.2f}\t{1:.2f}\t{2}\t{3}\t{4:.2f}\t{5:.2f}".format(mean(nocommentsbase), std(nocommentsbase), amin(nocommentsbase), amax(nocommentsbase), skew(nocommentsbase), kurtosis(nocommentsbase))
# engagement is(users that comment or like the same post are connected
print "– replies\t{0:.2f}\t{1:.2f}\t{2}\t{3}\t{4:.2f}\t{5:.2f}".format(mean(nocommentsreply), std(nocommentsreply), amin(nocommentsreply), amax(nocommentsreply), skew(nocommentsreply), kurtosis(nocommentsreply))
print "ComLikes1\t{0:.2f}\t{1:.2f}\t{2}\t{3}\t{4:.2f}\t{5:.2f}".format(mean(nocomment_likes), std(nocomment_likes), amin(nocomment_likes), amax(nocomment_likes), skew(nocomment_likes), kurtosis(nocomment_likes))
print "Enagement2\t{0:.2f}\t{1:.2f}\t{2}\t{3}\t{4:.2f}\t{5:.2f}".format(mean(noengagement), std(noengagement), amin(noengagement), amax(noengagement), skew(noengagement), kurtosis(noengagement))
print "1 The aggregated number of likes a comment on a post receives"
print "2 engagement = shares + likes + comments + comment likes"
print "\n"
print "\n"
def content(filename,textcolumn):
countbestand=filename+"_count.csv"
print textcolumn
print type(textcolumn)
# TODO: LIJST MET STOPWOORDEN DOOR GEBRUIKER LATEN AANLEVEREN
eruitgooien=[""]
# how many words to include in word count. For the most often used 10%, write 0.1"
percentage=0.05
i=0
messages_list=[]
global messages_processed_list
messages_processed_list=[]
lengte_list=[]
print "Opening",filename
reader=TsvUnicodeReader(open(filename,"r"))
print "Reading message nr. "
for row in reader:
i=i+1
print "\r",str(i)," ",
sys.stdout.flush()
messages_list.append(row[textcolumn])
lengte_list.append(len(row[textcolumn]))
print str(i)+" messages, mean length: "+str(mean(lengte_list))+", SD = "+str(std(lengte_list))+", median = "+str(median(lengte_list))
i=0
print "Processing message nr. "
for message in messages_list:
i=i+1
print "\r",str(i)," ",
sys.stdout.flush()
for p in list(punctuation):
message_processed=message.replace(p,' ')
message_processed = ' '.join([word for word in message_processed.split() if word not in (eruitgooien)])
# message_processed = ' '.join([word for word in message_processed.split() if len(word)>1])
# dubbele woorden eruit
message_processed=' '.join(unique_list(message_processed.split()))
messages_processed_list.append(message_processed)
wordlist=[]
for message in messages_processed_list:
wordlist=wordlist+message.split()
numberofwords=len(wordlist)
numberofwordsmostfreq=int(numberofwords*percentage)
print "\nThere are "+str(numberofwords)+" words in the dataset. We will count the most frequently used "+ str(numberofwordsmostfreq) + "words, which is a fraction of "+str(percentage)+"."
print "Counting words ..."
global c
c=Counter(wordlist)
print "Write count table to"+countbestand
with codecs.open(countbestand, "wb", encoding="utf-8", errors="replace") as f:
for k,v in c.most_common(numberofwordsmostfreq):
f.write( u"{},{}\n".format(v,k) )
def cooccurrences(filename):
gdfbestand=filename+"_netwerk.gdf"
minedgeweight=0
cooc=defaultdict(int)
tweets=[]
f = codecs.open(gdfbestand, 'wb', encoding="utf-8")
for message in messages_processed_list:
words=message.split()
for a,b in combinations(words,2):
if a!=b:
cooc[(a,b)]+=1
f.write("nodedef>name VARCHAR, width DOUBLE\n")
algenoemd=[]
verwijderen=[]
for k in cooc:
if cooc[k]<minedgeweight:
verwijderen.append(k)
else:
if k[0] not in algenoemd:
f.write(k[0]+","+str(c[k[0]])+"\n")
algenoemd.append(k[0])
if k[1] not in algenoemd:
f.write(k[1]+","+str(c[k[1]])+"\n")
algenoemd.append(k[1])
for k in verwijderen:
del cooc[k]
f.write("edgedef>node1 VARCHAR,node2 VARCHAR, weight DOUBLE\n")
for k, v in cooc.iteritems():
regel= ",".join(k)+","+str(v)
f.write(regel+"\n")
print "Done! GDF-file writetn to",gdfbestand
# start main program
print "vizzstat Copyright (C) 2014 Damian Trilling"
print "This program comes with ABSOLUTELY NO WARRANTY."
print "This is free software, and you are welcome to redistribute it under certain conditions; read the file LICENSE which you received with this program."
print "\n"
parser=argparse.ArgumentParser(description='This program creates a report with summary statistics of Facebook data. As input, it takes files created py the "page data" function provided by netvizz (apps.facebook.com/netvizz)')
parser.add_argument("filename", help="The file you want to analyze")
parser.add_argument("filetype", choices=["a","b","c"], help="a b or c - the file type as provided by netvizz: (a) bipartite graph file in gdf format that shows posts, users, and connections between the two; (b) tabular file (tsv) that lists different metrics for each post; (c) tabular file (tsv) that contains the text of user comments.")
parser.add_argument("--descriptives", help="Provides general descriptive statistics", action="store_true")
parser.add_argument("--content", help="Analyzes the content and provides statistics like the average message length or the most frequently used words", action="store_true")
parser.add_argument("--cooccurrences", help="Equal to --content, but in addition, it procudes a GDF-file with word-cooccurrences", action="store_true")
args=parser.parse_args()
if args.descriptives:
if args.filetype=="a":
descriptives_users(args.filename)
if args.filetype=="b":
descriptives_posts(args.filename)
if args.filetype=="c":
descriptives_comments(args.filename)
if args.content:
if args.filetype=="c":
content(args.filename,7)
if args.filetype=="b":
content(args.filename,2)
if args.filetype=="a":
print "-- content is not available for graph files (type a). Please make a different choice. "
if args.cooccurrences:
if args.filetype=="a":
print "-- cooccurances is not available for graph files (type a). Please make a different choice. "
# first call content, it's a prerequisite
if args.filetype=="c":
content(args.filename,7)
if args.filetype=="b":
content(args.filename,2)
# now the real work:
cooccurrences(args.filename)