-
Notifications
You must be signed in to change notification settings - Fork 1
/
spans.py
185 lines (151 loc) · 8.1 KB
/
spans.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
from itertools import compress
import numpy as np
import torch
class TopicSpans:
def __init__(self, config, data, topic_num, docs_embeddings=None, docs_lengths=None, is_training=True):
self.config = config
self.data = data
self.is_training = is_training
self.num_tokens = 0
# origin idx
self.doc_ids = []
self.sentence_id = []
self.origin_start = []
self.origin_end = []
self.width = []
self.combined_ids = []
self.span_texts = []
self.text = []
self.lemma = []
# bert idx
self.segment_ids = []
self.bert_start = []
self.bert_end = []
self.labels = []
# embeddings
self.start_end_embeddings = []
self.continuous_embeddings = []
self.knowledge_start_end_embeddings = []
self.knowledge_continuous_embeddings = []
self.knowledge_width = []
self.knowledge_text = []
self.get_all_spans_from_topic(data, topic_num, docs_embeddings, docs_lengths)
self.create_tensor()
def set_span_labels(self):
self.labels = self.data.get_candidate_labels(self.doc_ids, self.origin_start, self.origin_end)
def create_tensor(self):
self.doc_ids = np.asarray(self.doc_ids)
self.sentence_id = torch.tensor(self.sentence_id)
self.origin_start = torch.tensor(self.origin_start)
self.origin_end = torch.tensor(self.origin_end)
self.width = torch.stack(self.width)
self.segment_ids = torch.tensor(self.segment_ids)
self.bert_start = torch.tensor(self.bert_start)
self.bert_end = torch.tensor(self.bert_end)
if len(self.start_end_embeddings) > 0:
self.start_end_embeddings = torch.stack(self.start_end_embeddings)
device = self.start_end_embeddings.device
self.width = self.width.to(device)
def get_docs_candidate(self, original_tokens, bert_start_end):
max_span_width = self.config['max_mention_span']
num_tokens = len(original_tokens)
sentences = torch.tensor([x[0] for x in original_tokens])
# Find all possible spans up to max_span_width in the same sentence
candidate_starts = torch.tensor(range(num_tokens)).unsqueeze(1).repeat(1, max_span_width)
candidate_ends = candidate_starts + torch.tensor(range(max_span_width)).unsqueeze(0)
candidate_start_sentence_indices = sentences.unsqueeze(1).repeat(1, max_span_width)
padded_sentence_map = torch.cat((sentences, sentences[-1].repeat(max_span_width)))
candidate_end_sentence_indices = torch.stack(
list(padded_sentence_map[i:i + max_span_width] for i in range(num_tokens)))
candidate_mask = (candidate_start_sentence_indices == candidate_end_sentence_indices) * (
candidate_ends < num_tokens)
flattened_candidate_mask = candidate_mask.view(-1)
candidate_starts = candidate_starts.view(-1)[flattened_candidate_mask]
candidate_ends = candidate_ends.view(-1)[flattened_candidate_mask]
sentence_span = candidate_start_sentence_indices.view(-1)[flattened_candidate_mask]
# Original tokens ids
original_token_ids = torch.tensor([x[1] for x in original_tokens])
original_candidate_starts = original_token_ids[candidate_starts]
original_candidate_ends = original_token_ids[candidate_ends]
# Convert to BERT ids
bert_candidate_starts = bert_start_end[candidate_starts, 0]
bert_candidate_ends = bert_start_end[candidate_ends, 1]
return sentence_span, (original_candidate_starts, original_candidate_ends), \
(bert_candidate_starts, bert_candidate_ends)
def get_all_spans_from_topic(self, data, topic_num, docs_embeddings, docs_length):
# doc names may appear more than once if the doc was splitted into segments
doc_names = data.topics_list_of_docs[topic_num]
for i in range(len(doc_names)):
doc_id = doc_names[i]
original_tokens = data.topics_origin_tokens[topic_num][i]
bert_start_end = data.topics_start_end_bert[topic_num][i]
if self.is_training: # Filter only the validated sentences according to Cybulska setup
filt = [x[-1] for x in original_tokens]
bert_start_end = bert_start_end[filt]
original_tokens = list(compress(original_tokens, filt))
if not original_tokens:
continue
self.num_tokens += len(original_tokens)
sentence_span, original_candidates, bert_candidates = self.get_docs_candidate(original_tokens,
bert_start_end)
original_candidate_starts, original_candidate_ends = original_candidates
# Get actual span texts corresponding to the embeddings
sid_list = sentence_span.tolist()
start_list = original_candidate_starts.tolist()
end_list = original_candidate_ends.tolist()
keys = [(x[0], x[1]) for x in original_tokens]
original_token_texts = [x[2] for x in original_tokens]
lookup = dict(zip(keys, original_token_texts))
spans = []
for start, end, sid in zip(start_list, end_list, sid_list):
text = ""
for idx in range(start, end + 1):
if (sid, idx) in lookup:
text += lookup[(sid, idx)] + " "
spans.append(text)
self.span_texts.extend(spans)
# token_text = np.asarray([x[2] for x in original_tokens])
# update origin idx
self.doc_ids.extend([doc_id] * len(sentence_span))
self.sentence_id.extend(sentence_span)
self.origin_start.extend(original_candidate_starts)
self.origin_end.extend(original_candidate_ends)
self.width.extend(original_candidate_ends - original_candidate_starts)
# update bert idx
bert_candidate_starts, bert_candidate_ends = bert_candidates
self.segment_ids.extend([i] * len(sentence_span))
self.bert_start.extend(bert_candidate_starts)
self.bert_end.extend(bert_candidate_ends)
# add span embeddings
if docs_embeddings is not None:
doc_embeddings = docs_embeddings[i][torch.tensor(range(docs_length[i]))] # remove padding
self.start_end_embeddings.extend(torch.cat((doc_embeddings[bert_candidate_starts],
doc_embeddings[bert_candidate_ends]), dim=1))
continuous_tokens_embedding, lengths = self.get_all_token_embedding(doc_embeddings,
bert_candidate_starts,
bert_candidate_ends)
self.continuous_embeddings.extend(continuous_tokens_embedding)
def get_all_token_embedding(self, embedding, start, end):
span_embeddings, length = [], []
for s, e in zip(start, end):
indices = torch.tensor(range(s, e + 1))
span_embeddings.append(embedding[indices])
length.append(len(indices))
return span_embeddings, length
def prune_spans(self, indices):
# origin idx
self.doc_ids = self.doc_ids[indices]
self.sentence_id = self.sentence_id[indices]
self.origin_start = self.origin_start[indices]
self.origin_end = self.origin_end[indices]
self.width = self.width[indices]
# bert idx
self.segment_ids = self.segment_ids[indices]
self.bert_start = self.bert_start[indices]
self.bert_end = self.bert_end[indices]
self.labels = self.labels[indices]
self.span_texts = [self.span_texts[k] for k in indices]
# embeddings
if len(self.start_end_embeddings) > 0:
self.start_end_embeddings = self.start_end_embeddings[indices]
self.continuous_embeddings = [self.continuous_embeddings[x] for x in indices]