-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
133 lines (115 loc) · 4.86 KB
/
dataset.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
import torch
from torch.utils import data
data_dir = "./conll2003/"
class Input():
def __init__(self, guid, text, label=None, segment_ids=None):
self.guid = guid
self.text = text
self.label = label
self.segment_ids = segment_ids
def readfile(filename):
f = open(filename)
data = []
sentence = []
label = []
for line in f:
if len(line) == 0 or line.startswith('-DOCSTART') or line[0] == "\n":
if len(sentence) > 0:
data.append((sentence, label))
sentence = []
label = []
continue
splits = line.split(' ')
sentence.append(splits[0])
label.append(splits[-1][:-1])
if len(sentence) > 0:
data.append((sentence, label))
sentence = []
label = []
return data
vocab = {}
class CoNLL2003Processor():
@staticmethod
def _create_examples(lines, set_type):
examples = []
for i, (sentence, label) in enumerate(lines):
guid = "%s-%s" % (set_type, i)
for token in sentence:
if token not in vocab.keys():
vocab[token] = len(vocab)
text_a = ' '.join(sentence)
label = label
examples.append(Input(guid=guid, text=text_a, label=label))
return examples
def get_train(self):
return self._create_examples(readfile(data_dir + "train.txt"), "train")
def get_valid(self):
return self._create_examples(readfile(data_dir + "valid.txt"), "valid")
def get_test(self):
return self._create_examples(readfile(data_dir + "test.txt"), "test")
def get_labels(self):
return ["O", "B-MISC", "I-MISC", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC",
"[CLS]", "[SEP]", "X"]
class CoNLL2003DataSet(data.Dataset):
def __init__(self, data_list, tokenizer, label_map, max_len):
self.max_len = max_len
self.label_map = label_map
self.data_list = data_list
self.tokenizer = tokenizer
def __len__(self):
return len(self.data_list)
def __getitem__(self, idx):
input_example = self.data_list[idx]
text = input_example.text
label = input_example.label
word_tokens = ['[CLS]']
label_list = ['[CLS]']
label_mask = [0] # value in (0, 1) - 0 signifies invalid token
input_ids = [self.tokenizer.convert_tokens_to_ids('[CLS]')]
label_ids = [self.label_map['[CLS]']]
# iterate over individual tokens and their labels
for word, label in zip(text.split(), label):
tokenized_word = self.tokenizer.tokenize(word)
for token in tokenized_word:
word_tokens.append(token)
input_ids.append(self.tokenizer.convert_tokens_to_ids(token))
label_list.append(label)
label_ids.append(self.label_map[label])
label_mask.append(1)
# len(tokenized_word) > 1 only if it splits word in between, in which case
# the first token gets assigned NER tag and the remaining ones get assigned
# X
for i in range(1, len(tokenized_word)):
label_list.append('X')
label_ids.append(self.label_map['X'])
label_mask.append(0)
assert len(word_tokens) == len(label_list) == len(input_ids) == len(label_ids) == len(
label_mask)
if len(word_tokens) >= self.max_len:
word_tokens = word_tokens[:(self.max_len - 1)]
label_list = label_list[:(self.max_len - 1)]
input_ids = input_ids[:(self.max_len - 1)]
label_ids = label_ids[:(self.max_len - 1)]
label_mask = label_mask[:(self.max_len - 1)]
assert len(word_tokens) < self.max_len, len(word_tokens)
word_tokens.append('[SEP]')
label_list.append('[SEP]')
input_ids.append(self.tokenizer.convert_tokens_to_ids('[SEP]'))
label_ids.append(self.label_map['[SEP]'])
label_mask.append(0)
assert len(word_tokens) == len(label_list) == len(input_ids) == len(label_ids) == len(
label_mask)
sentence_id = [0 for _ in input_ids]
attention_mask = [1 for _ in input_ids]
while len(input_ids) < self.max_len:
input_ids.append(0)
label_ids.append(self.label_map['X'])
attention_mask.append(0)
sentence_id.append(0)
label_mask.append(0)
assert len(word_tokens) == len(label_list)
assert len(input_ids) == len(label_ids) == len(attention_mask) == len(sentence_id) == len(
label_mask) == self.max_len, len(input_ids)
# return word_tokens, label_list,
return torch.LongTensor(input_ids), torch.LongTensor(label_ids), torch.LongTensor(
attention_mask), torch.LongTensor(sentence_id), torch.BoolTensor(label_mask)