-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
1121 lines (1042 loc) · 65.9 KB
/
parse.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
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from concurrent.futures import process
import os
import pandas as pd
from tqdm import tqdm
import shortuuid
import xml.etree.ElementTree as ET
import re
import sys
import string
from setup import *
import numpy as np
import json
import random
from sklearn.utils import shuffle
pd.set_option("display.max_colwidth", None)
filter_strings = ["\n", "", " ", None] # the strings we want to filter out
# For debugging or selecting specific datasets to parse:
# DATASETS = ["ETPC"]
def parse_datasets():
'''
Parses all datasets specified in DATASETS to "output/true_data.json" in a unified format.
:return: the statistics about the data during the parsing
'''
df = pd.DataFrame(columns=[DATASET, ORIGIN, PAIR_ID, ID1, ID2, TEXT1, TEXT2, PARAPHRASE, PARAPHRASE_TYPE, SPLIT])
filtered_str = "FILTERING STATS \n\n"
for dataset in DATASETS:
path_to_dataset = os.path.join(DATASETS_FOLDER, dataset)
print("Processing dataset: " + str(path_to_dataset))
filtered_amount = 0
filtered_duplicates = 0
df_tmp = pd.DataFrame(columns=[
DATASET,
ORIGIN,
PAIR_ID,
ID1,
ID2,
TEXT1,
TEXT2,
PARAPHRASE,
PARAPHRASE_TYPE,
SPLIT
])
if dataset == "MPC":
mpcbert_og_path = os.path.join(path_to_dataset, "og") #read og data
mpcbert_mg_path = os.path.join(path_to_dataset, "longformer-large-4096_parallel_mlm_prob_0.15", "mg") #read og data
processed_texts = 0
og_lines = []
mg_lines = []
# First read in all dataset lines
for j, origin_folder in enumerate(os.listdir(mpcbert_og_path)):
print("Reading " + str(origin_folder))
for i, file in enumerate(tqdm(os.listdir(os.path.join(mpcbert_og_path, origin_folder)))):
with open(os.path.join(mpcbert_og_path, origin_folder, file), encoding="utf8", mode = "r") as f1:
with open(os.path.join(mpcbert_mg_path, origin_folder, str(file.replace("ORIG", "SPUN"))), encoding="utf8", mode = "r") as f2:
og_line = f1.readlines()
og_line = [line.rstrip() for line in og_line]
og_line = [l for l in og_line if l != ""][0]
mg_line = f2.readlines()
mg_line = [line.rstrip() for line in mg_line]
mg_line = [l for l in mg_line if l != ""][0]
og_lines.append(og_line)
mg_lines.append(mg_line)
# shuffle both lists with the same order keeping them aligned (random sampling)
print("Shuffle dataset entries to produce random sampling...")
temp = list(zip(og_lines, mg_lines))
random.shuffle(temp)
og_lines, mg_lines = zip(*temp)
og_lines, mg_lines = list(og_lines), list(mg_lines)
for i, og_line in tqdm(enumerate(og_lines), total=len(og_lines)):
mg_line = mg_lines[i]
if og_line not in filter_strings and mg_line not in filter_strings and og_line != mg_line:
if og_line not in df_tmp[TEXT1].tolist() or mg_line not in df_tmp[TEXT2].tolist():
df_tmp.loc[processed_texts] = np.array([
dataset,
str(origin_folder).split("_")[0],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
og_line,
mg_line,
True,
[0],
None
], dtype=object)
processed_texts = processed_texts + 1
if df_tmp.shape[0] >= MAX_DATASET_INPUT: # stop (do not process all)
print("\nReached the max. amount: " + str(MAX_DATASET_INPUT))
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
df_tmp.reset_index(drop=True, inplace=True)
filtered_str = filtered_str + str(dataset) + ": " + str(len(og_lines)) + "\n"
filtered_str = filtered_str + "after filtering: " + str(df_tmp.shape[0]) + "\n"
filtered_str = filtered_str + "filtered duplicates (counted separately): " + str(filtered_duplicates) + "\n"
filtered_str = filtered_str + "filtered pairs: " + str(filtered_amount) + "\n\n"
elif dataset == "ETPC":
# get paraphrase types for all pair IDs (read from different files)
paraphrase_types = {}
with open(os.path.join(path_to_dataset, "textual_paraphrases.xml"), encoding='utf-8', mode = "r") as file:
tree = ET.parse(file)
root = tree.getroot()
for i, elem in enumerate(root):
if elem[0].text in paraphrase_types.keys():
paraphrase_types[elem[0].text] = { PARAPHRASE_TYPE: paraphrase_types[elem[0].text][PARAPHRASE_TYPE] +
[ { TYPE_ID: int(elem[1].text), SENSE_PRESERVING: bool(elem[2].text == "yes"), TEXT1_SCOPE: elem[4].text, TEXT2_SCOPE: elem[5].text } ] }
else:
paraphrase_types[elem[0].text] = { PARAPHRASE_TYPE: [ { TYPE_ID: int(elem[1].text), SENSE_PRESERVING: bool(elem[2].text == "yes"), TEXT1_SCOPE: elem[4].text, TEXT2_SCOPE: elem[5].text } ] }
with open(os.path.join(path_to_dataset, "textual_np_neg.xml"), encoding='utf-8', mode = "r") as file:
tree = ET.parse(file)
root = tree.getroot()
for i, elem in enumerate(root):
if elem[0].text in paraphrase_types.keys():
paraphrase_types[elem[0].text] = { PARAPHRASE_TYPE: paraphrase_types[elem[0].text][PARAPHRASE_TYPE] +
[ { TYPE_ID: int(elem[1].text), SENSE_PRESERVING: bool(elem[2].text == "yes"), TEXT1_SCOPE: elem[4].text, TEXT2_SCOPE: elem[5].text } ] }
else:
paraphrase_types[elem[0].text] = { PARAPHRASE_TYPE: [ { TYPE_ID: int(elem[1].text), SENSE_PRESERVING: bool(elem[2].text == "yes"), TEXT1_SCOPE: elem[4].text, TEXT2_SCOPE: elem[5].text } ] }
with open(os.path.join(path_to_dataset, "textual_np_pos.xml"), encoding='utf-8', mode = "r") as file:
tree = ET.parse(file)
root = tree.getroot()
for i, elem in enumerate(root):
if elem[0].text in paraphrase_types.keys():
paraphrase_types[elem[0].text] = { PARAPHRASE_TYPE: paraphrase_types[elem[0].text][PARAPHRASE_TYPE] +
[ { TYPE_ID: int(elem[1].text), SENSE_PRESERVING: bool(elem[2].text == "yes"), TEXT1_SCOPE: elem[4].text, TEXT2_SCOPE: elem[5].text } ] }
else:
paraphrase_types[elem[0].text] = { PARAPHRASE_TYPE: [ { TYPE_ID: int(elem[1].text), SENSE_PRESERVING: bool(elem[2].text == "yes"), TEXT1_SCOPE: elem[4].text, TEXT2_SCOPE: elem[5].text } ] }
with open(os.path.join(path_to_dataset, "negation.xml"), encoding='utf-8', mode = "r") as file:
tree = ET.parse(file)
root = tree.getroot()
for i, elem in enumerate(root):
if elem[0].text in paraphrase_types.keys():
paraphrase_types[elem[0].text] = { PARAPHRASE_TYPE: paraphrase_types[elem[0].text][PARAPHRASE_TYPE] +
[ { TYPE_ID: int(elem[1].text), SENSE_PRESERVING: bool(elem[2].text == "yes"), TEXT1_SCOPE: elem[4].text, TEXT2_SCOPE: elem[5].text } ] }
else:
paraphrase_types[elem[0].text] = { PARAPHRASE_TYPE: [ { TYPE_ID: int(elem[1].text), SENSE_PRESERVING: bool(elem[2].text == "yes"), TEXT1_SCOPE: elem[4].text, TEXT2_SCOPE: elem[5].text } ] }
# get text pairs and assign the type data
with open(os.path.join(path_to_dataset, "text_pairs.xml"), encoding='utf-8', mode = "r") as file:
tree = ET.parse(file)
root = tree.getroot()
processed_texts = 0
for i, elem in enumerate(tqdm(root)):
paraphrase_types_list = [type_dict[TYPE_ID] for type_dict in paraphrase_types[elem[0].text][PARAPHRASE_TYPE] ]
if elem[3].text not in filter_strings and elem[4].text not in filter_strings and elem[3].text != elem[4].text:
if df_tmp[df_tmp[PARAPHRASE] == bool(int(elem[8].text))].shape[0] >= MAX_DATASET_INPUT/2:
continue # make sure the read in data is balanced
if elem[3].text not in df_tmp[TEXT1].tolist() or elem[4].text not in df_tmp[TEXT2].tolist():
df_tmp.loc[processed_texts] = np.array([
dataset,
"newswire",
shortuuid.uuid()[:8],
elem[1].text,
elem[2].text,
elem[3].text,
elem[4].text,
bool(int(elem[8].text)),
paraphrase_types_list,
None
], dtype=object)
processed_texts = processed_texts + 1
if processed_texts >= MAX_DATASET_INPUT:
print("\nReached the max. amount: " + str(MAX_DATASET_INPUT))
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
#df_tmp = balance_dataset(df_tmp)
df_tmp.reset_index(drop=True, inplace=True)
filtered_str = filtered_str + str(dataset) + ": " + str(len(root)) + "\n"
filtered_str = filtered_str + "after filtering: " + str(df_tmp.shape[0]) + "\n"
filtered_str = filtered_str + "filtered duplicates (counted separately): " + str(filtered_duplicates) + "\n"
filtered_str = filtered_str + "filtered pairs: " + str(filtered_amount) + "\n\n"
elif dataset == "SAv2":
asv2_path = os.path.join(path_to_dataset)
with open(os.path.join(asv2_path, "normal.aligned"), encoding="utf8", mode = "r") as f1:
with open(os.path.join(asv2_path, "simple.aligned"), encoding="utf8", mode = "r") as f2:
og_lines = f1.readlines()
og_lines = [line.rstrip() for line in og_lines]
og_lines = [l for l in og_lines if l != ""]
mg_lines = f2.readlines()
mg_lines = [line.rstrip() for line in mg_lines]
mg_lines = [l for l in mg_lines if l != ""]
# shuffle both lists with the same order keeping them aligned (random sampling)
print("Shuffle dataset entries to produce random sampling...")
temp = list(zip(og_lines, mg_lines))
random.shuffle(temp)
og_lines, mg_lines = zip(*temp)
og_lines, mg_lines = list(og_lines), list(mg_lines)
print("Read dataset entries...")
for i, og_line in enumerate(tqdm(og_lines)):
if og_line.split("\t")[2] not in filter_strings and mg_lines[i].split("\t")[2] not in filter_strings and og_line.split("\t")[2] != mg_lines[i].split("\t")[2]:
if og_line.split("\t")[2] not in df_tmp[TEXT1].tolist() or mg_lines[i].split("\t")[2] not in df_tmp[TEXT2].tolist():
df_tmp.loc[i] = np.array([
dataset,
"wikipedia",
shortuuid.uuid()[:8],
og_line.split("\t")[0].translate(str.maketrans('', '', string.punctuation+" ")) + "_" + shortuuid.uuid()[:8],
mg_lines[i].split("\t")[0].translate(str.maketrans('', '', string.punctuation+" ")) + "_" + shortuuid.uuid()[:8],
og_line.split("\t")[2],
mg_lines[i].split("\t")[2],
True,
[16], # simplification dataset ( => only ellipsis)
None
], dtype=object)
if df_tmp.shape[0] >= MAX_DATASET_INPUT:
print("\nReached the max. amount: " + str(MAX_DATASET_INPUT))
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
df_tmp.reset_index(drop=True, inplace=True)
filtered_str = filtered_str + str(dataset) + ": " + str(len(og_lines)) + "\n"
filtered_str = filtered_str + "after filtering: " + str(df_tmp.shape[0]) + "\n"
filtered_str = filtered_str + "filtered duplicates (counted separately): " + str(filtered_duplicates) + "\n"
filtered_str = filtered_str + "filtered pairs: " + str(filtered_amount) + "\n\n"
elif dataset == "QQP":
qqp_path = os.path.join(path_to_dataset, "questions.csv")
quora_df = pd.read_csv(qqp_path)
quora_df = quora_df.sample(frac=1) # shuffle for random sampling
for i, row in tqdm(quora_df.iterrows(), total=quora_df.shape[0]):
if df_tmp[df_tmp[PARAPHRASE] == bool(row["is_duplicate"])].shape[0] >= MAX_DATASET_INPUT / 2:
continue # make sure the read in data is balanced
if row["question1"] not in filter_strings and row["question2"] not in filter_strings and row["question1"] != row["question2"]:
if row["question1"] not in df_tmp[TEXT1].tolist() or row["question2"] not in df_tmp[TEXT2].tolist():
df_tmp.loc[i] = np.array([
dataset,
"quora",
str(row["id"]) + "_" + shortuuid.uuid()[:8],
str(row["qid1"]) + "_" + shortuuid.uuid()[:8],
str(row["qid2"]) + "_" + shortuuid.uuid()[:8],
row["question1"],
row["question2"],
bool(row["is_duplicate"]),
[0], # unknown type
None
], dtype=object)
if df_tmp.shape[0] >= MAX_DATASET_INPUT:
print("\nReached the max. amount: " + str(MAX_DATASET_INPUT))
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
#df_tmp = balance_dataset(df_tmp)
df_tmp.reset_index(drop=True, inplace=True)
filtered_str = filtered_str + str(dataset) + ": " + str(quora_df.shape[0]) + "\n"
filtered_str = filtered_str + "after filtering: " + str(df_tmp.shape[0]) + "\n"
filtered_str = filtered_str + "filtered duplicates (counted separately): " + str(filtered_duplicates) + "\n"
filtered_str = filtered_str + "filtered pairs: " + str(filtered_amount) + "\n\n"
elif dataset == "TURL":
turl_path = os.path.join(path_to_dataset)
processed_texts = 0
with open(os.path.join(turl_path, "Twitter_URL_Corpus_test.txt"), encoding="utf8", mode = "r") as f1:
test_lines = f1.readlines()
test_lines = [line.rstrip() for line in test_lines]
test_lines = [l for l in test_lines if l != ""]
random.shuffle(test_lines) # shuffle for random sampling
for i, line in enumerate(tqdm(test_lines)):
if line != "\n" and line.split("\t")[0] not in filter_strings and line.split("\t")[
1] not in filter_strings and line.split("\t")[0] != line.split("\t")[1] and int(
line.split("\t")[2][1]) != 3: # if amazon workers could not decide, skip (3/6)
# based on the datasets paper, we value a phrase as paraphrase when >=4 out of 6 amazon workers marked it a such
is_paraphrase = int(line.split("\t")[2][1]) >= 4
if df_tmp[df_tmp[PARAPHRASE] == is_paraphrase].shape[0] >= MAX_DATASET_INPUT / 2:
continue # make sure the read in data is balanced
if line.split("\t")[0] not in df_tmp[TEXT1].tolist() or line.split("\t")[1] not in df_tmp[TEXT2].tolist():
df_tmp.loc[processed_texts] = np.array([
dataset,
"twitter news",
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
line.split("\t")[0],
line.split("\t")[1],
is_paraphrase,
[0],
"test"
], dtype=object)
processed_texts = processed_texts + 1
if df_tmp.shape[0] >= MAX_DATASET_INPUT / 2:
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
amount_test_data = int(df_tmp.shape[0])
with open(os.path.join(turl_path, "Twitter_URL_Corpus_train.txt"), encoding="utf8", mode = "r") as f2:
train_lines = f2.readlines()
train_lines = [line.rstrip() for line in train_lines]
train_lines = [l for l in train_lines if l != ""]
random.shuffle(train_lines) # shuffle for random sampling
for i, line in enumerate(tqdm(train_lines)):
if line != "\n" and line.split("\t")[0] not in filter_strings and line.split("\t")[1] not in filter_strings and line.split("\t")[0] != line.split("\t")[1] and int(line.split("\t")[2][1]) != 3: # if amazon workers could not decide, skip (3/6)
# based on the datasets paper, we value a phrase as paraphrase when >=4 out of 6 amazon workers marked it a such
is_paraphrase = int(line.split("\t")[2][1]) >= 4
if df_tmp[df_tmp[PARAPHRASE] == is_paraphrase].shape[0] >= MAX_DATASET_INPUT / 2:
continue # make sure the read in data is balanced
if line.split("\t")[0] not in df_tmp[TEXT1].tolist() or line.split("\t")[1] not in df_tmp[
TEXT2].tolist():
df_tmp.loc[processed_texts] = np.array([
dataset,
"twitter news",
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
line.split("\t")[0],
line.split("\t")[1],
is_paraphrase,
[0],
"train"
], dtype=object)
processed_texts = processed_texts + 1
if df_tmp.shape[0] >= MAX_DATASET_INPUT:
print("\nReached the max. amount (depends on amount of test split data included): " + str(MAX_DATASET_INPUT-amount_test_data))
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
#df_tmp = balance_dataset(df_tmp)
df_tmp.reset_index(drop=True, inplace=True)
filtered_str = filtered_str + str(dataset) + ": " + str(len(test_lines+train_lines)) + "\n"
filtered_str = filtered_str + "after filtering: " + str(df_tmp.shape[0]) + "\n"
filtered_str = filtered_str + "filtered duplicates (counted separately): " + str(filtered_duplicates) + "\n"
filtered_str = filtered_str + "filtered pairs: " + str(filtered_amount) + "\n\n"
elif dataset == "ParaNMT":
nmt_path = os.path.join(path_to_dataset) #read train data
with open(os.path.join(nmt_path, "para-nmt-50m.txt"), encoding="utf8", mode = "r") as f: # read file line-by-line as it is very big
lines = f.readlines()
random.shuffle(lines) # shuffle for random sampling
for i, line in tqdm(enumerate(lines), total=len(lines)):
l = line.rstrip().split("\t")
if l[0] != l[1] and l[0] not in filter_strings and l[1] not in filter_strings:
if l[0] not in df_tmp[TEXT1].tolist() or l[1] not in df_tmp[TEXT2].tolist():
df_tmp.loc[i] = np.array([
dataset,
"czeng",
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
l[0],
l[1],
True,
[0],
None
], dtype=object)
if df_tmp.shape[0] >= MAX_DATASET_INPUT:
print("\nReached the max. amount: " + str(MAX_DATASET_INPUT))
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
df_tmp.reset_index(drop=True, inplace=True)
filtered_str = filtered_str + str(dataset) + ": " + str(len(lines)) + "\n"
filtered_str = filtered_str + "after filtering: " + str(df_tmp.shape[0]) + "\n"
filtered_str = filtered_str + "filtered duplicates (counted separately): " + str(filtered_duplicates) + "\n"
filtered_str = filtered_str + "filtered pairs: " + str(filtered_amount) + "\n\n"
elif dataset == "APT":
apt_path = os.path.join(path_to_dataset)
processed_texts = 0
print("Reading MSRP split...")
with open(os.path.join(apt_path, "apt5-m"), encoding="utf8", mode = "r") as f1:
lines = f1.readlines()
lines = [line.rstrip() for line in lines]
lines = [l for l in lines if l != ""][1:]
random.shuffle(lines) # shuffle for random sampling
for i, line in enumerate(tqdm(lines)):
if line.split("\t")[0] not in filter_strings and line.split("\t")[1] not in filter_strings and line.split("\t")[0] != line.split("\t")[1]:
is_paraphrase = bool(int(line.split("\t")[2]))
if df_tmp[df_tmp[PARAPHRASE] == is_paraphrase].shape[0] >= MAX_DATASET_INPUT / 2:
continue # make sure the read in data is balanced
if line.split("\t")[0] not in df_tmp[TEXT1].tolist() or line.split("\t")[1] not in df_tmp[TEXT2].tolist():
df_tmp.loc[processed_texts] = np.array([
dataset,
"msrp",
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
line.split("\t")[0],
line.split("\t")[1],
is_paraphrase,
[0],
None
], dtype=object)
processed_texts = processed_texts + 1
if df_tmp.shape[0] >= MAX_DATASET_INPUT / 2:
print("\nReached the max. amount (half-way): " + str(MAX_DATASET_INPUT))
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
df_tmp.reset_index(drop=True, inplace=True)
filtered_str = filtered_str + str(dataset) + " (msrp split): " + str(len(lines)) + "\n"
filtered_str = filtered_str + "after filtering: " + str(df_tmp.shape[0]) + "\n"
filtered_str = filtered_str + "filtered duplicates (counted separately): " + str(filtered_duplicates) + "\n"
filtered_str = filtered_str + "filtered pairs: " + str(filtered_amount) + "\n\n"
amount_msrp_split = int(df_tmp.shape[0])
print("Reading Twitter PPDB split...")
with open(os.path.join(apt_path, "apt5-tw"), encoding="utf8", mode = "r") as f1:
lines = f1.readlines()
lines = [line.rstrip() for line in lines]
lines = [l for l in lines if l != ""][1:]
random.shuffle(lines) # shuffle for random sampling
for i, line in enumerate(tqdm(lines)):
if line.split("\t")[0] not in filter_strings and line.split("\t")[1] not in filter_strings and line.split("\t")[0] != line.split("\t")[1]:
is_paraphrase = bool(int(line.split("\t")[2]))
if df_tmp[df_tmp[PARAPHRASE] == is_paraphrase].shape[0] >= MAX_DATASET_INPUT / 2:
continue # make sure the read in data is balanced
if line.split("\t")[0] not in df_tmp[TEXT1].tolist() or line.split("\t")[1] not in df_tmp[
TEXT2].tolist():
df_tmp.loc[processed_texts] = np.array([
dataset,
"twitterppdb",
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
line.split("\t")[0],
line.split("\t")[1],
is_paraphrase,
[0],
None
], dtype=object)
processed_texts = processed_texts + 1
if df_tmp.shape[0] >= MAX_DATASET_INPUT:
print("\nReached the max. amount: " + str(MAX_DATASET_INPUT))
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
#df_tmp = balance_dataset(df_tmp)
df_tmp.reset_index(drop=True, inplace=True)
filtered_str = filtered_str + str(dataset) + " (ppdb split): " + str(len(lines)) + "\n"
filtered_str = filtered_str + "after filtering: " + str(df_tmp.shape[0] - amount_msrp_split) + "\n"
filtered_str = filtered_str + "filtered duplicates (counted separately): " + str(filtered_duplicates) + "\n"
filtered_str = filtered_str + "filtered pairs: " + str(filtered_amount) + "\n\n"
elif dataset == "APH":
aph_path = os.path.join(path_to_dataset)
processed_texts = 0
with open(os.path.join(aph_path, "ap-h-test"), encoding="utf8", mode = "r") as f1:
test_lines = f1.readlines()
test_lines = [line.rstrip() for line in test_lines]
test_lines = [l for l in test_lines if l != ""][1:]
random.shuffle(test_lines) # shuffle for random sampling
for i, line in enumerate(tqdm(test_lines)):
if line.split("\t")[0] not in filter_strings and line.split("\t")[1] not in filter_strings and \
line.split("\t")[0] != line.split("\t")[1]:
is_paraphrase = bool(int(line.split("\t")[2]))
if df_tmp[df_tmp[PARAPHRASE] == is_paraphrase].shape[0] >= MAX_DATASET_INPUT / 2:
continue # make sure the read in data is balanced
if line.split("\t")[0] not in df_tmp[TEXT1].tolist() or line.split("\t")[1] not in df_tmp[
TEXT2].tolist():
df_tmp.loc[processed_texts] = np.array([
dataset,
"msrp,ppnmt",
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
line.split("\t")[0],
line.split("\t")[1],
is_paraphrase,
[0],
"test"
], dtype=object)
processed_texts = processed_texts + 1
if df_tmp.shape[0] >= MAX_DATASET_INPUT:
print("\nReached the max. amount: " + str(MAX_DATASET_INPUT))
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
with open(os.path.join(aph_path, "ap-h-train"), encoding="utf8", mode = "r") as f2:
train_lines = f2.readlines()
train_lines = [line.rstrip() for line in train_lines]
train_lines = [l for l in train_lines if l != ""][1:]
random.shuffle(train_lines) # shuffle for random sampling
for i, line in enumerate(tqdm(train_lines)):
if line.split("\t")[0] not in filter_strings and line.split("\t")[1] not in filter_strings and line.split("\t")[0] != line.split("\t")[1]:
is_paraphrase = bool(int(line.split("\t")[2]))
if df_tmp[df_tmp[PARAPHRASE] == is_paraphrase].shape[0] >= MAX_DATASET_INPUT / 2:
continue # make sure the read in data is balanced
if line.split("\t")[0] not in df_tmp[TEXT1].tolist() or line.split("\t")[1] not in df_tmp[
TEXT2].tolist():
df_tmp.loc[processed_texts] = np.array([
dataset,
"msrp,ppnmt",
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
line.split("\t")[0],
line.split("\t")[1],
is_paraphrase,
[0],
"train"
], dtype=object)
processed_texts = processed_texts + 1
if df_tmp.shape[0] >= MAX_DATASET_INPUT:
print("\nReached the max. amount: " + str(MAX_DATASET_INPUT))
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
#df_tmp = balance_dataset(df_tmp)
df_tmp.reset_index(drop=True, inplace=True)
filtered_str = filtered_str + str(dataset) + ": " + str(len(test_lines+train_lines)) + "\n"
filtered_str = filtered_str + "after filtering: " + str(df_tmp.shape[0]) + "\n"
filtered_str = filtered_str + "filtered duplicates (counted separately): " + str(filtered_duplicates) + "\n"
filtered_str = filtered_str + "filtered pairs: " + str(filtered_amount) + "\n\n"
elif dataset == "PAWSWiki":
paws_path = os.path.join(path_to_dataset)
processed_texts = 0
with open(os.path.join(paws_path, "dev.tsv"), encoding="utf8", mode = "r") as f1:
dev_lines = f1.readlines()
dev_lines = [line.rstrip() for line in dev_lines]
dev_lines = ["dev" + l for l in dev_lines if l != ""][1:]
random.shuffle(dev_lines) # shuffle for random sampling
for i, line in enumerate(tqdm(dev_lines)):
if line.split("\t")[1] not in filter_strings and line.split("\t")[2] not in filter_strings and \
line.split("\t")[1] != line.split("\t")[2]:
is_paraphrase = bool(int(line.split("\t")[3]))
if df_tmp[df_tmp[PARAPHRASE] == is_paraphrase].shape[0] >= MAX_DATASET_INPUT / 2:
continue # make sure the read in data is balanced
if line.split("\t")[1] not in df_tmp[TEXT1].tolist() or line.split("\t")[2] not in df_tmp[
TEXT2].tolist():
df_tmp.loc[processed_texts] = np.array([
dataset,
"wikipedia",
line.split("\t")[0] + "_" + shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
line.split("\t")[1],
line.split("\t")[2],
is_paraphrase,
[0],
"dev"
], dtype=object)
processed_texts = processed_texts + 1
if df_tmp.shape[0] >= MAX_DATASET_INPUT:
print("\nReached the max. amount: " + str(MAX_DATASET_INPUT))
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
with open(os.path.join(paws_path, "test.tsv"), encoding="utf8", mode = "r") as f2:
test_lines = f2.readlines()
test_lines = [line.rstrip() for line in test_lines]
test_lines = ["test" + l for l in test_lines if l != ""][1:]
random.shuffle(test_lines) # shuffle for random sampling
for i, line in enumerate(tqdm(test_lines)):
if line.split("\t")[1] not in filter_strings and line.split("\t")[2] not in filter_strings and \
line.split("\t")[1] != line.split("\t")[2]:
is_paraphrase = bool(int(line.split("\t")[3]))
if df_tmp[df_tmp[PARAPHRASE] == is_paraphrase].shape[0] >= MAX_DATASET_INPUT / 2:
continue # make sure the read in data is balanced
if line.split("\t")[1] not in df_tmp[TEXT1].tolist() or line.split("\t")[2] not in df_tmp[
TEXT2].tolist():
df_tmp.loc[processed_texts] = np.array([
dataset,
"wikipedia",
line.split("\t")[0] + "_" + shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
line.split("\t")[1],
line.split("\t")[2],
is_paraphrase,
[0],
"test"
], dtype=object)
processed_texts = processed_texts + 1
if df_tmp.shape[0] >= MAX_DATASET_INPUT:
print("\nReached the max. amount: " + str(MAX_DATASET_INPUT))
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
with open(os.path.join(paws_path, "train.tsv"), encoding="utf8", mode = "r") as f3:
train_lines = f3.readlines()
train_lines = [line.rstrip() for line in train_lines]
train_lines = ["train"+l for l in train_lines if l != ""][1:]
random.shuffle(train_lines) # shuffle for random sampling
for i, line in enumerate(tqdm(train_lines)):
if line.split("\t")[1] not in filter_strings and line.split("\t")[2] not in filter_strings and \
line.split("\t")[1] != line.split("\t")[2]:
is_paraphrase = bool(int(line.split("\t")[3]))
if df_tmp[df_tmp[PARAPHRASE] == is_paraphrase].shape[0] >= MAX_DATASET_INPUT / 2:
continue # make sure the read in data is balanced
if line.split("\t")[1] not in df_tmp[TEXT1].tolist() or line.split("\t")[2] not in df_tmp[
TEXT2].tolist():
df_tmp.loc[processed_texts] = np.array([
dataset,
"wikipedia",
line.split("\t")[0]+"_"+shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
line.split("\t")[1],
line.split("\t")[2],
is_paraphrase,
[0],
"train"
], dtype=object)
processed_texts = processed_texts + 1
if df_tmp.shape[0] >= MAX_DATASET_INPUT:
print("\nReached the max. amount: " + str(MAX_DATASET_INPUT))
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
#df_tmp = balance_dataset(df_tmp)
df_tmp.reset_index(drop=True, inplace=True)
filtered_str = filtered_str + str(dataset) + ": " + str(len(train_lines+test_lines+dev_lines)) + "\n"
filtered_str = filtered_str + "after filtering: " + str(df_tmp.shape[0]) + "\n"
filtered_str = filtered_str + "filtered duplicates (counted separately): " + str(filtered_duplicates) + "\n"
filtered_str = filtered_str + "filtered pairs: " + str(filtered_amount) + "\n\n"
elif dataset == "ParaSCI":
parasci_paths = [os.path.join(path_to_dataset, "ParaSCI-ACL"), os.path.join(path_to_dataset, "ParaSCI-arXiv")]
processed_texts = 0
# --> ACL split
print("Processing ACL split...")
all_og_lines = []
all_hg_lines = []
with open(os.path.join(parasci_paths[0], "test", "test.src"), encoding="utf8", mode = "r") as f1:
with open(os.path.join(parasci_paths[0], "test", "test.tgt"), encoding="utf8", mode = "r") as f2:
og_lines = f1.readlines()
og_lines = [line.rstrip() for line in og_lines]
hg_lines = f2.readlines()
hg_lines = [line.rstrip() for line in hg_lines]
all_og_lines = all_og_lines + og_lines
all_hg_lines = all_hg_lines + hg_lines
# shuffle both lists with the same order keeping them aligned (random sampling)
temp = list(zip(og_lines, hg_lines))
random.shuffle(temp)
og_lines, hg_lines = zip(*temp)
og_lines, hg_lines = list(og_lines), list(hg_lines)
for i, og_line in tqdm(enumerate(og_lines), total=len(og_lines)):
hg_line = hg_lines[i]
if og_line not in filter_strings and hg_line not in filter_strings and og_line != hg_line:
if og_line not in df_tmp[TEXT1].tolist() or hg_line not in df_tmp[TEXT2].tolist():
df_tmp.loc[processed_texts] = np.array([
dataset,
"ACL",
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
og_line,
hg_line,
True,
[0],
"test"
], dtype=object)
processed_texts = processed_texts + 1
if df_tmp.shape[0] >= MAX_DATASET_INPUT / 6: # stop (do not process all)
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
with open(os.path.join(parasci_paths[0], "train", "train.src"), encoding="utf8", mode = "r") as f1:
with open(os.path.join(parasci_paths[0], "train", "train.tgt"), encoding="utf8", mode = "r") as f2:
og_lines = f1.readlines()
og_lines = [line.rstrip() for line in og_lines]
hg_lines = f2.readlines()
hg_lines = [line.rstrip() for line in hg_lines]
all_og_lines = all_og_lines + og_lines
all_hg_lines = all_hg_lines + hg_lines
# shuffle both lists with the same order keeping them aligned (random sampling)
temp = list(zip(og_lines, hg_lines))
random.shuffle(temp)
og_lines, hg_lines = zip(*temp)
og_lines, hg_lines = list(og_lines), list(hg_lines)
for i, og_line in tqdm(enumerate(og_lines), total=len(og_lines)):
hg_line = hg_lines[i]
if og_line not in filter_strings and hg_line not in filter_strings and og_line != hg_line:
if og_line not in df_tmp[TEXT1].tolist() or hg_line not in df_tmp[TEXT2].tolist():
df_tmp.loc[processed_texts] = np.array([
dataset,
"ACL",
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
og_line,
hg_line,
True,
[0],
"train"
], dtype=object)
processed_texts = processed_texts + 1
if df_tmp.shape[0] >= 2*MAX_DATASET_INPUT / 6: # stop (do not process all)
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
with open(os.path.join(parasci_paths[0], "val", "val.src"), encoding="utf8", mode = "r") as f1:
with open(os.path.join(parasci_paths[0], "val", "val.tgt"), encoding="utf8", mode = "r") as f2:
og_lines = f1.readlines()
og_lines = [line.rstrip() for line in og_lines]
hg_lines = f2.readlines()
hg_lines = [line.rstrip() for line in hg_lines]
all_og_lines = all_og_lines + og_lines
all_hg_lines = all_hg_lines + hg_lines
# shuffle both lists with the same order keeping them aligned (random sampling)
temp = list(zip(og_lines, hg_lines))
random.shuffle(temp)
og_lines, hg_lines = zip(*temp)
og_lines, hg_lines = list(og_lines), list(hg_lines)
for i, og_line in tqdm(enumerate(og_lines), total=len(og_lines)):
hg_line = hg_lines[i]
if og_line not in filter_strings and hg_line not in filter_strings and og_line != hg_line:
if og_line not in df_tmp[TEXT1].tolist() or hg_line not in df_tmp[TEXT2].tolist():
df_tmp.loc[processed_texts] = np.array([
dataset,
"ACL",
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
og_line,
hg_line,
True,
[0],
"val"
], dtype=object)
processed_texts = processed_texts + 1
if df_tmp.shape[0] >= 3*MAX_DATASET_INPUT / 6: # stop (do not process all)
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
df_tmp.reset_index(drop=True, inplace=True)
amount_acl_split = int(df_tmp.shape[0])
filtered_str = filtered_str + str(dataset) + " (ACL split): " + str(len(all_og_lines)) + "\n"
filtered_str = filtered_str + "after filtering: " + str(df_tmp.shape[0]) + "\n"
filtered_str = filtered_str + "filtered duplicates (counted separately): " + str(filtered_duplicates) + "\n"
filtered_str = filtered_str + "filtered pairs: " + str(filtered_amount) + "\n\n"
# --> arXiv split
filtered_amount = 0
filtered_duplicates = 0
all_og_lines = []
all_hg_lines = []
print("Processing arXiv split...")
with open(os.path.join(parasci_paths[1], "test", "test.src"), encoding="utf8", mode="r") as f1:
with open(os.path.join(parasci_paths[1], "test", "test.tgt"), encoding="utf8", mode="r") as f2:
og_lines = f1.readlines()
og_lines = [line.rstrip() for line in og_lines]
hg_lines = f2.readlines()
hg_lines = [line.rstrip() for line in hg_lines]
all_og_lines = all_og_lines + og_lines
all_hg_lines = all_hg_lines + hg_lines
# shuffle both lists with the same order keeping them aligned (random sampling)
temp = list(zip(og_lines, hg_lines))
random.shuffle(temp)
og_lines, hg_lines = zip(*temp)
og_lines, hg_lines = list(og_lines), list(hg_lines)
for i, og_line in tqdm(enumerate(og_lines), total=len(og_lines)):
hg_line = hg_lines[i]
if og_line not in filter_strings and hg_line not in filter_strings and og_line != hg_line:
if og_line not in df_tmp[TEXT1].tolist() or hg_line not in df_tmp[TEXT2].tolist():
df_tmp.loc[processed_texts] = np.array([
dataset,
"arXiv",
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
og_line,
hg_line,
True,
[0],
"test"
], dtype=object)
processed_texts = processed_texts + 1
if df_tmp.shape[0] >= 4*MAX_DATASET_INPUT / 6: # stop (do not process all)
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
with open(os.path.join(parasci_paths[1], "train", "train.src"), encoding="utf8", mode="r") as f1:
with open(os.path.join(parasci_paths[1], "train", "train.tgt"), encoding="utf8", mode="r") as f2:
og_lines = f1.readlines()
og_lines = [line.rstrip() for line in og_lines]
hg_lines = f2.readlines()
hg_lines = [line.rstrip() for line in hg_lines]
all_og_lines = all_og_lines + og_lines
all_hg_lines = all_hg_lines + hg_lines
# shuffle both lists with the same order keeping them aligned (random sampling)
temp = list(zip(og_lines, hg_lines))
random.shuffle(temp)
og_lines, hg_lines = zip(*temp)
og_lines, hg_lines = list(og_lines), list(hg_lines)
for i, og_line in tqdm(enumerate(og_lines), total=len(og_lines)):
hg_line = hg_lines[i]
if og_line not in filter_strings and hg_line not in filter_strings and og_line != hg_line:
if og_line not in df_tmp[TEXT1].tolist() or hg_line not in df_tmp[TEXT2].tolist():
df_tmp.loc[processed_texts] = np.array([
dataset,
"arXiv",
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
og_line,
hg_line,
True,
[0],
"train"
], dtype=object)
processed_texts = processed_texts + 1
if df_tmp.shape[0] >= 5*MAX_DATASET_INPUT / 6: # stop (do not process all)
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
with open(os.path.join(parasci_paths[1], "val", "val.src"), encoding="utf8", mode="r") as f1:
with open(os.path.join(parasci_paths[1], "val", "val.tgt"), encoding="utf8", mode="r") as f2:
og_lines = f1.readlines()
og_lines = [line.rstrip() for line in og_lines]
hg_lines = f2.readlines()
hg_lines = [line.rstrip() for line in hg_lines]
all_og_lines = all_og_lines + og_lines
all_hg_lines = all_hg_lines + hg_lines
# shuffle both lists with the same order keeping them aligned (random sampling)
temp = list(zip(og_lines, hg_lines))
random.shuffle(temp)
og_lines, hg_lines = zip(*temp)
og_lines, hg_lines = list(og_lines), list(hg_lines)
for i, og_line in tqdm(enumerate(og_lines), total=len(og_lines)):
hg_line = hg_lines[i]
if og_line not in filter_strings and hg_line not in filter_strings and og_line != hg_line:
if og_line not in df_tmp[TEXT1].tolist() or hg_line not in df_tmp[TEXT2].tolist():
df_tmp.loc[processed_texts] = np.array([
dataset,
"arXiv",
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
og_line,
hg_line,
True,
[0],
"val"
], dtype=object)
processed_texts = processed_texts + 1
if df_tmp.shape[0] >= MAX_DATASET_INPUT: # stop (do not process all)
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
df_tmp.reset_index(drop=True, inplace=True)
filtered_str = filtered_str + str(dataset) + " (arXiv split): " + str(len(all_og_lines)) + "\n"
filtered_str = filtered_str + "after filtering: " + str(df_tmp.shape[0] - amount_acl_split) + "\n"
filtered_str = filtered_str + "filtered duplicates (counted separately): " + str(filtered_duplicates) + "\n"
filtered_str = filtered_str + "filtered pairs: " + str(filtered_amount) + "\n\n"
elif dataset == "MSCOCO":
# after https://ojs.aaai.org/index.php/AAAI/article/view/11956
# every image has been annotated by multiple annotators. Whe combine these image captions to multiple versions of paraphrased pairs.
coco_train_path = os.path.join(path_to_dataset, "annotations", "captions_train2017.json")
coco_val_path = os.path.join(path_to_dataset, "annotations", "captions_val2017.json")
# --> Train Split
print("Processing train split...")
with open(coco_train_path, "r") as f:
data_dict = json.load(f)
captions_df = pd.read_json(json.dumps(data_dict["annotations"]))
img_ids = captions_df["image_id"].unique()
random.shuffle(img_ids) # sample randomly
print("Found unique images: " + str(len(img_ids)))
print("Reading data...")
for i, img_id in tqdm(enumerate(img_ids), total=len(img_ids)):
this_img_df = captions_df[captions_df["image_id"] == img_id].reset_index(drop=True)
random_caption_id_1 = random.randint(0, this_img_df.shape[0]-1) # sample two annotators randomly from all five annotators
random_caption_id_2 = random.randint(0, this_img_df.shape[0]-1)
while random_caption_id_2 == random_caption_id_1:
random_caption_id_2 = random.randint(0, this_img_df.shape[0]-1)
og_line = this_img_df.iloc[random_caption_id_1]["caption"]
hg_line = this_img_df.iloc[random_caption_id_2]["caption"]
if og_line not in filter_strings and hg_line not in filter_strings and og_line != hg_line:
if og_line not in df_tmp[TEXT1].tolist() or hg_line not in df_tmp[TEXT2].tolist():
df_tmp.loc[i] = np.array([
dataset,
"imgcaptions",
str(img_id)+"_"+shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
shortuuid.uuid()[:8],
og_line,
hg_line,
True,
[0],
"train"
], dtype=object)
if df_tmp.shape[0] >= MAX_DATASET_INPUT / 2: # stop (do not process all)
break
else:
filtered_duplicates = filtered_duplicates + 1
else:
filtered_amount = filtered_amount + 1
df_tmp.reset_index(drop=True, inplace=True)
filtered_str = filtered_str + str(dataset) + " (unique image ids) (train split): " + str(len(img_ids)) + "\n"
filtered_str = filtered_str + "after filtering: " + str(df_tmp.shape[0]) + "\n"
filtered_str = filtered_str + "filtered duplicates (counted separately): " + str(filtered_duplicates) + "\n"
filtered_str = filtered_str + "filtered pairs: " + str(filtered_amount) + "\n\n"
amount_train_split = int(df_tmp.shape[0])
filtered_amount = 0
filtered_duplicates = 0
# --> Val Split
print("Processing val split...")
with open(coco_val_path, "r") as f:
data_dict = json.load(f)
captions_df = pd.read_json(json.dumps(data_dict["annotations"]))
img_ids = captions_df["image_id"].unique()
random.shuffle(img_ids) # sample randomly
print("Found unique images: " + str(len(img_ids)))
print("Reading data...")
for i, img_id in tqdm(enumerate(img_ids), total=len(img_ids)):
this_img_df = captions_df[captions_df["image_id"] == img_id].reset_index(drop=True)
random_caption_id_1 = random.randint(0, this_img_df.shape[0]-1) # sample two annotators randomly from all five annotators
random_caption_id_2 = random.randint(0, this_img_df.shape[0]-1)
while random_caption_id_2 == random_caption_id_1:
random_caption_id_2 = random.randint(0, this_img_df.shape[0]-1)
og_line = this_img_df.iloc[random_caption_id_1]["caption"]
hg_line = this_img_df.iloc[random_caption_id_2]["caption"]
if og_line not in filter_strings and hg_line not in filter_strings and og_line != hg_line:
if og_line not in df_tmp[TEXT1].tolist() or hg_line not in df_tmp[TEXT2].tolist():
df_tmp.loc[i] = np.array([
dataset,
"imgcaptions",
str(img_id)+"_"+shortuuid.uuid()[:8],