forked from maribelacosta/whovis
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wikistats.py
887 lines (724 loc) · 41.2 KB
/
wikistats.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
#!/bin/env python2
import os
import WikiwhoRelationships
from copy import deepcopy
import operator
from sys import argv,exit
import getopt
from wmf import dump
from structures.Revision import Revision
import re
import datetime
#from django.utils import simplejson
def getStatsOfFile(revisions, order, relations, tags):
statData = []
editDistSum = {}
totalEditCount = 0
all_authors = set([])
all_editors = []
prev_revision = None
outgoing_negative_actions = {}
incoming_negative_actions = {}
self_reintroductions = []
self_supports = []
all_antagonized_editors = []
all_supported_editors = []
for (rev_id, vandalism) in order:
if (vandalism):
data = {'revision' : rev_id,
'wikiGini V1':0,
#'wikiGini V2':0,
#'totalLength': 0,
'lengthChange' : 0,
'editRapidness': 0,
'antagonizedEditors': 0,
'antagonized_editors_avg_w1' : 0,
'antagonisticActions': 0,
'supportedEditors': 0,
'supported_editors_avg_w1' : 0,
'supportiveActions': 0,
#'tokenActions': 0,
'giniEditorship': 0,
'giniEditorship_w1': 0,
'giniOutgoingNegativeActions': 0,
'giniIncomingNegativeActions': 0,
'selfReintroductionRatio': 0,
'selfReintroductionRatio_avg_w1':0,
'selfSupportRatio': 0,
'selfSupportRatio_avg_w1': 0,
'statEditors': 0,
#'vandalism': 1,
'maintained': 0,
'npov': 0,
'goodArticle': 0,
'featuredArticle': 0,
'disputed': 0,
'protected': 0}
statData.append(data)
#prev_revision = rev_id
continue
# Compute authorship distribution information
revision = revisions[rev_id]
relation = relations[revision.wikipedia_id]
authors = getAuthorshipDataFromRevision(revision)
all_authors.update(set(authors))
authDistSum = sumAuthDist(authors)
sortedAuthDistSum = sorted(authDistSum.iteritems(), key=operator.itemgetter(1))
totalWordCount = len(authors)
totalAuthorCountAS = len(sortedAuthDistSum)
# Compute editorship distribution information
all_editors.append(revision.contributor_name)
editDistSum = sumAuthDist(all_editors)
sortedEditDistSum = sorted(editDistSum.iteritems(), key=operator.itemgetter(1))
# editorship with different windows
window1 = 50
#window2 = 20
# editorship with window1
editDistSum_w1 = None
if (len(all_editors)>=window1):
editDistSum_w1 = sumAuthDist(all_editors[len(all_editors)-window1:])
sortedEditDistSum_w1 = sorted(editDistSum_w1.iteritems(), key=operator.itemgetter(1))
# editorship with window2
#editDistSum_w2 = None
#if (len(all_editors)>=window2):
# editDistSum_w2 = sumAuthDist(all_editors[len(all_editors)-window2:])
# sortedEditDistSum_w2 = sorted(editDistSum_w2.iteritems(), key=operator.itemgetter(1))
# Compute wikigini: V1
i = 1
res = 0
for tup in sortedAuthDistSum:
res = res + (i * tup[1])
i = i + 1
wikiGini = ((2.0 * res)/ (len(sortedAuthDistSum) * totalWordCount)) - ((len(sortedAuthDistSum) + 1.0 ) / len(sortedAuthDistSum))
# Compute wikigini: V2
i = len(all_authors) - len(sortedAuthDistSum) + 1
res = 0
for tup in sortedAuthDistSum:
res = res + (i * tup[1])
i = i + 1
wikiGini2 = ((2.0 * res)/ (len(all_authors) * totalWordCount)) - ((len(all_authors) + 1.0 ) / len(all_authors))
# Compute length change in percentage
if (prev_revision == None):
lengthChange = 0
else:
lengthChange = ((revision.length - revisions[prev_revision].length) / float(revisions[prev_revision].length))
# Compute edit rapidness
if (prev_revision == None):
editRapidness = 0
else:
editRapidness = (revision.timestamp - revisions[prev_revision].timestamp) / 3600.0
# antagonized_editors: Revert actions + delete actions in revision (distinct editors)
antagonized_editors = set([])
for elem in relation.revert.keys():
antagonized_editors.add(revisions[elem].contributor_id)
for elem in relation.deleted.keys():
antagonized_editors.add(revisions[elem].contributor_id)
all_antagonized_editors.append(len(antagonized_editors))
antagonized_editors_avg_w1 = 0
if (len(all_antagonized_editors) >= window1):
antagonized_editors_avg_w1 = sum(all_antagonized_editors[len(all_antagonized_editors)-window1:]) / float(window1)
# antagonistic_actions: Revert actions + delete actions in revision (number of tokens)
antagonistic_actions = 0
for elem in relation.revert.keys():
antagonistic_actions = antagonistic_actions + relation.revert[elem]
for elem in relation.deleted.keys():
antagonistic_actions = antagonistic_actions + relation.deleted[elem]
# supported_editors: reintroductions + redeletes (distinct editors)
supported_editors = set([])
for elem in relation.reintroduced.keys():
supported_editors.add(revisions[elem].contributor_id)
for elem in relation.redeleted.keys():
supported_editors.add(revisions[elem].contributor_id)
all_supported_editors.append(len(supported_editors))
supported_editors_avg_w1 = 0
if (len(all_supported_editors) >= window1):
supported_editors_avg_w1 = sum(all_supported_editors[len(all_supported_editors)-window1:]) / float(window1)
# supportive actions: reintroductions + redeletes (number of tokens)
supportive_actions = 0
for elem in relation.reintroduced.keys():
supportive_actions = supportive_actions + relation.reintroduced[elem]
for elem in relation.redeleted.keys():
supportive_actions = supportive_actions + relation.redeleted[elem]
# total number of token actions
tokenActions = 0
for elem in relation.deleted.keys():
tokenActions = tokenActions + relation.deleted[elem]
for elem in relation.reintroduced.keys():
tokenActions = tokenActions + relation.reintroduced[elem]
for elem in relation.redeleted.keys():
tokenActions = tokenActions + relation.redeleted[elem]
for elem in relation.revert.keys():
tokenActions = tokenActions + relation.revert[elem]
tokenActions = tokenActions + relation.added
# Compute gini editorship
i = 1
res = 0
for tup in sortedEditDistSum:
res = res + (i * tup[1])
i = i + 1
giniEditorship = ((2.0 * res)/ (len(sortedEditDistSum) * len(all_editors))) - ((len(sortedEditDistSum) + 1.0 ) / len(sortedEditDistSum))
# Compute gini editorship with window 1
giniEditorship_w1 = 0
if (editDistSum_w1 != None):
i = 1
res = 0
for tup in sortedEditDistSum_w1:
res = res + (i * tup[1])
i = i + 1
giniEditorship_w1 = ((2.0 * res)/ (len(sortedEditDistSum_w1) * window1)) - ((len(sortedEditDistSum_w1) + 1.0 ) / len(sortedEditDistSum_w1))
#giniEditorship_w2 = 0
#if (editDistSum_w2 != None):
# i = 1
# res = 0
# for tup in sortedEditDistSum_w2:
# res = res + (i * tup[1])
# i = i + 1
# giniEditorship_w2 = ((2.0 * res)/ (len(sortedEditDistSum_w2) * window2)) - ((len(sortedEditDistSum_w2) + 1.0 ) / len(sortedEditDistSum_w2))
# Computing gini of outgoing negative actions
#if (revision.contributor_name in outgoing_negative_actions.keys()):
if (revision.contributor_name in outgoing_negative_actions.keys()):
outgoing_negative_actions.update({revision.contributor_name: outgoing_negative_actions[revision.contributor_name] + antagonistic_actions})
else:
outgoing_negative_actions.update({revision.contributor_name: antagonistic_actions})
sortedNegDistSum = sorted(outgoing_negative_actions.iteritems(), key=operator.itemgetter(1))
i = 1
res = 0
for tup in sortedNegDistSum:
res = res + (i * tup[1])
i = i + 1
giniOutgoingNegativeActions = 0
if (sum(outgoing_negative_actions.values()) > 0):
# len(all_editors) represent the number of revisions
giniOutgoingNegativeActions = ((2.0 * res)/ (len(sortedNegDistSum) * sum(outgoing_negative_actions.values()))) - ((len(sortedNegDistSum) + 1.0 ) / len(sortedNegDistSum))
#print rev_id , sortedNegDistSum, giniOutgoingNegativeActions
#print "giniOutgoingNegativeActions", giniOutgoingNegativeActions, "outgoing_negative_actions", outgoing_negative_actions
# Computing gini of incoming negative actions
for elem in relation.revert.keys():
if elem in incoming_negative_actions.keys():
incoming_negative_actions.update({elem : incoming_negative_actions[elem] + relation.revert[elem]})
else:
incoming_negative_actions.update({elem : relation.revert[elem]})
for elem in relation.deleted.keys():
if elem in incoming_negative_actions.keys():
incoming_negative_actions.update({elem : incoming_negative_actions[elem] + relation.deleted[elem]})
else:
incoming_negative_actions.update({elem : relation.deleted[elem]})
sortedNegDistSum = sorted(incoming_negative_actions.iteritems(), key=operator.itemgetter(1))
i = 1
res = 0
for tup in sortedNegDistSum:
res = res + (i * tup[1])
i = i + 1
giniIncomingNegativeActions = 0
if (sum(incoming_negative_actions.values()) > 0):
# len(all_editors) represent the number of revisions
giniIncomingNegativeActions = ((2.0 * res)/ (len(sortedNegDistSum) * sum(incoming_negative_actions.values()))) - ((len(sortedNegDistSum) + 1.0 ) / len(sortedNegDistSum))
# self-reintroduction ratio
all_actions = float(relation.added + sum(relation.deleted.values()) + sum(relation.redeleted.values()) + sum(relation.reintroduced.values()) + sum(relation.revert.values()) + sum(relation.self_reintroduced.values()) + sum(relation.self_redeleted.values()) + sum(relation.self_deleted.values()) + sum(relation.self_revert.values()))
selfReintroductionRatio = 0
if (all_actions != 0):
selfReintroductionRatio = sum(relation.self_reintroduced.values()) / all_actions
self_reintroductions.append(selfReintroductionRatio)
selfReintroductionRatio_avg_w1 = 0
if (len(self_reintroductions) >= window1):
selfReintroductionRatio_avg_w1 = sum(self_reintroductions[len(self_reintroductions)-window1:]) / float(window1)
# self-supported actions ration
selfSupportRatio = 0
if (all_actions != 0):
selfSupportRatio = (sum(relation.self_reintroduced.values()) + sum(relation.self_redeleted.values())) / all_actions
self_supports.append(selfSupportRatio)
selfSupportRatio_avg_w1 = 0
if (len(self_reintroductions) >= window1):
selfSupportRatio_avg_w1 = (sum(self_reintroductions[len(self_reintroductions)-window1:]) + sum(self_supports[len(self_reintroductions)-window1:])) / float(window1)
# Update editor stats
statEditors = {}
for a in authors:
statEditors.update({a : {'wordOwnership' : authDistSum[a]/float(totalWordCount)}})
#print rev_id, statEditors, statEditors[(rev_id, revision.contributor_name) ]
positiveActions = 0
negativeActions = 0
if (all_actions != 0):
positiveActions = ((sum(relation.redeleted.values()) + sum(relation.reintroduced.values()))) / all_actions
negativeActions = ((sum(relation.deleted.values()) + sum(relation.revert.values()))) / all_actions
if ((rev_id, revision.contributor_name) in statEditors.keys()):
statEditors[revision.contributor_name].update({'positiveActions' : positiveActions})
statEditors[revision.contributor_name].update({'negativeActions' : negativeActions})
else:
statEditors.update({revision.contributor_name : {'wordOwnership' : 0}})
statEditors[revision.contributor_name ].update({'add' : relation.added})
statEditors[revision.contributor_name ].update({'positiveActions' : positiveActions})
statEditors[revision.contributor_name ].update({'negativeActions' : negativeActions})
# Compute maintained tag
maintained = 0
npov = 0
good_article = 0
featured_article = 0
disputed = 0
timestamps = tags.keys()
timestamps.sort()
for talk_ts in timestamps:
if talk_ts <= revision.timestamp:
for t in tags[talk_ts]:
# Handling "maintained" tag
if (t["tagname"] == "maintained") and (t["type"] == "addition"):
maintained = 1
elif (t["tagname"] == "maintained") and (t["type"] == "removal"):
maintained = 0
# Handling "npov" tag
elif (t["tagname"] == "npov") and (t["type"] == "addition"):
npov = 1
elif (t["tagname"] == "npov") and (t["type"] == "removal"):
npov = 0
# Handling "good article" tag
elif (t["tagname"] == "good article") and (t["type"] == "addition"):
good_article = 1
elif (t["tagname"] == "good article") and (t["type"] == "removal"):
good_article = 0
# Handling "featured article" tag
elif (t["tagname"] == "featured article") and (t["type"] == "addition"):
featured_article = 1
elif (t["tagname"] == "featured article") and (t["type"] == "removal"):
featured_article = 0
# Handling "disputed" tag
elif (t["tagname"] == "disputed") and (t["type"] == "addition"):
disputed = 1
elif (t["tagname"] == "disputed") and (t["type"] == "removal"):
disputed = 0
#################################################################################
#print "revision.content", revision.content
if "featured article" in revision.content:
#print "revision.content", revision.content
featured_article = 1
protected = 0
reglist = list()
reglist.append({"tagname": "good article", "regexp": re.compile('\{\{(articleissues\|((?:(?!\}\}).)*\||)|multiple issues\|((?:(?!\}\}).)*\||)|)good article((\||=)(?:(?!\}\}).)*|)\}\}', re.IGNORECASE)})
#reglist.append({"tagname": "featured article", "regexp": re.compile('\{\{(articleissues\|((?:(?!\}\}).)*\||)|multiple issues\|((?:(?!\}\}).)*\||)|)featured article((\||=)(?:(?!\}\}).)*|)\}\}', re.IGNORECASE)})
#reglist.append({"tagname": "featured article", "regexp": re.compile('\|currentstatus=FA', re.IGNORECASE)})
reglist.append({"tagname": "npov", "regexp": re.compile('\{\{(articleissues\|((?:(?!\}\}).)*\||)|multiple issues\|((?:(?!\}\}).)*\||)|)(pov|npov)((\||=)(?:(?!\}\}).)*|)\}\}', re.IGNORECASE)})
reglist.append({"tagname": "disputed", "regexp": re.compile('\{\{(articleissues\|((?:(?!\}\}).)*\||)|multiple issues\|((?:(?!\}\}).)*\||)|)disputed((\||=)(?:(?!\}\}).)*|)\}\}', re.IGNORECASE)})
#re_user = re.compile('({{|\[\[)user.*?[:|](.*?)[}/\]|]', re.IGNORECASE)
reglist.append({"tagname": "protected", "regexp": re.compile('\{\{(articleissues\|((?:(?!\}\}).)*\||)|multiple issues\|((?:(?!\}\}).)*\||)|)(pp-pc.?)((\||=)(?:(?!\}\}).)*|)\}\}', re.IGNORECASE)})
for regexp in reglist:
m = regexp["regexp"].search(revision.content)
if m:
if regexp["tagname"] == "disputed":
disputed = 1
elif (regexp["tagname"] == "good article"):
good_article = 1
elif (regexp["tagname"] == "npov"):
npov = 1
elif (regexp["tagname"] == "protected"):
protected = 1
data = {'revision' : revision.wikipedia_id,
'wikiGini V1':wikiGini,
#'wikiGini V2':wikiGini2,
#'totalLength': (revision.length)/(1024.0*1024.0),
'lengthChange' : lengthChange,
'editRapidness': editRapidness,
'antagonizedEditors': len(antagonized_editors),
'antagonized_editors_avg_w1' : antagonized_editors_avg_w1,
'antagonisticActions': antagonistic_actions,
'supportedEditors': len(supported_editors),
'supported_editors_avg_w1' : supported_editors_avg_w1,
'supportiveActions': supportive_actions,
#'tokenActions': tokenActions,
'giniEditorship': giniEditorship,
'giniEditorship_w1': giniEditorship_w1,
'giniOutgoingNegativeActions': giniOutgoingNegativeActions,
'giniIncomingNegativeActions': giniIncomingNegativeActions,
'selfReintroductionRatio': selfReintroductionRatio,
'selfReintroductionRatio_avg_w1':selfReintroductionRatio_avg_w1,
'selfSupportRatio': selfSupportRatio,
'selfSupportRatio_avg_w1': selfSupportRatio_avg_w1,
'statEditors': statEditors,
#'vandalism': 0,
'maintained': maintained,
'npov': npov,
'goodArticle': good_article,
'featuredArticle': featured_article,
'disputed': disputed,
'protected': protected}
statData.append(data)
#editorData = {'revision:', revision.wikipedia_id}
#if (prev_revision != None):
# print "timestamp", revision.timestamp, revisions[prev_revision].timestamp, revision.timestamp - revisions[prev_revision].timestamp
prev_revision = rev_id
return statData
def saveStatsToFile(filename, stats):
text_file = open(filename, "w")
text_file.write(stats)
text_file.close()
def sumAuthDist(authors):
wordCount = {}
for author in authors:
if(author in wordCount.keys()):
wordCount[author] = wordCount[author]+1
else:
wordCount[author] = 1
return wordCount
def getTagDatesFromPage(file_name):
# Compile regexp
reglist = list()
reglist.append({"tagname": "maintained", "regexp": re.compile('\{\{(articleissues\|((?:(?!\}\}).)*\||)|multiple issues\|((?:(?!\}\}).)*\||)|)maintained((\||=)(?:(?!\}\}).)*|)\}\}', re.IGNORECASE)})
reglist.append({"tagname": "good article", "regexp": re.compile('\{\{(articleissues\|((?:(?!\}\}).)*\||)|multiple issues\|((?:(?!\}\}).)*\||)|)good article((\||=)(?:(?!\}\}).)*|)\}\}', re.IGNORECASE)})
#reglist.append({"tagname": "featured article", "regexp": re.compile('\{\{(articleissues\|((?:(?!\}\}).)*\||)|multiple issues\|((?:(?!\}\}).)*\||)|)featured article((\||=)(?:(?!\}\}).)*|)\}\}', re.IGNORECASE)})
#reglist.append({"tagname": "featured article", "regexp": re.compile('\|currentstatus=FA', re.IGNORECASE)})
reglist.append({"tagname": "npov", "regexp": re.compile('\{\{(articleissues\|((?:(?!\}\}).)*\||)|multiple issues\|((?:(?!\}\}).)*\||)|)(pov|npov)((\||=)(?:(?!\}\}).)*|)\}\}', re.IGNORECASE)})
reglist.append({"tagname": "disputed", "regexp": re.compile('\{\{(articleissues\|((?:(?!\}\}).)*\||)|multiple issues\|((?:(?!\}\}).)*\||)|)disputed((\||=)(?:(?!\}\}).)*|)\}\}', re.IGNORECASE)})
re_user = re.compile('({{|\[\[)user.*?[:|](.*?)[}/\]|]', re.IGNORECASE)
#"({{|\[\[)user[\s\S]*?[:|]([\s\S]*?)[}/\]|]"
# Access the file.
dumpIterator = dump.Iterator(file_name)
# Revisions to compare.
revision_curr = Revision()
revision_prev = Revision()
text_curr = None
listOfTagChanges = {}
all_contributors = {"maintained": {}, "good article": {}, "featured article": {}, "npov": {}, "disputed": {}}
# Iterate over the pages.
for page in dumpIterator.readPages():
# Iterate over revisions of the article.
i = 0
prev_matched = list()
for revision in page.readRevisions():
revision.wikipedia_id = int(revision.getId())
revision.timestamp = revision.getTimestamp()
# Some revisions don't have contributor.
if (revision.getContributor() != None):
revision.contributor_id = revision.getContributor().getId()
revision.contributor_name = revision.getContributor().getUsername()
else:
revision.contributor_id = 'Not Available'
revision.contribur_name = 'Not Available'
text_curr = revision.getText()
if(text_curr):
text_curr = text_curr.encode('utf-8')
text_curr = text_curr.lower()
else:
continue
matched = list()
aux = list()
for regexp in reglist:
m = regexp["regexp"].search(text_curr)
if m:
mc = re_user.split(m.group(0))
i = 2
users = []
users.append(mc[2])
while (i+3 < len(mc)):
#print m[i+3]
users.append(mc[i+3])
i = i +3
#print regexp["tagname"], contributor
#print regexp["tagname"], users
matched.append(regexp["tagname"])
aux.append((regexp["tagname"], users))
##
#m_user = re_user.search(m.group(0))
#contributor = m_user.group(2)
if "|currentstatus=FA" in text_curr:
matched.append("featured article")
aux.append((revision.contributor_name,"featured article"))
# Calculate additions
for (match, contributor) in aux:
if not (match in prev_matched):
if not (revision.timestamp in listOfTagChanges.keys()):
listOfTagChanges[revision.timestamp] = list()
listOfTagChanges[revision.timestamp].append({"rev": revision.wikipedia_id, "type": "addition", "tagname": match, "wikiname": revision.contributor_name, "timestamp": revision.timestamp, "date": datetime.datetime.fromtimestamp(int(revision.timestamp)).strftime('%Y-%m-%d %H:%M:%S')})
all_contributors[match].update({revision.timestamp : {"rev": revision.wikipedia_id, "user":contributor, "date":datetime.datetime.fromtimestamp(int(revision.timestamp)).strftime('%Y-%m-%d %H:%M:%S')}})
# Calculate removals
for match in prev_matched:
if not (match in matched):
if not (revision.timestamp in listOfTagChanges.keys()):
listOfTagChanges[revision.timestamp] = list()
listOfTagChanges[revision.timestamp].append({"rev": revision.wikipedia_id, "type": "removal", "tagname": match, "wikiname": revision.contributor_name, "timestamp": revision.timestamp, "date": datetime.datetime.fromtimestamp(int(revision.timestamp)).strftime('%Y-%m-%d %H:%M:%S')})
prev_matched = matched
return listOfTagChanges, all_contributors
def getAuthorshipDataFromRevision(revision):
#print "Printing authorship for revision: ", revision.wikipedia_id
#text = []
authors = []
for hash_paragraph in revision.ordered_paragraphs:
p_copy = deepcopy(revision.paragraphs[hash_paragraph])
paragraph = p_copy.pop(0)
for hash_sentence in paragraph.ordered_sentences:
sentence = paragraph.sentences[hash_sentence].pop(0)
for word in sentence.words:
#text.append(word.value)
authors.append(word.author_id)
return authors
def printStats(stats):
# Stats to print
finalStats = {}
# Mappings of revisions and fake ids
revs = {}
# Stats per revisions
#wikiGini1 = []
#wikiGini2 = []
#totalLength = []
lengthChange = []
editRapidness = []
#antagonizedEditors = []
#antagonisticActions = []
#supportedEditors = []
#supportiveActions = []
#tokenActions = []
giniEditorship = []
giniEditorship_w1 = []
giniOutgoingNegativeActions = []
giniIncomingNegativeActions = []
#selfReintroductionRatio = []
#selfSupportRatio = []
#selfReintroductionRatio_avg_w1 = []
#selfSupportRatio_avg_w1 = []
#vandalism = []
maintained = []
npov = []
goodArticle = []
featuredArticle = []
disputed = []
protected = []
antagonized_editors_avg_w1 = []
#supported_editors_avg_w1 = []
# Stats per editors
editorStats = {}
count = 0
for elem in stats:
#wikiGini1.append({"x": count, "y": elem['wikiGini V1'], "z": elem["revision"]})
#wikiGini2.append({"x": count, "y": elem['wikiGini V2'], "z": elem["revision"]})
#totalLength.append({"x": count, "y": elem['totalLength'], "z": elem["revision"]})
lengthChange.append({"x": count, "y": elem['lengthChange'], "z": elem["revision"]})
editRapidness.append({"x": count, "y": elem['editRapidness'], "z": elem["revision"]})
#antagonizedEditors.append({"x": count, "y": elem['antagonizedEditors'], "z": elem["revision"]})
antagonized_editors_avg_w1.append({"x": count, "y": elem['antagonized_editors_avg_w1'], "z": elem["revision"]})
#antagonisticActions.append({"x": count, "y": elem['antagonisticActions'], "z": elem["revision"]})
#supportedEditors.append({"x": count, "y": elem['supportedEditors'], "z": elem["revision"]})
#supported_editors_avg_w1.append({"x": count, "y": elem['supported_editors_avg_w1'], "z": elem["revision"]})
#supportiveActions.append({"x": count, "y": elem['supportiveActions'], "z": elem["revision"]})
#tokenActions.append({"x": count, "y": elem['tokenActions'], "z": elem["revision"]})
giniEditorship.append({"x": count, "y": elem['giniEditorship'], "z": elem["revision"]})
giniEditorship_w1.append({"x": count, "y": elem['giniEditorship_w1'], "z": elem["revision"]})
giniOutgoingNegativeActions.append({"x": count, "y": elem['giniOutgoingNegativeActions'], "z": elem["revision"]})
giniIncomingNegativeActions.append({"x": count, "y": elem['giniIncomingNegativeActions'], "z": elem["revision"]})
#selfReintroductionRatio.append({"x": count, "y": elem['selfReintroductionRatio'], "z": elem["revision"]})
#selfReintroductionRatio_avg_w1.append({"x": count, "y": elem['selfReintroductionRatio_avg_w1'], "z": elem["revision"]})
#selfSupportRatio.append({"x": count, "y": elem['selfSupportRatio'], "z": elem["revision"]})
#selfSupportRatio_avg_w1.append({"x": count, "y": elem['selfSupportRatio_avg_w1'], "z": elem["revision"]})
#vandalism.append({"x": count, "y": elem['vandalism'], "z": elem["revision"]})
maintained.append({"x": count, "y": elem['maintained'], "z": elem["revision"]})
npov.append({"x": count, "y": elem['npov'], "z": elem["revision"]})
goodArticle.append({"x": count, "y": elem['goodArticle'], "z": elem["revision"]})
protected.append({"x": count, "y": elem['protected'], "z": elem["revision"]})
featuredArticle.append({"x": count, "y": elem['featuredArticle'], "z": elem["revision"]})
disputed.append({"x": count, "y": elem['disputed'], "z": elem["revision"]})
#for editor in elem['statEditors']:
count = count + 1
#serie1 = {"key" : "WikiGini V1", "values": wikiGini1}
#serie2 = {"key" : "WikiGini V2", "values": wikiGini2, "disabled": "true"}
#serie3 = {"key" : "Total Length (MB)", "values": totalLength, "disabled": "true"}
serie4 = {"key" : "Length Change (%)", "values": lengthChange, "disabled": "true"}
serie5 = {"key" : "Edit Rapidness (h.)", "values": editRapidness, "disabled": "true"}
#serie6 = {"key" : "Antagonized editors", "values": antagonizedEditors, "disabled": "true"}
serie26 = {"key" : "Avg. editors disagreed with (window=50)", "values": antagonized_editors_avg_w1}
#serie7 = {"key" : "Antagonized actions", "values": antagonisticActions, "disabled": "true"}
#serie8 = {"key" : "Supported editors", "values": supportedEditors, "disabled": "true"}
#serie27 = {"key" : "Avg. Supported editors (window=50)", "values": supported_editors_avg_w1, "disabled": "true"}
#serie9 = {"key" : "Supportive actions", "values": supportiveActions, "disabled": "true"}
#serie10 = {"key" : "Total token actions", "values": tokenActions, "disabled": "true"}
serie11 = {"key" : "Gini editorship", "values": giniEditorship, "disabled": "true"}
serie12 = {"key" : "Gini editorship (window=50)", "values": giniEditorship_w1, "disabled": "true"}
serie14 = {"key" : "Gini outgoing dis. actions", "values": giniOutgoingNegativeActions, "disabled": "true"}
serie15 = {"key" : "Gini incoming dis. actions", "values": giniIncomingNegativeActions, "disabled": "true"}
#serie16 = {"key" : "Self-reintroduction ratio", "values": selfReintroductionRatio, "disabled": "true"}
#serie17 = {"key" : "Avg. Self-reintroduction ratio (window=50)", "values": selfReintroductionRatio_avg_w1, "disabled": "true"}
#serie18 = {"key" : "Self-support ratio", "values": selfSupportRatio, "disabled": "true"}
#serie19 = {"key" : "Avg. Self-support ratio (window=50)", "values": selfSupportRatio_avg_w1, "disabled": "true"}
#serie20 = {"key" : "Vandalism", "values": vandalism, "disabled": "true"}
serie21 = {"key" : "Template:Maintained", "values": maintained, "disabled": "true"}
serie22 = {"key" : "Template:NPOV", "values": npov, "disabled": "true"}
serie23 = {"key" : "Template:Good Article", "values": goodArticle, "disabled": "true"}
serie24 = {"key" : "Template:Featured Article", "values": featuredArticle, "disabled": "true"}
serie25 = {"key" : "Template:Disputed", "values": disputed, "disabled": "true"}
serie27 = {"key" : "Template:Protected", "values": protected, "disabled": "true"}
finalStats.update({'revisions' : [serie26, serie4, serie5, serie14, serie15, serie11, serie12, serie21, serie22, serie23, serie24, serie25, serie27]})
return "example = " + str(finalStats) + ";"
def printStatsCSV(stats):
# Stats to print
finalStats = {}
# Mappings of revisions and fake ids
revs = {}
# Stats per revisions
wikiGini1 = []
#wikiGini2 = []
#totalLength = []
lengthChange = []
editRapidness = []
antagonizedEditors = []
antagonisticActions = []
supportedEditors = []
supportiveActions = []
#tokenActions = []
giniEditorship = []
giniEditorship_w1 = []
giniOutgoingNegativeActions = []
giniIncomingNegativeActions = []
selfReintroductionRatio = []
selfSupportRatio = []
selfReintroductionRatio_avg_w1 = []
selfSupportRatio_avg_w1 = []
#vandalism = []
maintained = []
npov = []
goodArticle = []
featuredArticle = []
disputed = []
antagonized_editors_avg_w1 = []
supported_editors_avg_w1 = []
# Stats per editors
editorStats = {}
lines = []
header = ["WikiGini V1", "Length Change (%)"] #...
",".join(header)
lines.append(header)
for elem in stats:
row = []
row.append(elem["revision"])
row.append(elem['wikiGini V1'])
row.append(elem['lengthChange'])
row.append(elem['editRapidness'])
row.append(elem['antagonizedEditors'])
row.append(elem['antagonized_editors_avg_w1'])
row.append(elem['antagonisticActions'])
row.append(elem['supportedEditors'])
# ...
",".join(row)
lines.append(row)
"\n".join(lines)
# ...
# antagonisticActions.append({"x": count, "y": elem['antagonisticActions'], "z": elem["revision"]})
# supportedEditors.append({"x": count, "y": elem['supportedEditors'], "z": elem["revision"]})
# supported_editors_avg_w1.append({"x": count, "y": elem['supported_editors_avg_w1'], "z": elem["revision"]})
#
# supportiveActions.append({"x": count, "y": elem['supportiveActions'], "z": elem["revision"]})
# #tokenActions.append({"x": count, "y": elem['tokenActions'], "z": elem["revision"]})
# giniEditorship.append({"x": count, "y": elem['giniEditorship'], "z": elem["revision"]})
# giniEditorship_w1.append({"x": count, "y": elem['giniEditorship_w1'], "z": elem["revision"]})
#
# giniOutgoingNegativeActions.append({"x": count, "y": elem['giniOutgoingNegativeActions'], "z": elem["revision"]})
# giniIncomingNegativeActions.append({"x": count, "y": elem['giniIncomingNegativeActions'], "z": elem["revision"]})
# selfReintroductionRatio.append({"x": count, "y": elem['selfReintroductionRatio'], "z": elem["revision"]})
# selfReintroductionRatio_avg_w1.append({"x": count, "y": elem['selfReintroductionRatio_avg_w1'], "z": elem["revision"]})
# selfSupportRatio.append({"x": count, "y": elem['selfSupportRatio'], "z": elem["revision"]})
# selfSupportRatio_avg_w1.append({"x": count, "y": elem['selfSupportRatio_avg_w1'], "z": elem["revision"]})
# #vandalism.append({"x": count, "y": elem['vandalism'], "z": elem["revision"]})
# maintained.append({"x": count, "y": elem['maintained'], "z": elem["revision"]})
# npov.append({"x": count, "y": elem['npov'], "z": elem["revision"]})
# goodArticle.append({"x": count, "y": elem['goodArticle'], "z": elem["revision"]})
# featuredArticle.append({"x": count, "y": elem['featuredArticle'], "z": elem["revision"]})
# disputed.append({"x": count, "y": elem['disputed'], "z": elem["revision"]})
#for editor in elem['statEditors']:
#
#
#
# serie1 = {"key" : "WikiGini V1", "values": wikiGini1}
# #serie2 = {"key" : "WikiGini V2", "values": wikiGini2, "disabled": "true"}
# #serie3 = {"key" : "Total Length (MB)", "values": totalLength, "disabled": "true"}
# serie4 = {"key" : "Length Change (%)", "values": lengthChange, "disabled": "true"}
# serie5 = {"key" : "Edit Rapidness (h.)", "values": editRapidness, "disabled": "true"}
# serie6 = {"key" : "Antagonized editors", "values": antagonizedEditors, "disabled": "true"}
# serie26 = {"key" : "Avg. Antagonized editors (window=50)", "values": antagonized_editors_avg_w1, "disabled": "true"}
#
# serie7 = {"key" : "Antagonized actions", "values": antagonisticActions, "disabled": "true"}
# serie8 = {"key" : "Supported editors", "values": supportedEditors, "disabled": "true"}
# serie27 = {"key" : "Avg. Supported editors (window=50)", "values": supported_editors_avg_w1, "disabled": "true"}
#
# serie9 = {"key" : "Supportive actions", "values": supportiveActions, "disabled": "true"}
# #serie10 = {"key" : "Total token actions", "values": tokenActions, "disabled": "true"}
# serie11 = {"key" : "Gini editorship", "values": giniEditorship, "disabled": "true"}
# serie12 = {"key" : "Gini editorship (window=50)", "values": giniEditorship_w1, "disabled": "true"}
#
# serie14 = {"key" : "Gini outgoing neg. actions", "values": giniOutgoingNegativeActions, "disabled": "true"}
# serie15 = {"key" : "Gini incoming neg. actions", "values": giniIncomingNegativeActions, "disabled": "true"}
# serie16 = {"key" : "Self-reintroduction ratio", "values": selfReintroductionRatio, "disabled": "true"}
# serie17 = {"key" : "Avg. Self-reintroduction ratio (window=50)", "values": selfReintroductionRatio_avg_w1, "disabled": "true"}
# serie18 = {"key" : "Self-support ratio", "values": selfSupportRatio, "disabled": "true"}
# serie19 = {"key" : "Avg. Self-support ratio (window=50)", "values": selfSupportRatio_avg_w1, "disabled": "true"}
# #serie20 = {"key" : "Vandalism", "values": vandalism, "disabled": "true"}
# serie21 = {"key" : "Maintained", "values": maintained, "disabled": "true"}
# serie22 = {"key" : "NPOV", "values": npov, "disabled": "true"}
# serie23 = {"key" : "Good Article", "values": goodArticle, "disabled": "true"}
# serie24 = {"key" : "Featured Article", "values": featuredArticle, "disabled": "true"}
# serie25 = {"key" : "Disputed", "values": disputed, "disabled": "true"}
#finalStats.update({'revisions' : [serie1, serie4, serie5, serie6, serie26, serie7, serie8, serie27, serie9, serie11, serie12, serie14, serie15, serie16, serie17, serie18, serie19, serie21, serie22, serie23, serie24, serie25]})
#return "example = " + str(finalStats) + ";"
return lines
def printTags(contributors, revisions, order, file_name):
maintainers = contributors["maintained"]
timestamps = maintainers.keys()
timestamps.sort()
csv_file = open(file_name, "w")
for (rev_id, vandalism) in order:
users = None
for talk_ts in timestamps:
if not(vandalism) and talk_ts <= revisions[rev_id].timestamp:
users = maintainers[talk_ts]["user"]
if (users != None):
csv_file.write(str(rev_id) + "\t" + "maintained" + "\t " + "\t".join(users) + "\n")
csv_file.close()
def main(my_argv):
inputfile = ''
output = None
if (len(my_argv) <= 3):
try:
opts, _ = getopt.getopt(my_argv,"i:",["ifile="])
except getopt.GetoptError:
print 'Usage: wikistats.py -i <inputfile> [-o <output>]'
exit(2)
else:
try:
opts, _ = getopt.getopt(my_argv,"i:o:",["ifile=","output="])
except getopt.GetoptError:
print 'Usage: wikistats.py -i <inputfile> [-o <output>]'
exit(2)
for opt, arg in opts:
if opt in ('-h', "--help"):
print "wikistats"
print
print 'Usage: wikistats.py -i <inputfile> [-rev <revision_id>]'
print "-i --ifile File to analyze"
print "-o --output Type of output. Options: 'json', 'table'. If not specified, JSON is the default."
print "-h --help This help."
exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--output"):
output = arg
return (inputfile,output)
if __name__ == '__main__':
(file_name, output) = main(argv[1:])
# Compute distribution information
(revisions, order, relations) = WikiwhoRelationships.analyseArticle(file_name)
# Compute file name of talk page data file
talkPageFileName = os.path.join(os.path.dirname(file_name), "talk_" + os.path.basename(file_name))
tags, contributors = getTagDatesFromPage(talkPageFileName)
printTags(contributors, revisions, order, file_name.replace(".xml", "_maintainers.csv"))
if (output == None or output == 'json'):
# Compute statistics
stats = getStatsOfFile(revisions, order, relations, tags)
print printStats(stats)
elif (output == 'table'):
WikiwhoRelationships.printRelationships(relations, order)
elif (output== 'csv'):
# Compute statistics
stats = getStatsOfFile(revisions, order, relations, tags)
print printStatsCSV(stats)
else:
print "Output format", output, "not supported"
#print stats
#time2 = time()
#pos = file_name.rfind("/")
#print file_name[pos+1: len(file_name)-len(".xml")], time2-time1
#printRelationships(relations, order)
#printRevision(revisions[11])
#print "Execution time:", time2-time1
#saveStatsToFile("/home/jurkan/Dokumente/Informatik/ciseminar/software/nethackstats.json", getStatsOfFile("/home/jurkan/Dokumente/Informatik/ciseminar/software/nethack.xml"))