-
Notifications
You must be signed in to change notification settings - Fork 9
/
precompute_contextual.py
executable file
·66 lines (40 loc) · 2.17 KB
/
precompute_contextual.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
import json
import logging
import numpy as np
import spacy
from pytt_hf import toks2vecs
from pytt_hf import PYTT_CONFIG
from umls import umls_kb_st21pv as umls_kb
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%d-%b-%y %H:%M:%S')
def read_mm_converted(mm_set_path):
with open(mm_set_path, 'r') as json_f:
mm_set = json.load(json_f)
return list(mm_set['docs'])
if __name__ == '__main__':
split_label = 'train'
# split_label = 'dev'
# train_data = list(iterate_docs_converted('data/MedMentions/mm_converted.%s.json' % split_label))
mm_docs = read_mm_converted('data/MedMentions/st21pv/custom/mm_converted.%s.json' % split_label)
all_embeddings_info = []
for doc_idx, doc in enumerate(mm_docs):
logging.info('At doc %d - %d embeddings' % (doc_idx, len(all_embeddings_info)))
for sent_idx, sent in enumerate(doc['sentences']):
sent['ctx_vecs'] = toks2vecs(sent['tokens'], return_tokens=False)
assert len(sent['ctx_vecs']) == len(sent['tokens'])
# for ent_idx, ent in enumerate(sent['entities']):
for span_idx, span in enumerate(sent['spans']):
span_vecs = [sent['ctx_vecs'][i] for i in range(span['start'], span['end'])]
span_vec = np.array(span_vecs, dtype=np.float32).mean(axis=0)
if np.isnan(span_vec.sum()) or span_vec.sum() == 0:
continue
emb_info = (doc_idx, sent_idx, span_idx, span['cui'], span['st'], span_vec)
all_embeddings_info.append(emb_info)
logging.info('Writing Embeddings ...')
vecs_path = 'mm_st21pv.%s.%s.precomputed' % (split_label, PYTT_CONFIG['name'])
with open(vecs_path, 'w') as vecs_f:
for (doc_idx, sent_idx, ent_idx, ent_cui, ent_st, ent_vec) in all_embeddings_info:
ent_vec_str = ' '.join([str(round(v, 6)) for v in ent_vec.tolist()])
vecs_f.write('%d\t%d\t%d\t%s\t%s\t%s\n' % (doc_idx, sent_idx, ent_idx, ent_cui, ent_st, ent_vec_str))
logging.info('Written %s' % vecs_path)