-
Notifications
You must be signed in to change notification settings - Fork 0
/
rumourObj.py
969 lines (729 loc) · 37.4 KB
/
rumourObj.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
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
import torch
# a general model to load up all our models
from transformers import AutoModel, AutoModelForSequenceClassification
class GeneralModel:
def __init__(self, model_name='bert-base-uncased', tokenizer=None, device='cuda:0', load_pretrained_model=False, pretrained_model=None):
self.load_pretrained_model = load_pretrained_model
self.pretrained_model = pretrained_model
self.tokenizer = tokenizer
self.device = device
if not self.load_pretrained_model:
# Load model and model for sequence classification with Auto classes
self.base_model = AutoModel.from_pretrained(model_name, output_hidden_states=True)
self.model = AutoModelForSequenceClassification.from_pretrained(model_name)
# Resize token embeddings if a tokenizer is provided
if tokenizer is not None:
self.model.resize_token_embeddings(len(tokenizer))
self.base_model.resize_token_embeddings(len(tokenizer))
# Set padding index if applicable (specific to models with word embeddings like BERT and RoBERTa)
if hasattr(self.model, 'embeddings') and hasattr(self.model.embeddings, 'word_embeddings'):
self.model.embeddings.word_embeddings.padding_idx = 1
self.base_model.embeddings.word_embeddings.padding_idx = 1
self.model.eval()
self.model.to(self.device)
self.base_model.to(self.device)
else:
# Load pretrained model with specified configuration
self.base_model = AutoModel.from_pretrained(pretrained_model, output_hidden_states=True)
self.model = AutoModelForSequenceClassification.from_pretrained(pretrained_model, output_hidden_states=True)
if tokenizer is not None:
self.model.resize_token_embeddings(len(tokenizer))
self.base_model.resize_token_embeddings(len(tokenizer))
if hasattr(self.model, 'embeddings') and hasattr(self.model.embeddings, 'word_embeddings'):
self.model.embeddings.word_embeddings.padding_idx = 1
self.base_model.embeddings.word_embeddings.padding_idx = 1
self.model.eval()
self.model.to(self.device)
self.base_model.to(self.device)
class Tweet(object):
def __init__(self, source_id, source_text, reply_text_lst, candidate_comments, is_rumour):
self.source_id = source_id
self.source_text = source_text
self.reply_text_lst = reply_text_lst
self.candidate_comments = candidate_comments
self.is_rumour = is_rumour
#listofTweets = [(Tweet(row.source_id,row.source_text, row.reply_text_lst, row.is_rumour)) for index, row in df.iterrows() ]
class Story(object):
def __init__(self, story_id, story_content, source_text_lst, is_rumour, source_turnaround_lst):
self.story_id = story_id
self.story_content = story_content
self.source_text_lst = source_text_lst
self.is_rumour = is_rumour #[0,1,2]
self.source_turnaround_lst = source_turnaround_lst
class TweetLite(object):
def __init__(self, source_id, source_text, reply_text_lst, is_rumour):
self.source_id = source_id
self.source_text = source_text
self.reply_text_lst = reply_text_lst
self.is_rumour = is_rumour
def sep_token_help(input_ids, sepecial_token_id):
#input_ids = input_ids.tolist()
size = len(input_ids)
#print('here is the size ', size)
#input_ids = input_ids.tolist()
#print('input_ids ',input_ids)
#print('special_token_id in list ', sepecial_token_id in input_ids)
idx_list = [idx + 1 for idx, val in enumerate(input_ids) if val == sepecial_token_id]
#print('len of idx_list ',len(idx_list))
res = [input_ids[i:j] for i, j in zip([0] + idx_list, idx_list + ([size] if idx_list[-1] != size else []))]
return res
def convert_to_sublists(input_ids, sepecial_token_id):
#print('converter ',len(input_ids))
start = 0
idx_list = [idx + 1 for idx, val in enumerate(input_ids) if val == sepecial_token_id]
sublists = []
#print('IM in converter ', len(idx_list))
for i, item in enumerate(input_ids):
if i in idx_list:
end = i
sublist = list(range(start, end+1))
sublists.append(sublist)
start = end + 1
return sublists
def encode_mask_sentence(mask_tok, sentence, tokenizer,max_len):
sentence_word_lst = sentence.split()
sentence_len = len(sentence_word_lst)
masked_sentence_lst = [mask_tok] * sentence_len
masked_tok = tokenizer.encode_plus(masked_sentence_lst, is_split_into_words=True,add_special_tokens=True, max_length=max_len, truncation_strategy='only_second',padding='max_length', pad_to_max_length=True)
return masked_tok
def gen_mask_sentence(mask_tok, sentence):
sentence_word_lst = sentence.split()
sentence_len = len(sentence_word_lst)
masked_sentence_lst = [mask_tok] * sentence_len
masked_sentence_str = ''.join(masked_sentence_lst)
return masked_sentence_lst, masked_sentence_str
class PairIntervention():
def __init__(self, tokennizer, source_sentence: str, reaction_lst: list, alt_loc, gold_label, turnaround_label_lst: list, max_len, device):
super()
self.device = device
self.enc = tokennizer
self.custom_tokens = ["[CMT]","[MASK_SENT]"]
self.max_len = max_len
self.alt_loc = alt_loc
self.enc.add_special_tokens({"additional_special_tokens": self.custom_tokens})
source_tok = self.enc.encode_plus(text=source_sentence,
add_special_tokens = True, # Add '[CLS]' and '[SEP]'
max_length = self.max_len, # Pad & truncate all sentences.
truncation_strategy = 'only_second',
padding = 'max_length',
pad_to_max_length = True) # Return pytorch tensors.
source_tok_input_ids = source_tok['input_ids']
source_tok_attention_masks = source_tok['attention_mask']
reaction_tok_input_ids = []
reaction_tok_attention_mask = []
tokenized_reactions = [self.enc.encode_plus(text, add_special_tokens=True, max_length=self.max_len, truncation_strategy='only_second',padding='max_length', pad_to_max_length=True) for text in reaction_lst]
#debug
#print('len of tokenized_rections ', len(tokenized_reactions))
for t_r in tokenized_reactions:
reaction_tok_input_ids.append(t_r['input_ids'])
reaction_tok_attention_mask.append(t_r['attention_mask'])
#altered_tokenized_reactions = tokenized_reactions.copy()
masked_tok = encode_mask_sentence(self.custom_tokens[0], reaction_lst[alt_loc], self.enc, self.max_len)
altered_input_ids = []
altered_attention_mask = []
for index, val in enumerate(tokenized_reactions):
if index == alt_loc:
altered_input_ids.append(masked_tok['input_ids'])
altered_attention_mask.append(masked_tok['attention_mask'])
else:
altered_input_ids.append(val['input_ids'])
altered_attention_mask.append(val['attention_mask'])
#print('len of altered_tokenized_reactions ',len(altered_tokenized_reactions))
self.source_input_ids = torch.tensor(source_tok_input_ids).to(device)
self.source_attention_masks = torch.tensor(source_tok_attention_masks).to(device)
#print('self.source_input_ids ', self.source_input_ids.shape)
self.reaction_tok_input_ids = torch.tensor(reaction_tok_input_ids).to(device)
self.reaction_tok_attention_mask = torch.tensor(reaction_tok_attention_mask).to(device)
#print('self.reaction_tok_input_ids ', self.reaction_tok_input_ids.shape)
self.altered_reaction_tok_input_ids = torch.tensor(altered_input_ids).to(device)
self.altered_reaction_tok_attention_mask = torch.tensor(altered_attention_mask).to(device)
#self.rumour_label = gold_label
self.label_tok = torch.tensor(gold_label,dtype=torch.int8).to(device)
self.turnaround_lst = turnaround_label_lst
self.turnaround_tok = torch.tensor(self.turnaround_lst).to(device)
class TotalIntervention():
def __init__(self, tokennizer, source_sentence: str, reaction_lst: list, alt_loc, gold_label, turnaround_label_lst: list, max_len, device):
super()
self.device = device
self.enc = tokennizer
self.custom_tokens = ["[CMT]","[MASK_SENT]","[MASKTOK]"]
self.max_len = max_len
self.alt_loc = alt_loc
self.enc.add_special_tokens({"additional_special_tokens": self.custom_tokens})
reaction_lst_str = ' '.join(reaction_lst)
source_tok = self.enc.encode_plus(text=source_sentence,
text_pair=reaction_lst_str,
add_special_tokens = True, # Add '[CLS]' and '[SEP]'
max_length = self.max_len, # Pad & truncate all sentences.
truncation_strategy = 'only_second',
padding = 'max_length',
pad_to_max_length = True) # Return pytorch tensors.
source_tok_input_ids = source_tok['input_ids']
source_tok_attention_masks = source_tok['attention_mask']
altered_reaction_lst = []
position_lst = []
for index, value in enumerate(reaction_lst):
if index == alt_loc:
masked_lst, masked_ss = gen_mask_sentence(self.custom_tokens[0], value)
altered_reaction_lst.append(masked_ss)
position_lst.extend(len(masked_lst)*[1])
else:
altered_reaction_lst.append(value)
value_lst = value.split()
position_lst.extend(len(value_lst)*[0])
altered_indices = [i for i, pos in enumerate(position_lst) if pos == 1]
self.target_locations = altered_indices
altered_reaction_str = ' '.join(altered_reaction_lst)
altered_tok = self.enc.encode_plus(
text = source_sentence,
text_pair = altered_reaction_str,
add_special_tokens = True,
max_length = self.max_len,
truncation_strategy = 'only_second',
padding = 'max_length',
pad_to_max_length=True)
altered_tok_input_ids = altered_tok['input_ids']
altered_tok_attention_mask = altered_tok['attention_mask']
self.source_input_ids = torch.tensor(source_tok_input_ids).to(device)
self.source_attention_masks = torch.tensor(source_tok_attention_masks).to(device)
self.altered_input_ids = torch.tensor(altered_tok_input_ids).to(device)
self.altered_attention_mask = torch.tensor(altered_tok_attention_mask).to(device)
#self.rumour_label = gold_label
self.label_tok = torch.tensor(gold_label,dtype=torch.int8).to(device)
self.turnaround_lst = turnaround_label_lst
self.turnaround_tok = torch.tensor(self.turnaround_lst).to(device)
class TokenIntervention():
def __init__(self, tokennizer, source_sentence: str, reaction_sentence: str, alt_loc, gold_label, turnaround_label_lst: list, max_len, device):
super()
self.device = device
self.enc = tokennizer
self.custom_tokens = ["[CMT]","[MASK_SENT]","[MASKTOK]"]
self.max_len = max_len
self.alt_loc = alt_loc
self.enc.add_special_tokens({"additional_special_tokens": self.custom_tokens})
source_tok = self.enc.encode_plus(text=source_sentence,
text_pair=reaction_sentence,
add_special_tokens = True, # Add '[CLS]' and '[SEP]'
max_length = self.max_len, # Pad & truncate all sentences.
truncation_strategy = 'only_second',
padding = 'max_length',
pad_to_max_length = True) # Return pytorch tensors.
source_tok_input_ids = source_tok['input_ids']
source_tok_attention_masks = source_tok['attention_mask']
reaction_sentence_lst = reaction_sentence.split()
reaction_sentence_lst[alt_loc] = self.custom_tokens[2]
altered_reaction_sentence = ' '.join(reaction_sentence_lst)
self.target_locations = [alt_loc]
altered_tok = self.enc.encode_plus(
text = source_sentence,
text_pair = altered_reaction_sentence,
add_special_tokens = True,
max_length = self.max_len,
truncation_strategy = 'only_second',
padding = 'max_length',
pad_to_max_length=True)
altered_tok_input_ids = altered_tok['input_ids']
altered_tok_attention_mask = altered_tok['attention_mask']
self.source_input_ids = torch.tensor(source_tok_input_ids).to(device)
self.source_attention_masks = torch.tensor(source_tok_attention_masks).to(device)
self.altered_input_ids = torch.tensor(altered_tok_input_ids).to(device)
self.altered_attention_mask = torch.tensor(altered_tok_attention_mask).to(device)
#self.rumour_label = gold_label
self.label_tok = torch.tensor(gold_label,dtype=torch.int8).to(device)
self.turnaround_lst = turnaround_label_lst
self.turnaround_tok = torch.tensor(self.turnaround_lst).to(device)
class PairInterventionV2():
def __init__(self, tokennizer, source_sentence: str, reaction_lst: list, alt_loc, gold_label, turnaround_label_lst: list, max_len, device):
super()
self.device = device
self.enc = tokennizer
self.custom_tokens = ["[CMT]","[MASK_SENT]","[MASKTOK]"]
self.max_len = max_len
self.alt_loc = alt_loc
self.enc.add_special_tokens({"additional_special_tokens": self.custom_tokens})
reaction_lst_str = ' '.join(reaction_lst)
source_tok = self.enc.encode_plus(text=source_sentence,
text_pair=reaction_lst_str,
add_special_tokens = True, # Add '[CLS]' and '[SEP]'
max_length = self.max_len, # Pad & truncate all sentences.
truncation_strategy = 'only_second',
padding = 'max_length',
pad_to_max_length = True) # Return pytorch tensors.
source_tok_input_ids = source_tok['input_ids']
source_tok_attention_masks = source_tok['attention_mask']
altered_reaction_lst = []
marked_lst = []
for index, value in enumerate(reaction_lst):
if index == alt_loc:
masked_lst, masked_ss = gen_mask_sentence(self.custom_tokens[0], value)
altered_reaction_lst.append(masked_ss)
masked_lst.append(len(masked_lst)*[1])
else:
altered_reaction_lst.append(value)
value_lst = value.split()
masked_lst.append(len(value_lst)*[0])
altered_indices = [i for i, pos in enumerate(masked_lst) if pos == 1]
self.target_locations = altered_indices
altered_reaction_str = ' '.join(altered_reaction_lst)
altered_tok = self.enc.encode_plus(
text = source_sentence,
text_pair = altered_reaction_str,
add_special_tokens = True,
max_length = self.max_len,
truncation_strategy = 'only_second',
padding = 'max_length',
pad_to_max_length=True)
altered_tok_input_ids = altered_tok['input_ids']
altered_tok_attention_mask = altered_tok['attention_mask']
self.source_input_ids = torch.tensor(source_tok_input_ids).to(device)
self.source_attention_masks = torch.tensor(source_tok_attention_masks).to(device)
self.altered_input_ids = torch.tensor(altered_tok_input_ids).to(device)
self.altered_attention_mask = torch.tensor(altered_tok_attention_mask).to(device)
#self.rumour_label = gold_label
self.label_tok = torch.tensor(gold_label,dtype=torch.int8).to(device)
self.turnaround_lst = turnaround_label_lst
self.turnaround_tok = torch.tensor(self.turnaround_lst).to(device)
class NodeIntervention():
def __init__(self, tokenizer, parent_content: str, child_content: str, custom_tokens:list, alt_loc, is_turn_label, max_len, device):
super()
self.device = device
self.enc = tokenizer
self.custom_tokens = custom_tokens
self.max_len = max_len
self.enc.add_special_tokens({"additional_special_tokens": self.custom_tokens})
pair_tok = self.enc.encode_plus(text=parent_content,
text_pair= child_content, # Sentence to encode.
add_special_tokens = True, # Add '[CLS]' and '[SEP]'
max_length = max_len, # Pad & truncate all sentences.
truncation_strategy = 'only_second',
padding = 'max_length',
pad_to_max_length = True) # Return pytorch tensors.)
og_pair_input_ids = pair_tok['input_ids']
og_pair_attention_masks = pair_tok['attention_mask']
# Identify the boundary index
boundary_idx = pair_input_ids.index(self.enc.sep_token_id)
# Create a copy of the tokenized input
pair_input_ids_clone = og_pair_input_ids.clone()
if alt_loc == 'parent':
masked_tok = encode_mask_sentence(self.custom_tokens[0], parent_content, self.enc)
elif alt_loc == 'child':
masked_tok = encode_mask_sentence(self.custom_tokens[0], child_content, self.enc)
# Replace the tokens in the first part with [MASK] tokens
masked_input[1:boundary_idx] = self.enc.mask_token_id
# Update attention mask for [MASK] tokens
attention_mask[1:boundary_idx] = 1
self.og_pair_input_ids = torch.tensor(og_pair_input_ids).to(device)
self.og_pair_attention_masks = torch.tensor(og_pair_attention_masks).to(device)
if is_turn_label == True:
self.turn_label = 1
else:
self.turn_label = 0
self.turn_label_tok = torch.tensor(self.turn_label).to(device)
class GraphIntervention():
def __init__(self, tokennizer, story_content: str, reaction_lst: list, custom_tokens:list, alt_loc, gold_label,max_len, device):
super()
self.device = device
self.enc = tokennizer
self.custom_tokens = custom_tokens
self.max_len = max_len
self.enc.add_special_tokens({"additional_special_tokens": self.custom_tokens})
source_tok = self.enc.encode_plus(text=story_content,
add_special_tokens = True, # Add '[CLS]' and '[SEP]'
max_length = self.max_len, # Pad & truncate all sentences.
truncation_strategy = 'only_second',
padding = 'max_length',
pad_to_max_length = True) # Return pytorch tensors.
source_tok_input_ids = source_tok['input_ids']
source_tok_attention_masks = source_tok['attention_mask']
reaction_tok_input_ids = []
reaction_tok_attention_mask = []
tokenized_reactions = [self.enc.encode_plus(text, add_special_tokens=True, max_length=self.max_len, truncation_strategy='only_second',padding='max_length', pad_to_max_length=True) for text in reaction_list]
for t_r in tokenizer_reactions:
reaction_tok_input_ids.append(t_r['input_ids'])
reaction_tok_attention_mask.append(t_r['attention_mask'])
altered_tokenized_reactions = tokenizer_reactions
masked_tok = encode_mask_sentence(self.custom_tokens[0], reaction_lst[alt_loc], self.enc)
altered_tokenized_reactions[alt_loc] = masked_tok
altered_reaction_tok_input_ids = []
altered_reaction_tok_attention_mask = []
for a_r in altered_tokenized_reactions:
altered_reaction_tok_input_ids.append(a_r['input_id'])
altered_reaction_tok_attention_mask.append(a_r['attention_mask'])
self.source_input_ids = torch.tensor(source_tok_input_ids).to(device)
self.source_attention_masks = torch.tensor(source_tok_attention_masks).to(device)
self.reaction_tok_input_ids = torch.tensor(reaction_tok_input_ids).to(device)
self.reaction_tok_attention_mask = torch.tensor(reaction_tok_attention_mask).to(device)
self.altered_reaction_tok_input_ids = torch.tensor(altered_reaction_tok_input_ids).to(device)
self.altered_reaction_tok_attention_mask = torch.tensor(altered_reaction_tok_attention_mask).to(device)
if gold_label == 'rumour':
self.label_tok = 1
else:
self.label_tok = 0
self.label_tok = torch.tensor(self.label_tok).to(device)
class StoryIntervention():
def __init__(self, tokennizer, story_content: str, reactions_lst: list, alt_loc, gold_label, device='cuda:0'):
super()
self.device = device
self.enc = tokennizer
custom_tokens = ["[MASK]","[CMT]"]
self.enc.add_tokens(["[MASK]","[CMT]"])
self.enc.add_special_tokens({"additional_special_tokens": [special_token]})
story_tok = self.enc.encode_plus(text=story_content,
add_special_tokens = True, # Add '[CLS]' and '[SEP]'
max_length = 512, # Pad & truncate all sentences.
truncation_strategy = 'only_second',
padding = 'max_length',
pad_to_max_length = True) # Return pytorch tensors.
altered_lst = og_comments
altered_lst[alt_loc] = '[MASK]'
if len(altered_lst) == 1:
altered_str = '[CMT]' + altered_lst[0]
else:
altered_str = '[CMT]'.join(altered_lst)
#print('len of og_comments ',len(og_comments))
#print('og_comments str ', og_comments_str)
#print('altered_str', altered_str)
altered_comments_tok = self.enc.encode_plus(text=source_string,
text_pair= altered_str, # Sentence to encode.
add_special_tokens = True, # Add '[CLS]' and '[SEP]'
max_length = 512, # Pad & truncate all sentences.
truncation_strategy = 'only_second',
padding = 'max_length',
pad_to_max_length = True) # Return pytorch tensors.)
og_comments_input_ids = og_comments_tok['input_ids']
og_comments_attention_masks = og_comments_tok['attention_mask']
altered_comments_input_ids = altered_comments_tok['input_ids']
altered_comments_attention_masks = altered_comments_tok['attention_mask']
custom_tokens_input_ids = self.enc.convert_tokens_to_ids(custom_tokens)
mask_token_id = custom_tokens_input_ids[0]
cmt_token_id = custom_tokens_input_ids[1]
#need to figure out the corresponding token positions
sep_og_comments_input_ids = sep_token_help(og_comments_input_ids, cmt_token_id)
sep_altered_comments_input_ids = sep_token_help(altered_comments_input_ids, cmt_token_id)
for item in sep_og_comments_input_ids:
if item not in sep_altered_comments_input_ids:
target_index = sep_og_comments_input_ids.index(item)
if not target_index:
raise ValueError('cannot catch that long [MASK] value')
sublists = convert_to_sublists(og_comments_input_ids, cmt_token_id)
#print('value of the sbulists ', sublists)
#print('target_index ', target_index)
#print('len of the sublists ',len(sublists))
target_loc = sublists[target_index-1]
self.target_locations = [t-1 for t in target_loc]
self.og_input_ids = torch.tensor(og_comments_input_ids).to(device)
self.og_attention_mask = torch.tensor(og_comments_attention_masks).to(device)
self.altered_input_ids = torch.tensor(altered_comments_input_ids).to(device)
self.altered_attention_mask = torch.tensor(altered_comments_attention_masks).to(device)
if gold_label == 'rumour':
self.label_tok = 1
else:
self.label_tok = 0
self.label_tok = torch.tensor(self.label_tok).to(device)
class RumourIntervention():
def __init__(self, tokennizer, source_string: str, og_comments: list, alt_loc, gold_label, device='cuda:0'):
super()
self.device = device
self.enc = tokennizer
custom_tokens = ["[MASK]","[CMT]"]
self.enc.add_tokens(["[MASK]","[CMT]"])
if len(og_comments) == 1:
og_comments_str = '[CMT]' + og_comments[0]
else:
og_comments_str = '[CMT]'.join(og_comments)
og_comments_tok = self.enc.encode_plus(text=source_string,
text_pair= og_comments_str, # Sentence to encode.
add_special_tokens = True, # Add '[CLS]' and '[SEP]'
max_length = 512, # Pad & truncate all sentences.
truncation_strategy = 'only_second',
padding = 'max_length',
pad_to_max_length = True) # Return pytorch tensors.
altered_lst = og_comments
altered_lst[alt_loc] = '[MASK]'
if len(altered_lst) == 1:
altered_str = '[CMT]' + altered_lst[0]
else:
altered_str = '[CMT]'.join(altered_lst)
#print('len of og_comments ',len(og_comments))
#print('og_comments str ', og_comments_str)
#print('altered_str', altered_str)
altered_comments_tok = self.enc.encode_plus(text=source_string,
text_pair= altered_str, # Sentence to encode.
add_special_tokens = True, # Add '[CLS]' and '[SEP]'
max_length = 512, # Pad & truncate all sentences.
truncation_strategy = 'only_second',
padding = 'max_length',
pad_to_max_length = True) # Return pytorch tensors.)
og_comments_input_ids = og_comments_tok['input_ids']
og_comments_attention_masks = og_comments_tok['attention_mask']
altered_comments_input_ids = altered_comments_tok['input_ids']
altered_comments_attention_masks = altered_comments_tok['attention_mask']
custom_tokens_input_ids = self.enc.convert_tokens_to_ids(custom_tokens)
mask_token_id = custom_tokens_input_ids[0]
cmt_token_id = custom_tokens_input_ids[1]
#need to figure out the corresponding token positions
sep_og_comments_input_ids = sep_token_help(og_comments_input_ids, cmt_token_id)
sep_altered_comments_input_ids = sep_token_help(altered_comments_input_ids, cmt_token_id)
##trial and error
'''
og_size = len(og_comments_input_ids)
og_idx_list = [idx + 1 for idx, val in enumerate(og_comments_input_ids) if val == cmt_token_id]
sep_og_comments_input_ids = [og_comments_input_ids[i:j] for i, j in zip([0] + og_idx_list, og_idx_list + ([og_size] if og_idx_list[-1] != og_size else []))]
altered_size = len(altered_comments_input_ids)
altered_idx_list = [idx + 1 for idx, val in enumerate(altered_comments_input_ids) if val == cmt_token_id]
sep_altered_comments_input_ids = [altered_comments_input_ids[i:j] for i, j in zip([0] + altered_idx_list, altered_idx_list + ([altered_size] if altered_idx_list[-1] != altered_size else []))]
'''
#print('sep_og_comments_input_ids len', len(sep_og_comments_input_ids))
#print('sep_og_comments_input_ids shape ', len(sep_altered_comments_input_ids))
#find the different index
for item in sep_og_comments_input_ids:
if item not in sep_altered_comments_input_ids:
target_index = sep_og_comments_input_ids.index(item)
if not target_index:
raise ValueError('cannot catch that long [MASK] value')
sublists = convert_to_sublists(og_comments_input_ids, cmt_token_id)
#print('value of the sbulists ', sublists)
#print('target_index ', target_index)
#print('len of the sublists ',len(sublists))
target_loc = sublists[target_index-1]
self.target_locations = [t-1 for t in target_loc]
self.og_input_ids = torch.tensor(og_comments_input_ids).to(device)
self.og_attention_mask = torch.tensor(og_comments_attention_masks).to(device)
self.altered_input_ids = torch.tensor(altered_comments_input_ids).to(device)
self.altered_attention_mask = torch.tensor(altered_comments_attention_masks).to(device)
if gold_label == 'rumour':
self.label_tok = 1
else:
self.label_tok = 0
self.label_tok = torch.tensor(self.label_tok).to(device)
class RumourTokenIntervention():
def __init__(self, tokennizer, source_string: str, og_comments: list, alt_loc,token_alt_loc, device='cuda'):
super()
self.device = device
self.enc = tokennizer
custom_tokens = ["[MASKTOK]","[CMT]"]
#[CMT] is used to seperate each comment
#[MASK] is used to mask whole input sequence, removed [MASK] for now, we do not need [MASK] on token level
#[MASKTOK] is used to mask out each token in the input sequence
self.enc.add_tokens(custom_tokens)
if len(og_comments) == 1:
og_comments_str = '[CMT]'+og_comments[0]
else:
og_comments_str = '[CMT]'.join(og_comments)
og_comments_tok = self.enc.encode_plus(text=source_string,
text_pair= og_comments_str, # Sentence to encode.
add_special_tokens = True, # Add '[CLS]' and '[SEP]'
max_length = 512, # Pad & truncate all sentences.
truncation_strategy = 'only_second',
padding = 'max_length',
pad_to_max_length = True) # Return pytorch tensors.
og_comments_input_ids = og_comments_tok['input_ids']
og_comments_attention_masks = og_comments_tok['attention_mask']
target_alt_sentence = og_comments[alt_loc]
target_alt_tok = target_alt_sentence[token_alt_loc]
#alt_og_sentence_lst = og_comments
alt_og_sentence_lst = []
for og_index, og_content in enumerate(og_comments):
if og_index == alt_loc:
tok_lst = og_content.split()
new_lst = []
for tok_index, tok_content in enumerate(tok_lst):
if tok_index == token_alt_loc:
new_lst.append('[MASKTOK]')
else:
new_lst.append(tok_content)
new_content = ' '.join(new_lst)
alt_og_sentence_lst.append(new_content)
else:
alt_og_sentence_lst.append(og_content)
#alt_og_sentence_lst[alt_loc][token_alt_loc] = '[MASKTOK]'
if len(alt_og_sentence_lst) == 1:
altered_str = '[CMT]'+alt_og_sentence_lst[0]
else:
altered_str = '[CMT]'.join(alt_og_sentence_lst)
altered_comments_tok = self.enc.encode_plus(text=source_string,
text_pair= altered_str, # Sentence to encode.
add_special_tokens = True, # Add '[CLS]' and '[SEP]'
max_length = 512, # Pad & truncate all sentences.
truncation_strategy = 'only_second',
padding = 'max_length',
pad_to_max_length = True) # Return pytorch tensors.)
altered_comments_input_ids = altered_comments_tok['input_ids']
altered_comments_attention_masks = altered_comments_tok['attention_mask']
custom_tokens_input_ids = self.enc.convert_tokens_to_ids(custom_tokens)
mask_token_id = custom_tokens_input_ids[0]
cmt_token_id = custom_tokens_input_ids[1]
#need to figure out the corresponding token positions
sep_og_comments_input_ids = sep_token_help(og_comments_input_ids, cmt_token_id)
sep_altered_comments_input_ids = sep_token_help(altered_comments_input_ids, cmt_token_id)
##trial and error
'''
og_size = len(og_comments_input_ids)
og_idx_list = [idx + 1 for idx, val in enumerate(og_comments_input_ids) if val == cmt_token_id]
sep_og_comments_input_ids = [og_comments_input_ids[i:j] for i, j in zip([0] + og_idx_list, og_idx_list + ([og_size] if og_idx_list[-1] != og_size else []))]
altered_size = len(altered_comments_input_ids)
altered_idx_list = [idx + 1 for idx, val in enumerate(altered_comments_input_ids) if val == cmt_token_id]
sep_altered_comments_input_ids = [altered_comments_input_ids[i:j] for i, j in zip([0] + altered_idx_list, altered_idx_list + ([altered_size] if altered_idx_list[-1] != altered_size else []))]
'''
#print('sep_og_comments_input_ids len', len(sep_og_comments_input_ids))
#print('sep_og_comments_input_ids shape ', len(sep_altered_comments_input_ids))
#find the different index
for item in sep_og_comments_input_ids:
if item not in sep_altered_comments_input_ids:
target_index = sep_og_comments_input_ids.index(item)
if not target_index:
raise ValueError('cannot catch that long [MASK] value')
sublists = convert_to_sublists(og_comments_input_ids, cmt_token_id)
#print('value of the sbulists ', sublists)
#print('target_index ', target_index)
#print('len of the sublists ',len(sublists))
target_loc = sublists[target_index-1]
self.target_locations = [t-1 for t in target_loc]
self.og_input_ids = torch.tensor(og_comments_input_ids).to(device)
self.og_attention_mask = torch.tensor(og_comments_attention_masks).to(device)
self.altered_input_ids = torch.tensor(altered_comments_input_ids).to(device)
self.altered_attention_mask = torch.tensor(altered_comments_attention_masks).to(device)
class RumourComboIntervention():
def __init__(self, tokennizer, source_string: str, og_comments: list, alt_loc, device='cuda'):
super()
self.device = device
self.enc = tokennizer
custom_tokens = ["[MASKTOK]","[CMT]"]
#[CMT] is used to seperate each comment
#[MASK] is used to mask whole input sequence, removed [MASK] for now, we do not need [MASK] on token level
#[MASKTOK] is used to mask out each token in the input sequence
self.enc.add_tokens(custom_tokens)
if len(og_comments) == 1:
og_comments_str = '[CMT]'+og_comments[0]
else:
og_comments_str = '[CMT]'.join(og_comments)
og_comments_tok = self.enc.encode_plus(text=source_string,
text_pair= og_comments_str, # Sentence to encode.
add_special_tokens = True, # Add '[CLS]' and '[SEP]'
max_length = 512, # Pad & truncate all sentences.
truncation_strategy = 'only_second',
padding = 'max_length',
pad_to_max_length = True) # Return pytorch tensors.
og_comments_input_ids = og_comments_tok['input_ids']
og_comments_attention_masks = og_comments_tok['attention_mask']
target_alt_sentence = og_comments[alt_loc]
tokens = self.enc.tokenize(target_alt_sentence)
masked_sequence = " ".join(["[MASKTOK]" for _ in tokens])
alt_og_sentence_lst = []
for og_index, og_content in enumerate(og_comments):
if og_index == alt_loc:
alt_og_sentence_lst.append(masked_sequence)
else:
alt_og_sentence_lst.append(og_content)
if len(alt_og_sentence_lst) == 1:
altered_str = '[CMT]'+alt_og_sentence_lst[0]
else:
altered_str = '[CMT]'.join(alt_og_sentence_lst)
altered_comments_tok = self.enc.encode_plus(text=source_string,
text_pair= altered_str, # Sentence to encode.
add_special_tokens = True, # Add '[CLS]' and '[SEP]'
max_length = 512, # Pad & truncate all sentences.
truncation_strategy = 'only_second',
padding = 'max_length',
pad_to_max_length = True) # Return pytorch tensors.)
altered_comments_input_ids = altered_comments_tok['input_ids']
altered_comments_attention_masks = altered_comments_tok['attention_mask']
custom_tokens_input_ids = self.enc.convert_tokens_to_ids(custom_tokens)
mask_token_id = custom_tokens_input_ids[0]
cmt_token_id = custom_tokens_input_ids[1]
#need to figure out the corresponding token positions #NEED to figure out the internvention position
sep_og_comments_input_ids = sep_token_help(og_comments_input_ids, cmt_token_id)
sep_altered_comments_input_ids = sep_token_help(altered_comments_input_ids, cmt_token_id)
for item in sep_og_comments_input_ids:
if item not in sep_altered_comments_input_ids:
target_index = sep_og_comments_input_ids.index(item)
if not target_index:
raise ValueError('cannot catch that long [MASK] value')
sublists = convert_to_sublists(og_comments_input_ids, cmt_token_id)
target_loc = sublists[target_index-1]
self.target_locations = [t-1 for t in target_loc]
print('HERE IS THE CALCULATED TARGET LOCATIONS ', self.target_locations)
self.og_input_ids = torch.tensor(og_comments_input_ids).to(device)
self.og_attention_mask = torch.tensor(og_comments_attention_masks).to(device)
self.altered_input_ids = torch.tensor(altered_comments_input_ids).to(device)
self.altered_attention_mask = torch.tensor(altered_comments_attention_masks).to(device)
class RumourInterventionBASE():
def __init__(self, tokennizer, source_string: str, og_comments: list, alt_loc, gold_label, device='cuda:0'):
super()
self.device = device
self.enc = tokennizer
custom_tokens = ["[MASK]","[CMT]"]
self.enc.add_tokens(["[MASK]","[CMT]"])
if len(og_comments) == 1:
og_comments_str = '[CMT]' + og_comments[0]
else:
og_comments_str = '[CMT]'.join(og_comments)
og_comments_tok = self.enc.encode_plus(text=source_string,
text_pair= og_comments_str, # Sentence to encode.
add_special_tokens = True, # Add '[CLS]' and '[SEP]'
max_length = 512, # Pad & truncate all sentences.
truncation_strategy = 'only_second',
padding = 'max_length',
pad_to_max_length = True) # Return pytorch tensors.
og_comments_input_ids = og_comments_tok['input_ids']
og_comments_attention_masks = og_comments_tok['attention_mask']
self.og_input_ids = torch.tensor(og_comments_input_ids).to(device)
self.og_attention_mask = torch.tensor(og_comments_attention_masks).to(device)
if gold_label == 'rumour':
self.label_tok = 1
else:
self.label_tok = 0
self.label_tok = torch.tensor(self.label_tok).to(device)
class Intervention():
'''
Wrappter for all the possible interventions
one intervention with one modified comment
'''
def __init__(self, tokenizer, source_string: str, og_comments: list, alt_loc, gold_label, device='cpu'):
super()
self.device = device
self.enc = tokenizer
og_comments_str = '[CMT] '.join(og_comments)
og_comments_tok = self.enc.tokenize(og_comments_str)
#cmt_token_index = og_comments_tok.index('[CMT]')
cmt_token_indexes = self.find_custom_token_indexes(og_comments_tok, '[CMT]')
if len(og_comments_tok) > 512:
og_comments_tok = og_comments_tok[:512]
# Create the attention mask
og_attention_mask = [1] * len(og_comments_tok)
for cmt_index in cmt_token_indexes:
og_attention_mask[cmt_index] = 0
#og_attention_mask[cmt_token_index] = 0
# Convert tokenized text to input_ids
og_input_ids = self.enc.convert_tokens_to_ids(og_comments_tok)
self.og_input_ids = torch.tensor(og_input_ids).to(device)
self.og_attention_mask = torch.tensor(og_attention_mask).to(device)
## Pad the input_ids to max_len
## padded_inputs = pad_sequence([torch.tensor(input_ids)],
## batch_first=True,
## padding_value=0,
## max_len = tokenizer.max_len)
altered_lst = og_comments
altered_lst[alt_loc] = '[MASK]'
altered_str = '[CMT] '.join(altered_lst)
altered_comments_tok = self.enc.tokenize(altered_str)
altered_cmt_indexes = self.find_custom_token_indexes(altered_comments_tok, '[CMT]')
if len(altered_comments_tok) > 512:
altered_comments_tok = altered_comments_tok[:512]
#Create attention mask for altered input
altered_comments_attention_mask = [1] * len(altered_comments_tok)
for alt_cmt_index in altered_cmt_indexes:
altered_comments_attention_mask[alt_cmt_index] = 0
altered_input_ids = self.enc.convert_tokens_to_ids(altered_comments_tok)
self.altered_input_ids = torch.tensor(altered_input_ids).to(device)
self.altered_attention_mask = torch.tensor(altered_comments_attention_mask).to(device)
if gold_label == 'rumour':
self.label_tok = 1
else:
self.label_tok = 0
self.label_tok = torch.tensor(self.label_tok).to(device)
def find_custom_token_indexes(self, lst, tok):
indexes = []
for i in range(lst.count(tok)):
indexes.append(lst.index(tok, indexes[i-1]+1 if i > 0 else 0))
return indexes