-
Notifications
You must be signed in to change notification settings - Fork 83
/
data_loader.py
207 lines (166 loc) · 7.36 KB
/
data_loader.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
import json
import torch
from torch.utils.data import Dataset
from torch.nn.utils.rnn import pad_sequence
import numpy as np
import prettytable as pt
from gensim.models import KeyedVectors
from transformers import AutoTokenizer
import os
import utils
import requests
os.environ["TOKENIZERS_PARALLELISM"] = "false"
dis2idx = np.zeros((1000), dtype='int64')
dis2idx[1] = 1
dis2idx[2:] = 2
dis2idx[4:] = 3
dis2idx[8:] = 4
dis2idx[16:] = 5
dis2idx[32:] = 6
dis2idx[64:] = 7
dis2idx[128:] = 8
dis2idx[256:] = 9
class Vocabulary(object):
PAD = '<pad>'
UNK = '<unk>'
SUC = '<suc>'
def __init__(self):
self.label2id = {self.PAD: 0, self.SUC: 1}
self.id2label = {0: self.PAD, 1: self.SUC}
def add_label(self, label):
label = label.lower()
if label not in self.label2id:
self.label2id[label] = len(self.label2id)
self.id2label[self.label2id[label]] = label
assert label == self.id2label[self.label2id[label]]
def __len__(self):
return len(self.token2id)
def label_to_id(self, label):
label = label.lower()
return self.label2id[label]
def id_to_label(self, i):
return self.id2label[i]
def collate_fn(data):
bert_inputs, grid_labels, grid_mask2d, pieces2word, dist_inputs, sent_length, entity_text = map(list, zip(*data))
max_tok = np.max(sent_length)
sent_length = torch.LongTensor(sent_length)
max_pie = np.max([x.shape[0] for x in bert_inputs])
bert_inputs = pad_sequence(bert_inputs, True)
batch_size = bert_inputs.size(0)
def fill(data, new_data):
for j, x in enumerate(data):
new_data[j, :x.shape[0], :x.shape[1]] = x
return new_data
dis_mat = torch.zeros((batch_size, max_tok, max_tok), dtype=torch.long)
dist_inputs = fill(dist_inputs, dis_mat)
labels_mat = torch.zeros((batch_size, max_tok, max_tok), dtype=torch.long)
grid_labels = fill(grid_labels, labels_mat)
mask2d_mat = torch.zeros((batch_size, max_tok, max_tok), dtype=torch.bool)
grid_mask2d = fill(grid_mask2d, mask2d_mat)
sub_mat = torch.zeros((batch_size, max_tok, max_pie), dtype=torch.bool)
pieces2word = fill(pieces2word, sub_mat)
return bert_inputs, grid_labels, grid_mask2d, pieces2word, dist_inputs, sent_length, entity_text
class RelationDataset(Dataset):
def __init__(self, bert_inputs, grid_labels, grid_mask2d, pieces2word, dist_inputs, sent_length, entity_text):
self.bert_inputs = bert_inputs
self.grid_labels = grid_labels
self.grid_mask2d = grid_mask2d
self.pieces2word = pieces2word
self.dist_inputs = dist_inputs
self.sent_length = sent_length
self.entity_text = entity_text
def __getitem__(self, item):
return torch.LongTensor(self.bert_inputs[item]), \
torch.LongTensor(self.grid_labels[item]), \
torch.LongTensor(self.grid_mask2d[item]), \
torch.LongTensor(self.pieces2word[item]), \
torch.LongTensor(self.dist_inputs[item]), \
self.sent_length[item], \
self.entity_text[item]
def __len__(self):
return len(self.bert_inputs)
def process_bert(data, tokenizer, vocab):
bert_inputs = []
grid_labels = []
grid_mask2d = []
dist_inputs = []
entity_text = []
pieces2word = []
sent_length = []
for index, instance in enumerate(data):
if len(instance['sentence']) == 0:
continue
tokens = [tokenizer.tokenize(word) for word in instance['sentence']]
pieces = [piece for pieces in tokens for piece in pieces]
_bert_inputs = tokenizer.convert_tokens_to_ids(pieces)
_bert_inputs = np.array([tokenizer.cls_token_id] + _bert_inputs + [tokenizer.sep_token_id])
length = len(instance['sentence'])
_grid_labels = np.zeros((length, length), dtype=np.int)
_pieces2word = np.zeros((length, len(_bert_inputs)), dtype=np.bool)
_dist_inputs = np.zeros((length, length), dtype=np.int)
_grid_mask2d = np.ones((length, length), dtype=np.bool)
if tokenizer is not None:
start = 0
for i, pieces in enumerate(tokens):
if len(pieces) == 0:
continue
pieces = list(range(start, start + len(pieces)))
_pieces2word[i, pieces[0] + 1:pieces[-1] + 2] = 1
start += len(pieces)
for k in range(length):
_dist_inputs[k, :] += k
_dist_inputs[:, k] -= k
for i in range(length):
for j in range(length):
if _dist_inputs[i, j] < 0:
_dist_inputs[i, j] = dis2idx[-_dist_inputs[i, j]] + 9
else:
_dist_inputs[i, j] = dis2idx[_dist_inputs[i, j]]
_dist_inputs[_dist_inputs == 0] = 19
for entity in instance["ner"]:
index = entity["index"]
for i in range(len(index)):
if i + 1 >= len(index):
break
_grid_labels[index[i], index[i + 1]] = 1
_grid_labels[index[-1], index[0]] = vocab.label_to_id(entity["type"])
_entity_text = set([utils.convert_index_to_text(e["index"], vocab.label_to_id(e["type"]))
for e in instance["ner"]])
sent_length.append(length)
bert_inputs.append(_bert_inputs)
grid_labels.append(_grid_labels)
grid_mask2d.append(_grid_mask2d)
dist_inputs.append(_dist_inputs)
pieces2word.append(_pieces2word)
entity_text.append(_entity_text)
return bert_inputs, grid_labels, grid_mask2d, pieces2word, dist_inputs, sent_length, entity_text
def fill_vocab(vocab, dataset):
entity_num = 0
for instance in dataset:
for entity in instance["ner"]:
vocab.add_label(entity["type"])
entity_num += len(instance["ner"])
return entity_num
def load_data_bert(config):
with open('./data/{}/train.json'.format(config.dataset), 'r', encoding='utf-8') as f:
train_data = json.load(f)
with open('./data/{}/dev.json'.format(config.dataset), 'r', encoding='utf-8') as f:
dev_data = json.load(f)
with open('./data/{}/test.json'.format(config.dataset), 'r', encoding='utf-8') as f:
test_data = json.load(f)
tokenizer = AutoTokenizer.from_pretrained(config.bert_name, cache_dir="./cache/")
vocab = Vocabulary()
train_ent_num = fill_vocab(vocab, train_data)
dev_ent_num = fill_vocab(vocab, dev_data)
test_ent_num = fill_vocab(vocab, test_data)
table = pt.PrettyTable([config.dataset, 'sentences', 'entities'])
table.add_row(['train', len(train_data), train_ent_num])
table.add_row(['dev', len(dev_data), dev_ent_num])
table.add_row(['test', len(test_data), test_ent_num])
config.logger.info("\n{}".format(table))
config.label_num = len(vocab.label2id)
config.vocab = vocab
train_dataset = RelationDataset(*process_bert(train_data, tokenizer, vocab))
dev_dataset = RelationDataset(*process_bert(dev_data, tokenizer, vocab))
test_dataset = RelationDataset(*process_bert(test_data, tokenizer, vocab))
return (train_dataset, dev_dataset, test_dataset), (train_data, dev_data, test_data)