-
Notifications
You must be signed in to change notification settings - Fork 4
/
WikiwhoRelationships.py
943 lines (733 loc) · 44.3 KB
/
WikiwhoRelationships.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
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
'''
Created on Feb 20, 2013
@author: Maribel Acosta
@author: Fabian Floeck
'''
from wmf import dump
from difflib import Differ
from time import time
from structures.Revision import Revision
from structures.Paragraph import Paragraph
from structures.Sentence import Sentence
from structures.Word import Word
from structures import Text
from etc.Relation import Relation
from sys import argv,exit
import getopt
from copy import deepcopy, copy
# Container of revisions.
revisions = {}
revision_order = []
# Hash tables.
paragraphs_ht = {}
sentences_ht = {}
spam = []
# SPAM detection variables.
CHANGE_PERCENTAGE = -0.40
PREVIOUS_LENGTH = 1000
CURR_LENGTH = 1000
FLAG = "move"
UNMATCHED_PARAGRAPH = 0.0
WORD_DENSITY = 10
WORD_LEN = 100
# Word global id.
WORD_ID = 0
def analyseArticle(file_name):
# Container of relationships.
relations = {}
# Revisions to compare.
revision_curr = Revision()
revision_prev = Revision()
text_curr = None
# Access the file.
dumpIterator = dump.Iterator(file_name)
# Iterate over the pages.
for page in dumpIterator.readPages():
i = 0
# Iterate over revisions of the article.
for revision in page.readRevisions():
vandalism = False
#print "processing rev", revision.getId()
# Update the information about the previous revision.
revision_prev = revision_curr
if (revision.getSha1() == None):
revision.setSha1(Text.calculateHash(revision.getText().encode("utf-8")))
if (revision.getSha1() in spam):
vandalism = True
#TODO: SPAM detection: DELETION
if (revision.getComment()!= None and revision.getComment().find(FLAG) > 0):
pass
else:
if (revision_prev.length > PREVIOUS_LENGTH) and (len(revision.getText()) < CURR_LENGTH) and (((len(revision.getText())-revision_prev.length)/float(revision_prev.length)) <= CHANGE_PERCENTAGE):
vandalism = True
revision_curr = revision_prev
#if (vandalism):
#print "---------------------------- FLAG 1"
#print "SPAM", revision.getId()
#print revision.getText()
#print
if (not vandalism):
# Information about the current revision.
revision_curr = Revision()
revision_curr.id = i
revision_curr.wikipedia_id = int(revision.getId())
revision_curr.length = len(revision.getText())
revision_curr.timestamp = revision.getTimestamp()
revision_curr.comment = revision.getComment()
# Relation of the current relation.
relation = Relation()
relation.revision = int(revision.getId())
relation.length = len(revision.getText())
# Some revisions don't have contributor.
if (revision.getContributor() != None):
revision_curr.contributor_id = revision.getContributor().getId()
revision_curr.contributor_name = revision.getContributor().getUsername().encode('utf-8')
relation.author = revision.getContributor().getUsername().encode('utf-8')
else:
revision_curr.contributor_id = 'Not Available ' + revision.getId()
revision_curr.contribur_name = 'Not Available ' + revision.getId()
relation.author = 'Not Available ' + revision.getId()
# Content within the revision.
text_curr = revision.getText().encode('utf-8')
text_curr = text_curr.lower()
revision_curr.content = text_curr
# Perform comparison.
vandalism = determineAuthorship(revision_curr, revision_prev, text_curr, relation)
if (not vandalism):
#print "NOT SPAM", revision.getId()
# Add the current revision with all the information.
revisions.update({revision_curr.wikipedia_id : revision_curr})
relations.update({revision_curr.wikipedia_id : relation})
revision_order.append((revision_curr.wikipedia_id, False))
# Update the fake revision id.
i = i+1
# Calculate the number of tokens in the revision.
total = 0
for p in revision_curr.ordered_paragraphs:
for paragraph_curr in revision_curr.paragraphs[p]:
for hash_sentence_curr in paragraph_curr.sentences.keys():
for sentence_curr in paragraph_curr.sentences[hash_sentence_curr]:
total = total + len(sentence_curr.words)
revision_curr.total_tokens = total
relation.total_tokens = total
else:
#print "---------------------------- FLAG 2"
#print "SPAM", revision.getId()
#print revision.getText()
#print
revision_order.append((revision_curr.wikipedia_id, True))
revision_curr = revision_prev
spam.append(revision.getSha1())
return (revisions, revision_order, relations)
def determineAuthorship(revision_curr, revision_prev, text_curr, relation):
# Containers for unmatched paragraphs and sentences in both revisions.
unmatched_sentences_curr = []
unmatched_sentences_prev = []
matched_sentences_prev = []
matched_words_prev = []
possible_vandalism = False
vandalism = False
# Analysis of the paragraphs in the current revision.
(unmatched_paragraphs_curr, unmatched_paragraphs_prev, matched_paragraphs_prev) = analyseParagraphsInRevision(revision_curr, revision_prev, text_curr, relation)
# Analysis of the sentences in the unmatched paragraphs of the current revision.
if (len(unmatched_paragraphs_curr)>0):
(unmatched_sentences_curr, unmatched_sentences_prev, matched_sentences_prev, _) = analyseSentencesInParagraphs(unmatched_paragraphs_curr, unmatched_paragraphs_prev, revision_curr, revision_prev, relation)
#TODO: SPAM detection
if (len(unmatched_paragraphs_curr)/float(len(revision_curr.ordered_paragraphs)) > UNMATCHED_PARAGRAPH):
possible_vandalism = True
# Analysis of words in unmatched sentences (diff of both texts).
if (len(unmatched_sentences_curr)>0):
(matched_words_prev, vandalism) = analyseWordsInSentences(unmatched_sentences_curr, unmatched_sentences_prev, revision_curr, possible_vandalism, relation)
if (len(unmatched_paragraphs_curr) == 0):
for paragraph in unmatched_paragraphs_prev:
for sentence_key in paragraph.sentences.keys():
for sentence in paragraph.sentences[sentence_key]:
if not(sentence.matched):
unmatched_sentences_prev.append(sentence)
# Add the information of 'deletion' to words
for unmatched_sentence in unmatched_sentences_prev:
#if (revision_curr.wikipedia_id == 74544182):
#print "unmatched sentence", unmatched_sentence.value, revision_curr.wikipedia_id
for word_prev in unmatched_sentence.words:
if not(word_prev.matched):
for elem in word_prev.deleted:
if (elem != revision_curr.wikipedia_id) and (elem in revisions.keys()):
if (revisions[elem].contributor_name != revision_curr.contributor_name):
if (elem in relation.redeleted.keys()):
relation.redeleted.update({elem : relation.redeleted[elem] + 1})
else:
relation.redeleted.update({elem : 1})
else:
if (elem in relation.self_redeleted.keys()):
relation.self_redeleted.update({elem : relation.self_redeleted[elem] + 1})
else:
relation.self_redeleted.update({elem : 1})
# Revert: deleting something that somebody else reintroduced.
for elem in word_prev.freq:
#if (revision_curr.wikipedia_id == 11):
# print "Revert in 11", word_prev.value, word_prev.deleted, relation.revert
if (elem != revision_curr.wikipedia_id) and (elem in revisions.keys()):
if (revisions[elem].contributor_name != revision_curr.contributor_name):
if (elem in relation.revert.keys()):
relation.revert.update({elem: relation.revert[elem] +1})
else:
relation.revert.update({elem: 1})
else:
if (elem in relation.self_revert.keys()):
relation.self_revert.update({elem: relation.self_revert[elem] +1})
else:
relation.self_revert.update({elem: 1})
#print "relation.revert", word_prev.value, word_prev.deleted, relation.revert, revision_curr.wikipedia_id
word_prev.deleted.append(revision_curr.wikipedia_id)
#if (revision_curr.wikipedia_id == 74544182):
# print word_prev.value, word_prev.revision, revisions[word_prev.revision].contributor_name, revision_curr.contributor_name
if (revisions[word_prev.revision].contributor_name != revision_curr.contributor_name):
if (word_prev.revision in relation.deleted.keys()):
relation.deleted.update({word_prev.revision : relation.deleted[word_prev.revision] + 1 })
else:
relation.deleted.update({word_prev.revision : 1 })
else:
if (word_prev.revision in relation.self_deleted.keys()):
relation.self_deleted.update({word_prev.revision : relation.self_deleted[word_prev.revision] + 1 })
else:
relation.self_deleted.update({word_prev.revision : 1 })
# Reset matched structures from old revisions.
for matched_paragraph in matched_paragraphs_prev:
matched_paragraph.matched = False
for sentence_hash in matched_paragraph.sentences.keys():
for sentence in matched_paragraph.sentences[sentence_hash]:
sentence.matched = False
for word in sentence.words:
word.matched = False
for matched_sentence in matched_sentences_prev:
matched_sentence.matched = False
for word in matched_sentence.words:
word.matched = False
for matched_word in matched_words_prev:
matched_word.matched = False
if (not vandalism):
# Add the new paragraphs to hash table of paragraphs.
for unmatched_paragraph in unmatched_paragraphs_curr:
if (unmatched_paragraph.hash_value in paragraphs_ht.keys()):
paragraphs_ht[unmatched_paragraph.hash_value].append(unmatched_paragraph)
else:
paragraphs_ht.update({unmatched_paragraph.hash_value : [unmatched_paragraph]})
# Add the new sentences to hash table of sentences.
for unmatched_sentence in unmatched_sentences_curr:
if (unmatched_sentence.hash_value in sentences_ht.keys()):
sentences_ht[unmatched_sentence.hash_value].append(unmatched_sentence)
else:
sentences_ht.update({unmatched_sentence.hash_value : [unmatched_sentence]})
return vandalism
def analyseParagraphsInRevision(revision_curr, revision_prev, text_curr, relation):
# Containers for unmatched and matched paragraphs.
unmatched_paragraphs_curr = []
unmatched_paragraphs_prev = []
matched_paragraphs_prev = []
# Split the text of the current into paragraphs.
paragraphs = Text.splitIntoParagraphs(text_curr)
# Iterate over the paragraphs of the current version.
for paragraph in paragraphs:
# Build Paragraph structure and calculate hash value.
paragraph = paragraph.strip()
hash_curr = Text.calculateHash(paragraph)
matched_curr = False
# If the paragraph is in the previous revision,
# update the authorship information and mark both paragraphs as matched (also in HT).
if (hash_curr in revision_prev.ordered_paragraphs):
for paragraph_prev in revision_prev.paragraphs[hash_curr]:
if (not paragraph_prev.matched):
matched_curr = True
paragraph_prev.matched = True
matched_paragraphs_prev.append(paragraph_prev)
# TODO: added this (CHECK).
for hash_sentence_prev in paragraph_prev.sentences.keys():
for sentence_prev in paragraph_prev.sentences[hash_sentence_prev]:
sentence_prev.matched = True
for word_prev in sentence_prev.words:
word_prev.matched = True
word_prev.used.append(revision_curr.wikipedia_id)
#if (word_prev.revision in relation.reintroduced.keys()):
# relation.reintroduced.update({word_prev.revision : relation.reintroduced[word_prev.revision] + 1 })
#else:
# relation.reintroduced.update({word_prev.revision : 1 })
# Add paragraph to current revision.
if (hash_curr in revision_curr.paragraphs.keys()):
revision_curr.paragraphs[paragraph_prev.hash_value].append(paragraph_prev)
revision_curr.ordered_paragraphs.append(paragraph_prev.hash_value)
else:
revision_curr.paragraphs.update({paragraph_prev.hash_value : [paragraph_prev]})
revision_curr.ordered_paragraphs.append(paragraph_prev.hash_value)
break
# If the paragraph is not in the previous revision, but it is in an older revision
# update the authorship information and mark both paragraphs as matched.
if ((not matched_curr) and (hash_curr in paragraphs_ht)):
for paragraph_prev in paragraphs_ht[hash_curr]:
if (not paragraph_prev.matched):
matched_curr = True
paragraph_prev.matched = True
matched_paragraphs_prev.append(paragraph_prev)
# TODO: added this (CHECK).
for hash_sentence_prev in paragraph_prev.sentences.keys():
for sentence_prev in paragraph_prev.sentences[hash_sentence_prev]:
sentence_prev.matched = True
for word_prev in sentence_prev.words:
word_prev.matched = True
word_prev.used.append(revision_curr.wikipedia_id)
if (revision_prev.wikipedia_id not in word_prev.used):
word_prev.freq.append(revision_curr.wikipedia_id)
# Revert: reintroducing something that somebody else deleted,
# (and was not used in the previous revision)
if (revision_prev.wikipedia_id not in word_prev.used):
#if (revision_curr.wikipedia_id == 11):
# print "Revert in 11", word_prev.value, word_prev.deleted, relation.revert
for elem in word_prev.deleted:
if (elem in revisions.keys()):
if (revisions[elem].contributor_name != revision_curr.contributor_name):
if (elem in relation.revert.keys()):
relation.revert.update({elem : relation.revert[elem] + 1})
else:
relation.revert.update({elem : 1})
else:
if (elem in relation.self_revert.keys()):
relation.self_revert.update({elem : relation.self_revert[elem] + 1})
else:
relation.self_revert.update({elem : 1})
if (revision_prev.wikipedia_id not in word_prev.used):
if (elem in revisions.keys()):
if (revisions[word_prev.revision].contributor_name != revision_curr.contributor_name):
if (word_prev.revision in relation.reintroduced.keys()):
relation.reintroduced.update({word_prev.revision : relation.reintroduced[word_prev.revision] + 1 })
else:
relation.reintroduced.update({word_prev.revision : 1 })
else:
if (word_prev.revision in relation.self_reintroduced.keys()):
relation.self_reintroduced.update({word_prev.revision : relation.self_reintroduced[word_prev.revision] + 1})
else:
relation.self_reintroduced.update({word_prev.revision : 1})
# Add paragraph to current revision.
if (hash_curr in revision_curr.paragraphs.keys()):
revision_curr.paragraphs[paragraph_prev.hash_value].append(paragraph_prev)
revision_curr.ordered_paragraphs.append(paragraph_prev.hash_value)
else:
revision_curr.paragraphs.update({paragraph_prev.hash_value : [paragraph_prev]})
revision_curr.ordered_paragraphs.append(paragraph_prev.hash_value)
break
# If the paragraph did not match with previous revisions,
# add to container of unmatched paragraphs for further analysis.
if (not matched_curr):
paragraph_curr = Paragraph()
paragraph_curr.hash_value = Text.calculateHash(paragraph)
paragraph_curr.value = paragraph
revision_curr.ordered_paragraphs.append(paragraph_curr.hash_value)
if (paragraph_curr.hash_value in revision_curr.paragraphs.keys()):
revision_curr.paragraphs[paragraph_curr.hash_value].append(paragraph_curr)
else:
revision_curr.paragraphs.update({paragraph_curr.hash_value : [paragraph_curr]})
unmatched_paragraphs_curr.append(paragraph_curr)
# Identify unmatched paragraphs in previous revision for further analysis.
for paragraph_prev_hash in revision_prev.ordered_paragraphs:
for paragraph_prev in revision_prev.paragraphs[paragraph_prev_hash]:
if (not paragraph_prev.matched):
unmatched_paragraphs_prev.append(paragraph_prev)
return (unmatched_paragraphs_curr, unmatched_paragraphs_prev, matched_paragraphs_prev)
def analyseSentencesInParagraphs(unmatched_paragraphs_curr, unmatched_paragraphs_prev, revision_curr, revision_prev, relation):
# Containers for unmatched and matched sentences.
unmatched_sentences_curr = []
unmatched_sentences_prev = []
matched_sentences_prev = []
total_sentences = 0
# Iterate over the unmatched paragraphs of the current revision.
for paragraph_curr in unmatched_paragraphs_curr:
# Split the current paragraph into sentences.
sentences = Text.splitIntoSentences(paragraph_curr.value)
# Iterate over the sentences of the current paragraph
for sentence in sentences:
# Create the Sentence structure.
sentence = sentence.strip()
sentence = ' '.join(Text.splitIntoWords(sentence))
hash_curr = Text.calculateHash(sentence)
matched_curr = False
total_sentences = total_sentences + 1
# Iterate over the unmatched paragraphs from the previous revision.
for paragraph_prev in unmatched_paragraphs_prev:
if (hash_curr in paragraph_prev.sentences.keys()):
for sentence_prev in paragraph_prev.sentences[hash_curr]:
if (not sentence_prev.matched):
matched_one = False
matched_all = True
for word_prev in sentence_prev.words:
if (word_prev.matched):
matched_one = True
else:
matched_all = False
if not(matched_one):
sentence_prev.matched = True
matched_curr = True
matched_sentences_prev.append(sentence_prev)
# TODO: CHECK this
for word_prev in sentence_prev.words:
word_prev.matched = True
word_prev.used.append(revision_curr.wikipedia_id)
#if (word_prev.revision in relation.reintroduced.keys()):
# relation.reintroduced.update({word_prev.revision : relation.reintroduced[word_prev.revision] + 1 })
#else:
# relation.reintroduced.update({word_prev.revision : 1 })
# Add the sentence information to the paragraph.
if (hash_curr in paragraph_curr.sentences.keys()):
paragraph_curr.sentences[hash_curr].append(sentence_prev)
paragraph_curr.ordered_sentences.append(sentence_prev.hash_value)
else:
paragraph_curr.sentences.update({sentence_prev.hash_value : [sentence_prev]})
paragraph_curr.ordered_sentences.append(sentence_prev.hash_value)
break
elif (matched_all):
sentence_prev.matched = True
matched_sentences_prev.append(sentence_prev)
if (matched_curr):
break
# Iterate over the hash table of sentences from old revisions.
if ((not matched_curr) and (hash_curr in sentences_ht.keys())):
for sentence_prev in sentences_ht[hash_curr]:
if (not sentence_prev.matched):
matched_one = False
matched_all = True
for word_prev in sentence_prev.words:
if (word_prev.matched):
matched_one = True
else:
matched_all = False
if not(matched_one):
sentence_prev.matched = True
matched_curr = True
matched_sentences_prev.append(sentence_prev)
# TODO: CHECK this
for word_prev in sentence_prev.words:
word_prev.matched = True
word_prev.used.append(revision_curr.wikipedia_id)
if (revision_prev.wikipedia_id not in word_prev.used):
word_prev.freq.append(revision_curr.wikipedia_id)
# Revert: reintroducing something that somebody else deleted
if (revision_prev.wikipedia_id not in word_prev.used):
for elem in word_prev.deleted:
#if (revision_curr.wikipedia_id == 11):
# print "Revert in 11", word_prev.value, word_prev.deleted, relation.revert
if (elem in revisions.keys()):
if (revisions[elem].contributor_name != revision_curr.contributor_name):
if (elem in relation.revert.keys()):
relation.revert.update({elem : relation.revert[elem] + 1})
else:
relation.revert.update({elem : 1})
else:
if (elem in relation.self_revert.keys()):
relation.self_revert.update({elem : relation.self_revert[elem] + 1})
else:
relation.self_revert.update({elem : 1})
#print "relation.revert", word_prev.value, word_prev.deleted, relation.revert, revision_curr.wikipedia_id
if (revision_prev.wikipedia_id not in word_prev.used):
if (elem in revisions.keys()):
if (revisions[word_prev.revision].contributor_name != revision_curr.contributor_name):
if (word_prev.revision in relation.reintroduced.keys()):
relation.reintroduced.update({word_prev.revision : relation.reintroduced[word_prev.revision] + 1 })
else:
relation.reintroduced.update({word_prev.revision : 1 })
else:
if (word_prev.revision in relation.self_reintroduced.keys()):
relation.self_reintroduced.update({word_prev.revision : relation.self_reintroduced[word_prev.revision] + 1})
else:
relation.self_reintroduced.update({word_prev.revision : 1})
# Add the sentence information to the paragraph.
if (hash_curr in paragraph_curr.sentences.keys()):
paragraph_curr.sentences[hash_curr].append(sentence_prev)
paragraph_curr.ordered_sentences.append(sentence_prev.hash_value)
else:
paragraph_curr.sentences.update({sentence_prev.hash_value : [sentence_prev]})
paragraph_curr.ordered_sentences.append(sentence_prev.hash_value)
break
elif (matched_all):
sentence_prev.matched = True
matched_sentences_prev.append(sentence_prev)
# If the sentence did not match, then include in the container of unmatched sentences for further analysis.
if (not matched_curr):
sentence_curr = Sentence()
sentence_curr.value = sentence
sentence_curr.hash_value = hash_curr
paragraph_curr.ordered_sentences.append(sentence_curr.hash_value)
if (sentence_curr.hash_value in paragraph_curr.sentences.keys()):
paragraph_curr.sentences[sentence_curr.hash_value].append(sentence_curr)
else:
paragraph_curr.sentences.update({sentence_curr.hash_value : [sentence_curr]})
unmatched_sentences_curr.append(sentence_curr)
# Identify the unmatched sentences in the previous paragraph revision.
for paragraph_prev in unmatched_paragraphs_prev:
for sentence_prev_hash in paragraph_prev.ordered_sentences:
for sentence_prev in paragraph_prev.sentences[sentence_prev_hash]:
if (not sentence_prev.matched):
unmatched_sentences_prev.append(sentence_prev)
sentence_prev.matched = True
matched_sentences_prev.append(sentence_prev)
return (unmatched_sentences_curr, unmatched_sentences_prev, matched_sentences_prev, total_sentences)
def analyseWordsInSentences(unmatched_sentences_curr, unmatched_sentences_prev, revision_curr, possible_vandalism, relation):
matched_words_prev = []
unmatched_words_prev = []
global WORD_ID
# Split sentences into words.
text_prev = []
for sentence_prev in unmatched_sentences_prev:
for word_prev in sentence_prev.words:
if (not word_prev.matched):
text_prev.append(word_prev.value)
unmatched_words_prev.append(word_prev)
text_curr = []
for sentence_curr in unmatched_sentences_curr:
splitted = Text.splitIntoWords(sentence_curr.value)
text_curr.extend(splitted)
sentence_curr.splitted.extend(splitted)
# Edit consists of removing sentences, not adding new content.
if (len(text_curr) == 0):
return (matched_words_prev, False)
# SPAM detection.
if (possible_vandalism):
density = Text.computeAvgWordFreq(text_curr, revision_curr.wikipedia_id)
if (density > WORD_DENSITY):
return (matched_words_prev, possible_vandalism)
else:
possible_vandalism = False
if (len(text_prev) == 0):
for sentence_curr in unmatched_sentences_curr:
for word in sentence_curr.splitted:
word_curr = Word()
word_curr.internal_id = WORD_ID
word_curr.author_id = revision_curr.contributor_id
word_curr.author_name = revision_curr.contributor_name
word_curr.revision = revision_curr.wikipedia_id
word_curr.value = word
sentence_curr.words.append(word_curr)
word_curr.used.append(revision_curr.wikipedia_id)
relation.added = relation.added + 1
WORD_ID = WORD_ID + 1
return (matched_words_prev, possible_vandalism)
d = Differ()
diff = list(d.compare(text_prev, text_curr))
for sentence_curr in unmatched_sentences_curr:
for word in sentence_curr.splitted:
curr_matched = False
pos = 0
while (pos < len(diff)):
word_diff = diff[pos]
if (word == word_diff[2:]):
if (word_diff[0] == ' '):
for word_prev in unmatched_words_prev:
if ((not word_prev.matched) and (word_prev.value == word)):
word_prev.used.append(revision_curr.wikipedia_id)
word_prev.matched = True
curr_matched = True
sentence_curr.words.append(word_prev)
matched_words_prev.append(word_prev)
diff[pos] = ''
pos = len(diff)+1
#if (word_prev.revision in relation.reintroduced.keys()):
# relation.reintroduced.update({word_prev.revision : relation.reintroduced[word_prev.revision] + 1 })
#else:
# relation.reintroduced.update({word_prev.revision : 1 })
break
elif (word_diff[0] == '-'):
for word_prev in unmatched_words_prev:
if ((not word_prev.matched) and (word_prev.value == word)):
word_prev.matched = True
matched_words_prev.append(word_prev)
diff[pos] = ''
word_prev.deleted.append(revision_curr.wikipedia_id)
if (revisions[word_prev.revision].contributor_name != revision_curr.contributor_name):
if (word_prev.revision in relation.deleted.keys()):
relation.deleted.update({word_prev.revision : relation.deleted[word_prev.revision] + 1 })
else:
relation.deleted.update({word_prev.revision : 1 })
else:
if (word_prev.revision in relation.self_deleted.keys()):
relation.self_deleted.update({word_prev.revision : relation.self_deleted[word_prev.revision] + 1 })
else:
relation.self_deleted.update({word_prev.revision : 1 })
break
elif (word_diff[0] == '+'):
curr_matched = True
word_curr = Word()
word_curr.internal_id = WORD_ID
word_curr.value = word
word_curr.author_id = revision_curr.contributor_id
word_curr.author_name = revision_curr.contributor_name
word_curr.revision = revision_curr.wikipedia_id
word_curr.used.append(revision_curr.wikipedia_id)
sentence_curr.words.append(word_curr)
relation.added = relation.added + 1
WORD_ID = WORD_ID + 1
diff[pos] = ''
pos = len(diff)+1
pos = pos + 1
if not(curr_matched):
word_curr = Word()
word_curr.internal_id = WORD_ID
word_curr.value = word
word_curr.author_id = revision_curr.contributor_id
word_curr.author_name = revision_curr.contributor_name
word_curr.revision = revision_curr.wikipedia_id
word_curr.used.append(revision_curr.wikipedia_id)
sentence_curr.words.append(word_curr)
relation.added = relation.added + 1
WORD_ID = WORD_ID + 1
return (matched_words_prev, possible_vandalism)
def printAllRevisions(order, revisions):
for (revision, vandalism) in order:
if not(vandalism):
printRevision(revisions[revision])
def printRevision(revision):
print "Printing authorhship for revision: ", revision.wikipedia_id
text = []
authors = []
for hash_paragraph in revision.ordered_paragraphs:
#print hash_paragraph
#text = ''
p_copy = deepcopy(revision.paragraphs[hash_paragraph])
paragraph = p_copy.pop(0)
#print paragraph.value
#print len(paragraph.sentences)
for hash_sentence in paragraph.ordered_sentences:
#print hash_sentence
sentence = paragraph.sentences[hash_sentence].pop(0)
#print sentence.words
for word in sentence.words:
#print word
#text = text + ' ' + unicode(word.value,'utf-8') + "@@" + str(word.revision)
text.append(word.value)
authors.append(word.revision)
print text
print authors
def printRevisionTrackAppearance(revision):
print "Printing authorship for revision: ", revision.wikipedia_id
text = []
authors = []
for hash_paragraph in revision.ordered_paragraphs:
#print hash_paragraph
#text = ''
p_copy = deepcopy(revision.paragraphs[hash_paragraph])
paragraph = p_copy.pop(0)
#print paragraph.value
#print len(paragraph.sentences)
for hash_sentence in paragraph.ordered_sentences:
#print hash_sentence
sentence = paragraph.sentences[hash_sentence].pop(0)
#print sentence.words
for word in sentence.words:
appeared = copy(word.used)
disappeared = copy(word.deleted)
changes = []
changes.append("+(" + str(appeared.pop(0))+")")
while len(disappeared) > 0:
d = disappeared.pop(0)
if (d > revision.wikipedia_id):
break
changes.append("-(" + str(d)+")")
while len(appeared) > 0:
a = appeared.pop(0)
if (a > d):
changes.append("+(" + str(a)+")")
break
#print word.used
#print word.deleted
print unicode(word.value,'utf-8') + "@@" + str(word.revision) + "@@" + str(changes)
text.append(word.value)
authors.append(word.revision)
#print text
#print authors
def printRelationships(relations, order):
print "Printing relationships"
header = ["revision", "author", "deleted(-)", "revert(-)", "reintroduced(+)", "redeleted(+)", "added", "total", "self-deleted", "self-revert", "self-reintroduced", "self-redeleted"]
print "\t".join(header)
for (revision, vandalism) in order:
if (vandalism):
continue
relation = relations[revision]
#print relation.author
print str(relation.revision) + "\t" + (relation.author).decode("utf-8") + "\t" + str(relation.deleted) + "\t" + str(relation.revert) + "\t" + str(relation.reintroduced) + "\t" + str(relation.redeleted) + "\t" + str(relation.added) + "\t" + str(relation.total_tokens) + "\t" + str(relation.self_deleted) + "\t" + str(relation.self_revert) + "\t" + str(relation.self_reintroduced) + "\t" + str(relation.self_redeleted)
#def printJSON(relations, order):
#
# deleted_values = {}
# revert_values = {}
# reintroduced_values = {}
# redeleted_values = {}
#
# for (revision, vandalism) in order:
# if (vandalism):
# continue
# relation = relations[revision]
# print str(relation.revision) + "\t" + str(relation.author) + "\t" + str(relation.deleted) + "\t" + str(relation.revert) + "\t" + str(relation.reintroduced) + "\t" + str(relation.redeleted) + "\t" + str(relation.added) + "\t" + str(relation.total_tokens)
def printRevisionJSON(revision):
tokens = []
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:
i = 0
while i < len(word.deleted):
if word.deleted[i] > revision.wikipedia_id:
break
i = i+1
j = 0
while j < len(word.freq):
if word.freq[j] > revision.wikipedia_id:
break
j = j +1
tokens.append(str({"token": word.value, "author_name": word.author_name.encode("utf-8"), "rev_id": str(word.revision), "disappering": word.deleted[0:i], "incoming": word.freq[0:j]}))
print tokens #cjson.encode({"tokens" : tokens})
def main(my_argv):
inputfile = ''
revision = None
output = ''
if (len(my_argv) <= 3):
try:
opts, _ = getopt.getopt(my_argv,"i:",["ifile="])
except getopt.GetoptError:
print 'Usage: Wikiwho.py -i <inputfile> -o <output> [-rev <revision_id>]'
exit(2)
else:
try:
opts, _ = getopt.getopt(my_argv,"i:o:r:",["ifile=","revision=", "output="])
except getopt.GetoptError:
print 'Usage: Wikiwho.py -i <inputfile> -o output [-rev <revision_id>]'
exit(2)
for opt, arg in opts:
if opt in ('-h', "--help"):
print "WikiWho: An algorithm for detecting attribution of authorship in revisioned content"
print
print 'Usage: Wikiwho.py -i <inputfile> [-rev <revision_id>]'
print "-i --ifile File to analyze"
print "-o --type of output: <a> for authorship, <r> for relations"
print "-r --revision Revision to analyse. If not specified, the last revision is printed."
print "-h --help This help."
exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-r", "--revision"):
revision = arg
elif opt in ("-o", "--output"):
output = arg
return (inputfile,revision,output)
if __name__ == '__main__':
(file_name, revision, output) = main(argv[1:])
#print "Calculating authorship for:", file_name
time1 = time()
(revisions, order, relations) = analyseArticle(file_name)
time2 = time()
#pos = file_name.rfind("/")
#print file_name[pos+1: len(file_name)-len(".xml")], time2-time1
if (output == 'r'):
printRelationships(relations, order)
if (output == 'a'):
print "revision", revision
if (revision == 'all'):
printAllRevisions(order, revisions)
else:
printRevisionJSON(revisions[int(revision)])
#print "Execution time:", time2-time1