-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_kb_aligned_text_corpora.py
executable file
·1245 lines (1048 loc) · 52 KB
/
create_kb_aligned_text_corpora.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
# -*- coding: utf-8 -*-
import json
import itertools
import logging
import pickle as pkl
import pandas as pd
import collections
import pickle
import os
import random
import argparse
import spacy
from pathlib import Path
from tqdm import tqdm
from scispacy.umls_linking import UmlsEntityLinker
from scispacy.linking_utils import KnowledgeBase
from typing import List, Set, Dict, Union, Tuple, Optional
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
level=logging.INFO,
)
logger = logging.getLogger(__name__)
class Triples:
def __init__(
self,
fname: Union[str, Path],
cui2sty_fname: Union[str, Path] = None
):
self.read_triples(fname)
self.set_rel_to_pairs()
self.set_pair_to_rels()
if cui2sty_fname:
self.set_pair_to_type(cui2sty_fname)
self.counts()
def read_triples(self, fname):
self.triples: List[str, str, str] = list()
self.entities: Set[str] = set()
self.relations: Set[str] = set()
with open(fname) as rf:
for line in tqdm(rf, desc='Reading raw triples ...'):
line = line.strip()
if not line:
continue
h, r, t = line.split('\t')
self.triples.append((h, r, t))
self.entities.update([h, t])
self.relations.add(r)
def set_rel_to_pairs(self):
self.rel2pairs = collections.defaultdict(list)
for h, r, t in tqdm(self.triples, desc=' --- Intializing relation -> pairs map ...'):
self.rel2pairs[r].append((h, t))
def set_pair_to_rels(self):
self.pair2rels = collections.defaultdict(list)
for h, r, t in tqdm(self.triples, desc=' --- Intializing pair -> relations map ...'):
self.pair2rels[(h, t)].append(r)
def set_pair_to_type(self, cui2sty_fname):
with open(cui2sty_fname) as rf:
cui2sty = json.load(rf)
self.pair2type = dict()
for h, r, t in tqdm(self.triples, desc=' --- Intializing pair -> type map ...'):
self.pair2type[(h, t)] = (cui2sty[h], cui2sty[t])
def counts(self):
self.ent_counts = collections.Counter()
self.rel_counts = collections.Counter()
self.pair_counts = collections.Counter()
for h, r, t in tqdm(self.triples, desc=' --- Counting stats ...'):
self.ent_counts.update([h, t])
self.pair_counts.update([(h, t)])
self.rel_counts.update([r])
logger.info(f'Top-10 frequent entities: {self.ent_counts.most_common(10)}')
logger.info(f'Top-10 frequent arg pairs: {self.pair_counts.most_common(10)}')
logger.info(f'Top-10 frequent relations: {self.rel_counts.most_common(10)}')
def __getitem__(self):
return self.triples[idx]
def __len__(self):
return len(self.triples)
class BioDSRECorpus:
def __init__(
self,
medline_entities_linked_fname: Union[str, Path],
snomed_triples_dir: Union[str, Path],
split: Optional[str] = None,
has_def: Optional[bool] = False
):
self.medline_entities_linked_fname = medline_entities_linked_fname
self.snomed_triples_dir = snomed_triples_dir
# map from split to its triples
self.triples: Dict[str, List[str, str, str]] = dict()
# entities across all splits
self.entities = set()
# identify inductive or transductive split
self.split = (split + ('_def' if has_def else '') + '-') if split == 'ind' else ''
# type information is required to prune the negative pairs
cui2sty_fname = Path(self.snomed_triples_dir) / 'cui2sty.json'
with open(cui2sty_fname) as rf:
self.cui2sty = json.load(rf)
for data_split in ['train', 'dev', 'test']:
fname = Path(self.snomed_triples_dir) / f'{self.split}{data_split}.tsv'
self.triples[data_split] = Triples(fname, cui2sty_fname)
self.entities.update(self.triples[data_split].entities)
base_dir = os.path.split(medline_entities_linked_fname)[0]
self.pos_fname = lambda split: os.path.join(
base_dir, f'{self.split}{split}_pos_idxs_linked.tsv'
)
self.neg_fname = lambda split: os.path.join(
base_dir, f'{self.split}{split}_neg_idxs_linked.tsv'
)
self.base_dir = base_dir
def iter_entities_linked_file(self):
with open(self.medline_entities_linked_fname) as rf:
idx = 0
for jsonl in tqdm(rf, 'Reading entities linked file ...'):
jsonl = jsonl.strip()
if not jsonl:
continue
jsonl = json.loads(jsonl)
text = jsonl["text"]
# transform mention into OpenNRE mention format
# NB. m[1] is the score of entity linking, we keep
# it for entity linking error analysis otherwise it
# can be ignored
jsonl["mentions"] = [
{
"id": m[0], "pos": m[2], "name": text[m[2][0]:m[2][1]]
} for m in jsonl["mentions"]
]
# keep mentions if at least 3 character length only
jsonl["mentions"] = [m for m in jsonl["mentions"] if len(m["name"]) >= 3]
if not jsonl["mentions"]:
continue
yield idx, jsonl
idx += 1
def search_pos_and_neg_instances(
self,
raw_neg_sample_size: int = 500,
corrupt_arg: bool = True,
remove_multimentions_sents: bool = True,
use_type_constraint: bool = True,
use_arg_constraint: bool = True
):
"""Read positive pairs from triples and search for them in size-2
permutations of entities linked file, with mentions field. These
pairs are created using the CUIs and matched against SNOMED-CT
pairs' CUIs.
"""
# list of positive instances containing the fact pair with CUIs
# e.g. pos_idxs['train'] = [(128, ('C0205253', 'C0014653')), (.., ..), ...]
pos_idxs: Dict[str, List[Tuple[int, Tuple[str, str]]]] = dict(
train=list(), dev=list(), test=list()
)
# similarly, list of all negatives
neg_idxs: Dict[str, List[Tuple[int, Tuple[str, str]]]] = dict(
train=list(), dev=list(), test=list()
)
# ----------------------------------------
# **TRAIN** pairs, inverse pairs and types
# ----------------------------------------
train_pairs = set(self.triples['train'].pair2rels.keys())
train_pairs_inv = {(t, h) for h, t in train_pairs}
train_pairs_types = set(self.triples['train'].pair2type.values())
# ----------------------------------------
# **DEV** pairs, inverse pairs and types
# ----------------------------------------
dev_pairs = set(self.triples['dev'].pair2rels.keys())
dev_pairs_inv = {(t, h) for h, t in dev_pairs}
dev_pairs_types = set(self.triples['dev'].pair2type.values())
# ----------------------------------------
# **TEST** pairs, inverse pairs and types
# ----------------------------------------
test_pairs = set(self.triples['test'].pair2rels.keys())
test_pairs_inv = {(t, h) for h, t in test_pairs}
test_pairs_types = set(self.triples['test'].pair2type.values())
#-----------------------------------------------------------------
pairs = train_pairs | dev_pairs | test_pairs
heads, tails = zip(*pairs)
heads, tails = set(heads), set(tails)
heads_list, tails_list = sorted(list(heads)), sorted(list(tails))
# combine all inverse pairs and types pairs
inv = train_pairs_inv | dev_pairs_inv | test_pairs_inv
types = train_pairs_types | dev_pairs_types | test_pairs_types
entities = sorted(list(self.entities))
################################################################
#
# 1. COLLECT RAW NEGATIVE SAMPLES FROM +ve GROUPS
#
################################################################
# similar to usual corruption mechanism in KGC, we remove head
# or tail entity with probability 0.5 and sample from all entities;
# depending on the number of entities we sample for generating
# negatives, the positive to negative ratio will change.
# Too less negatives will result in lesser (more constrained)
# negative examples, i.e., for the NA relation type. These are
# raw candidate negative pairs but not the final set, as we
# further prune based on the positive semantic type pairs and
# check if the resulting heads appeared in any of the heads
# across all the splits and same for the tails.
neg_pairs = dict(train=set(), dev=set(), test=set())
n_samples = raw_neg_sample_size
for split, split_pairs in (
('train', train_pairs),
('dev', dev_pairs),
('test', test_pairs)
):
for h, t in tqdm(
sorted(list(split_pairs)),
desc=f'Creating candidate negative triples from positives for {split} ...'
):
# choose left or right side to corrupt
h_or_t = random.choice([0, 1])
# corrupt the **TAIL**
if h_or_t:
if corrupt_arg:
neg_tails = random.choices(tails_list, k=n_samples)
else:
neg_tails = random.choices(entities, k=n_samples)
neg_pairs[split].update({(h, t_neg) for t_neg in neg_tails})
# corrupt the **HEAD**
else:
if corrupt_arg:
neg_heads = random.choices(heads_list, k=n_samples)
else:
neg_heads = random.choices(entities, k=n_samples)
neg_pairs[split].update({(h_neg, t) for h_neg in neg_heads})
# remove positive groups if created during negative sampling and inverses as well
neg_pairs[split] = (neg_pairs[split] - pairs) - inv
logger.info(f'collected {len(neg_pairs[split])} number of negative samples for {split}')
ntr, nval, nte = 0, 0, 0
nntr, nnval, nnte = 0, 0, 0
for idx, jsonl in self.iter_entities_linked_file():
if idx % 1000000 == 0 and idx > 0:
ntr += len(pos_idxs['train'])
nval += len(pos_idxs['dev'])
nte += len(pos_idxs['test'])
nntr += len(neg_idxs['train'])
nnval += len(neg_idxs['dev'])
nnte += len(neg_idxs['test'])
ptr = (nntr / (nntr + ntr)) * 100
pval = (nnval / (nnval + nval)) * 100
pte = (nnte / (nnte + nte)) * 100
logger.info(
f'[Progress @ {idx}] -- POSITIVE # train {ntr} / # dev {nval} / # test {nte}'
)
logger.info(
f'[Progress @ {idx}] -- NA # train {nntr} / # dev {nnval} / # test {nnte}'
)
logger.info(
f'[Progress @ {idx}] -- NA (%) # train {ptr:.2f} / # dev {pval:.2f} / # test {pte:.2f}'
)
# periodically dump the collected positive and negative samples
# for each split to clear the memory; it grows fast :)
for split in ['train', 'dev', 'test']:
with open(self.pos_fname(split), 'a') as wf:
save_items(pos_idxs[split], wf)
with open(self.neg_fname(split), 'a') as wf:
save_items(neg_idxs[split], wf)
# reset the split indices
pos_idxs[split] = list()
neg_idxs[split] = list()
################################################################
#
# 2. REMOVE ENTITIES APPEARING MORE THAN ONCE (IF SET)
#
################################################################
cui2count = collections.Counter([item['id'] for item in jsonl['mentions']])
# check if any entity is present more than once, drop this sentence
# akin to: https://github.com/suamin/umls-medline-distant-re/blob/master/data_utils/link_entities.py#L45
if remove_multimentions_sents:
if sum(cui2count.values()) > len(cui2count):
continue
cuis = set(cui2count.keys())
################################################################
#
# 3. FIND OVERLAPPING ENTITES WITH THOSE FROM SNOMED TRIPLES
#
################################################################
# NB. this intersection prunes out entities which are not covered in any of the splits
snomed_cuis = cuis.intersection(self.entities)
if not snomed_cuis:
continue
################################################################
#
# 4. CREATE SIZE 2 (PAIRS) PERMUTATIONS OF INTERSECTING ENTITIES
#
################################################################
# permutations of size for concepts in a sentence that has SNOMED CUIs
matching_snomed_permutations = set(itertools.permutations(snomed_cuis, 2))
################################################################
#
# 5. LEAKAGE CHECK: REMOVE INVERSE GROUPS FROM THESE PAIRS
#
################################################################
# first remove inverses, we are not modeling them
matching_snomed_permutations = matching_snomed_permutations - inv
if not matching_snomed_permutations:
continue
################################################################
#
# 6. CHECK INTERSECTING PAIRS in TRAIN, DEV, OR TEST
#
################################################################
# check if we have the matching pairs in any of the splits
pairs_in_train = matching_snomed_permutations.intersection(train_pairs)
pairs_in_dev = matching_snomed_permutations.intersection(dev_pairs)
pairs_in_test = matching_snomed_permutations.intersection(test_pairs)
################################################################
#
# 7. LEAKAGE CHECK - NO MATCHING PAIRS ACROSS THE SPLITS
#
################################################################
# unlikely, but we make sure that no pairs are seen across the split; even though
# the triples creation process has taken care of it, this is to be on the safe-side
try:
assert not (pairs_in_train & pairs_in_dev)
assert not (pairs_in_train & pairs_in_test)
assert not (pairs_in_dev & pairs_in_test)
except AssertionError:
continue
# gather the pairs that have been matched
pruned_snomed_permutations = pairs_in_train | pairs_in_dev | pairs_in_test
################################################################
#
# 8. NO MATCHING PAIRS; CANDIDATE FOR NEGATIVE (NA) CLASS
#
################################################################
# we find no positive pairs across all the splits, this sentence
# can be considered for not applicable (NA) relations
if not pruned_snomed_permutations:
################################################################
#
# 8a. APPLY PAIRS TYPE CONSTRAINT ON NEGATIVE GROUPS
#
################################################################
if use_type_constraint:
# we prune out pairs which do not respect the type constraint, i.e.,
# the negative pair's type (TYPE_HEAD, TYPE_TAIL) must appear in
# pairs' types from fact triples. Next, we check if such entities
# appeared as head / tail **somewhere** across all the splits of facts.
# this is regarded as argument-role constraint. This filters out
# many of easy NA samples, which model can learn by simple heuristics.
temp = set()
for ent1, ent2 in matching_snomed_permutations:
ent1t = self.cui2sty[ent1]
ent2t = self.cui2sty[ent2]
if (ent1t, ent2t) in types:
pair = (ent1, ent2)
elif (ent2t, ent1t) in types:
pair = (ent2, ent1)
else:
continue
temp.add(pair)
matching_snomed_permutations = temp
if not matching_snomed_permutations:
continue
################################################################
#
# 8b. APPLY PAIRS ARG ROLE CONSTRAINT ON NEGATIVE GROUPS
#
################################################################
if use_arg_constraint:
temp = set()
for ent1, ent2 in matching_snomed_permutations:
if ent1 in heads and ent2 in tails:
pair = (ent1, ent2)
elif ent1 in tails and ent2 in heads:
pair = (ent2, ent1)
else:
continue
temp.add(pair)
matching_snomed_permutations = temp
if not matching_snomed_permutations:
continue
################################################################
#
# 8c. FROM COLLECTED GROUPS MATCH WITH THE RAW ONES
#
################################################################
# here, we consider the resultant pairs and see if they overlap
# with our candidates generated by simple head / tail entity replacements
candid_neg_pairs_in_test = matching_snomed_permutations.intersection(neg_pairs['test'])
candid_neg_pairs_in_dev = matching_snomed_permutations.intersection(neg_pairs['dev'])
candid_neg_pairs_in_train = matching_snomed_permutations.intersection(neg_pairs['train'])
candid_neg_pairs = candid_neg_pairs_in_train | candid_neg_pairs_in_dev | candid_neg_pairs_in_test
# after all the filtration steps we didn't find anything then we skip this sentence
if not candid_neg_pairs:
continue
################################################################
#
# 8d. LEAKAGE CHECK - NO MATCHING PAIRS IN NEGATIVE SPLITS
#
################################################################
try:
assert not (candid_neg_pairs_in_train & candid_neg_pairs_in_dev)
assert not (candid_neg_pairs_in_train & candid_neg_pairs_in_test)
assert not (candid_neg_pairs_in_dev & candid_neg_pairs_in_test)
except AssertionError:
continue
# we might have multiple matches, so we only consider one pair to
# associate with this sentence as negative to avoid duplicate sents in NA
################################################################
#
# FINAL STEP WHICH COLLECTS THE **NEGATIVE** GROUPS
#
################################################################
if candid_neg_pairs_in_test:
pair = random.choice(sorted(list(candid_neg_pairs_in_test)))
neg_idxs['test'].append((idx, pair))
elif candid_neg_pairs_in_dev:
pair = random.choice(sorted(list(candid_neg_pairs_in_dev)))
neg_idxs['dev'].append((idx, pair))
elif candid_neg_pairs_in_train:
pair = random.choice(sorted(list(candid_neg_pairs_in_train)))
neg_idxs['train'].append((idx, pair))
else:
################################################################
#
# FINAL STEP WHICH COLLECTS THE **POSITIVE** GROUPS
#
################################################################
if pairs_in_test:
pair = random.choice(sorted(list(pairs_in_test)))
pos_idxs['test'].append((idx, pair))
elif pairs_in_dev:
pair = random.choice(sorted(list(pairs_in_dev)))
pos_idxs['dev'].append((idx, pair))
elif pairs_in_train:
pair = random.choice(sorted(list(pairs_in_train)))
pos_idxs['train'].append((idx, pair))
h, t = pair
# safety check
assert h in cui2count
assert t in cui2count
return ntr, nval, nte, nntr, nnval, nnte
def process_jsonl_with_pair(
self,
jsonl: str,
pair: Tuple[str, str],
canonical_only: bool = False,
kb: KnowledgeBase = None
):
h, t = pair
# look for multiple appearence of head / tail entities, discard such ambigious examples
mention_ids = [item['id'] for item in jsonl['mentions']]
counts = collections.Counter(mention_ids)
# make sure head and tail are present
assert h in counts
assert t in counts
head_mention, tail_mention = None, None
################################################################
#
# KEEP ONLY SENTENCES WITH SINGLE MENTION OF HEAD / TAIL
#
################################################################
# since multiple mentions can cause ambiguity, we only pick instances of single h/t mention
if counts[h] == 1 and counts[t] == 1:
other_mentions = list()
for item in jsonl['mentions']:
if canonical_only:
ent = kb.cui_to_entity[item['id']]
names = [ent.canonical_name.lower(),] + list(map(str.lower, ent.aliases))
names = set(names)
if item['name'].lower() not in names:
continue
if item['id'] == h:
head_mention = item
elif item['id'] == t:
tail_mention = item
else:
other_mentions.append(item)
if head_mention == None or tail_mention == None:
return None
return head_mention, tail_mention, other_mentions
else:
return None
def create_corpus(
self,
train_size: float = 0.7,
dev_size: float = 0.1,
sample: float = 1.0,
use_sent_level_noise: bool = False,
neg_prop: float = None,
remove_mention_overlaps: bool = True,
canonical_or_aliases_only: bool = True,
prune_frequent_bags: bool = True,
max_bag_size: int = 500,
prune_frequent_mentions: bool = True,
max_mention_freq: int = 1000,
min_rel_freq: int = 1,
include_other_mentions: bool = True,
dataset: str = 'med_distant'
):
"""Once positive and negative pairs have been read, we now create the corpora in
OpenNRE format with different sizes and proportion of negative samples.
"""
assert 0 < sample <= 1.0
if neg_prop is not None:
assert 0 < neg_prop <= 1.0
counts = dict()
if canonical_or_aliases_only:
kb = UmlsEntityLinker(name='scispacy_linker').kb
else:
kb = None
for split in ['test', 'dev', 'train']:
logger.info(f'Creating corpus for split {split} ...')
pos_idx2pair = read_idx_file(self.pos_fname(split))
pair2pos_idxs = collections.defaultdict(list)
for pos_idx, pair in pos_idx2pair.items():
pair2pos_idxs[pair].append(pos_idx)
logger.info(f'Found {len(pair2pos_idxs)} positive pairs in `{split}`')
logger.info('Pruning noisy (high-frequency) positive pairs ...')
################################################################
#
# PRUNE HIGH-FREQUENCY POSITIVE (+ve) BAGS
#
################################################################
if prune_frequent_bags:
# remove highly-frequent (non-informative) pairs
for pair in list(pair2pos_idxs.keys()):
if len(pair2pos_idxs[pair]) > max_bag_size:
del pair2pos_idxs[pair]
logger.info(f'Number of positive pairs after pruning = {len(pair2pos_idxs)}')
pos_idxs = list(pos_idx2pair.keys())
neg_idx2pair = read_idx_file(self.neg_fname(split))
pair2neg_idxs = collections.defaultdict(list)
for neg_idx, pair in neg_idx2pair.items():
pair2neg_idxs[pair].append(neg_idx)
logger.info(f'Found {len(pair2neg_idxs)} negative pairs in `{split}`')
logger.info('Pruning noisy (high-frequency) negative pairs ...')
################################################################
#
# PRUNE HIGH-FREQUENCY NEGATIVE (-ve) BAGS
#
################################################################
if prune_frequent_bags:
# remove highly-frequent (non-informative) pairs
for pair in list(pair2neg_idxs.keys()):
if len(pair2neg_idxs[pair]) > max_bag_size:
del pair2neg_idxs[pair]
logger.info(f'Number of negative pairs after pruning = {len(pair2neg_idxs)}')
neg_idxs = list(neg_idx2pair.keys())
################################################################
#
# SUBSAMPLING POSITIVE SAMPLES
#
################################################################
# subsample the positive proportion to len(pos) * sample
if sample < 1.0:
logger.info(f'Subsampling positive idxs ...')
pos_idxs = list(random.sample(pos_idxs, int(len(pos_idxs) * sample)))
else:
pos_idxs = list(pos_idxs)
random.shuffle(pos_idxs)
random.shuffle(neg_idxs)
################################################################
#
# ADJUST +ve TO -ve's RATIO IF NEG PROP SPECIFIED
#
################################################################
if neg_prop is not None:
n_pos, n_neg = len(pos_idxs), len(neg_idxs)
# if positives sample size bigger than negatives
if n_pos > n_neg:
m = int(n_neg / neg_prop)
k_pos = m - n_neg
pos_idxs = pos_idxs[:k_pos]
# else we have more negatives than positives
else:
m = int(n_pos / (1 - neg_prop))
k_neg = m - n_pos
neg_idxs = neg_idxs[:k_neg]
n_pos, n_neg = len(pos_idxs), len(neg_idxs)
# for fast lookup
pos_idxs = set(pos_idxs)
neg_idxs = set(neg_idxs)
################################################################
#
# CHECK THERE FOR OVERLAP BETWEEN POSITIVE AND NEGATIVE SAMPLES
#
################################################################
assert not pos_idxs.intersection(neg_idxs)
logger.info(f'number of positive {n_pos} and negative {n_neg} idxs')
triples = self.triples[split]
# go through positive examples
pair2rel = dict()
for pos_idx in pos_idxs:
pair = pos_idx2pair[pos_idx]
rels = triples.pair2rels[pair]
# when labeled with multiple relations, consider a random one
if len(rels) > 1:
rel = random.choice(rels)
else:
rel = rels[0]
pair2rel[pair] = rel
logger.info(f'number of facts = {len(pair2rel)}')
corpus = list()
n_pos, n_neg = 0, 0 # fact instances count and NA instances count
for idx, jsonl in self.iter_entities_linked_file():
if idx % 1000000 == 0 and idx > 0:
logger.info(
f'[Progress @ {idx}] Collected {len(corpus)} lines with pos: {n_pos} and neg: {n_neg}'
)
if idx in pos_idxs:
pair = pos_idx2pair[idx]
ret = self.process_jsonl_with_pair(jsonl, pair, canonical_or_aliases_only, kb)
if ret is None:
continue
head_mention, tail_mention, other_mentions = ret
################################################################
#
# APPLY SENTENCE LEVEL NOISE (cf. Amin et al., 2020)
#
################################################################
if use_sent_level_noise:
# choose left or right side to corrupt
h_or_t = random.choice([0, 1])
has_neg = False
for idx, m in enumerate(other_mentions):
# corrupt the tail
if h_or_t:
candid_neg_pair = head_mention['id'], m['id']
tail_mention_noise = m
head_mention_noise = None
# corrupt the head
else:
candid_neg_pair = m['id'], tail_mention['id']
head_mention_noise = m
tail_mention_noise = None
if candid_neg_pair not in pair2neg_idxs:
continue
else:
has_neg = True
example = {
'text': jsonl['text'],
'h': head_mention if h_or_t else head_mention_noise,
't': tail_mention if not h_or_t else tail_mention_noise,
'relation': 'NA'
}
if include_other_mentions:
example['o'] = other_mentions
corpus.append(example)
n_neg += 1
if not has_neg:
continue
rel = pair2rel[pair]
example = {
'text': jsonl['text'],
'h': head_mention,
't': tail_mention,
'relation': rel
}
if include_other_mentions:
example['o'] = other_mentions
corpus.append(example)
n_pos += 1
elif idx in neg_idxs and not use_sent_level_noise:
pair = neg_idx2pair[idx]
ret = self.process_jsonl_with_pair(jsonl, pair, canonical_or_aliases_only, kb)
if ret is None:
continue
head_mention, tail_mention, other_mentions = ret
example = {
'text': jsonl['text'],
'h': head_mention,
't': tail_mention,
'relation': 'NA'
}
if include_other_mentions:
example['o'] = other_mentions
corpus.append(example)
n_neg += 1
logger.info(f'Final = Collected {len(corpus)} lines with pos: {n_pos} and neg: {n_neg}')
output_fname = os.path.join(self.base_dir, f'{self.split}{dataset}_{split}.txt')
logger.info(f'Saving the collected corpus to output file {output_fname} ...')
random.shuffle(corpus)
with open(output_fname, 'w', encoding='utf-8', errors='ignore') as wf:
for line in corpus:
wf.write(json.dumps(line) + '\n')
counts[split] = {'npos': n_pos, 'nneg': n_neg}
if remove_mention_overlaps:
for src, tgt in [('train', 'dev'), ('train', 'test'), ('dev', 'test')]:
src_jsonl_fname = os.path.join(self.base_dir, f'{self.split}{dataset}_{src}.txt')
tgt_jsonl_fname = os.path.join(self.base_dir, f'{self.split}{dataset}_{tgt}.txt')
################################################################
#
# REMOVE MENTION LEVEL OVERLAPS (IF ANY)
#
################################################################
remove_overlapping_pairs(src_jsonl_fname, tgt_jsonl_fname, tgt)
if prune_frequent_mentions:
for split in ['train', 'dev', 'test']:
jsonl_fname = os.path.join(self.base_dir, f'{self.split}{dataset}_{split}.txt')
prune_by_type_mentions(
jsonl_fname, self.cui2sty, max_freq=max_mention_freq
)
# remove relations which have no sample in dev / test from train as well, use min rel freq as well
split2rels = dict()
all_rels = set()
for split in ['train', 'dev', 'test']:
rel2count = collections.Counter()
jsonl_fname = os.path.join(self.base_dir, f'{self.split}{dataset}_{split}.txt')
with open(jsonl_fname, encoding='utf-8', errors='ignore') as rf:
for line in rf:
line = line.strip()
if not line:
continue
line = json.loads(line)
rel2count.update([line['relation'],])
split2rels[split] = rel2count
all_rels.update(list(rel2count.keys()))
train_relations = {rel for rel, rel_count in split2rels['train'].items() if rel_count >= min_rel_freq}
dev_relations = {
rel for rel, rel_count in split2rels['dev'].items()
if all([rel_count >= min_rel_freq, rel in train_relations])
}
test_relations = {
rel for rel, rel_count in split2rels['test'].items()
if all([rel_count >= min_rel_freq, rel in train_relations])
}
keep = test_relations.intersection(dev_relations.intersection(train_relations))
rels_to_discard = all_rels - keep
for split in ['train', 'dev', 'test']:
jsonl_fname = os.path.join(self.base_dir, f'{self.split}{dataset}_{split}.txt')
n_pos, n_neg = prune_by_rels(jsonl_fname, rels_to_discard)
logger.info(f'Final corpus statistics ...')
logger.info(f'{split}: {n_pos} +ve and {n_neg} NA instances')
def save_items(items, wf):
n = len(items)
for _ in range(n):
idx, pair = items.pop()
wf.write('\t'.join([str(idx), ','.join(pair)]) + '\n')
def read_idx_file(fname):
idx2pair = dict()
with open(fname) as rf:
for line in tqdm(rf, desc=f'Reading idxs file {fname}'):
line = line.strip()
if not line:
continue
idx, pair = line.split('\t')
pair = tuple(pair.split(','))
idx2pair[int(idx)] = pair
return idx2pair
def read_triples(fname):
triples = set()
with open(fname) as rf:
for line in rf:
line = line.strip()
if not line:
continue
h, r, t = line.split('\t')
triples.add((h, r, t))
return triples
def read_triples_from_jsonl(fname):
triples = set()
with open(fname) as rf:
for line in rf:
line = line.strip()
if not line:
continue
line = json.loads(line)
h = line['h']['name']
r = line['relation']
t = line['t']['name']
triples.add((h, r, t))
return triples
def remove_overlapping_pairs(src_jsonl_fname, tgt_jsonl_fname, name):
triples_to_remove, pairs_to_remove = check_overlap(
read_triples_from_jsonl(src_jsonl_fname),
read_triples_from_jsonl(tgt_jsonl_fname),
name
)
orig = 0
updated = 0
updated_jsonls = list()
with open(src_jsonl_fname) as rf:
for jsonl in rf:
jsonl = jsonl.strip()
if not jsonl:
continue
line = json.loads(jsonl)
orig += 1
h = line['h']['name']
r = line['relation']
t = line['t']['name']
if (h, r, t) in triples_to_remove:
continue
elif (t, r, h) in triples_to_remove:
continue
elif (h, t) in pairs_to_remove:
continue
elif (t, h) in pairs_to_remove:
continue
updated_jsonls.append(jsonl)
updated += 1
logger.info(f'Removed {orig - updated} instances from {orig}')
with open(src_jsonl_fname, 'w', encoding='utf-8', errors='ignore') as wf:
for jsonl in updated_jsonls:
wf.write(jsonl + '\n')
def check_overlap(train_triples, test_triples, name):
train_triples_inv = {(t, r, h) for h, r, t in train_triples}
train_pairs = {(h, t) for h, _, t in train_triples}
train_pairs_inv = {(t, h) for h, t in train_pairs}
test_triples_inv = {(t, r, h) for h, r, t in test_triples}
test_pairs = {(h, t) for h, _, t in test_triples}
test_pairs_inv = {(t, h) for h, t in test_pairs}
inter_triples = train_triples & test_triples
union_triples = train_triples | test_triples
inter_triples_inv = train_triples_inv & test_triples
union_triples_inv = train_triples_inv | test_triples
inter_pairs = train_pairs & test_pairs
inter_pairs_inv = train_pairs_inv & test_pairs
triples_to_remove = inter_triples | inter_triples_inv
pairs_to_remove = inter_pairs | inter_pairs_inv
logger.info(f'Training/{name} intersection size: {len(inter_triples)}')
logger.info(f'Number of {name} triples in Training: '
f'{(len(inter_triples) / len(test_triples)) * 100:.2f}%')
logger.info(f'Inverse Training/{name} intersection size: {len(inter_triples_inv)}')
logger.info(f'Number of {name} triples in Inverse Training: '
f'{(len(inter_triples_inv) / len(test_triples)) * 100:.2f}%')
logger.info(f'Number of {name} triples in Training or Inverse Training: '
f'{(len(inter_triples | inter_triples_inv) / len(test_triples)) * 100:.2f}%')
logger.info(f'Number of {name} pairs in Training: '
f'{(len(inter_pairs) / len(test_pairs)) * 100:.2f}%')
logger.info(f'Number of {name} pairs in Inverse Training: '
f'{(len(inter_pairs_inv) / len(test_pairs)) * 100:.2f}%')
return triples_to_remove, pairs_to_remove
def prune_by_type_mentions(benchmark_split_file, cui2sty, max_freq=1000):
sty2mention = collections.defaultdict(set)
logging.info(f'Reading file {benchmark_split_file} for type mentions-based pruning ...')
with open(benchmark_split_file, encoding="utf-8", errors="ignore") as rf:
for line in rf: