-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprocessors.py
214 lines (156 loc) · 7.44 KB
/
processors.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
from __future__ import absolute_import
import os
from abc import ABC
try:
from model_utils import readfile, read_jsonl_file, get_dataset_examples
except ImportError:
from .model_utils import readfile, read_jsonl_file, get_dataset_examples
class InputExample(object):
"""A single training/test example for simple sequence classification."""
def __init__(self, guid, text_a=None, text_b=None, label=None, image=None):
"""Constructs a InputExample.
Args:
guid: Unique id for the example.
text_a: string. The untokenized text of the first sequence. For single
sequence tasks, only this sequence must be specified.
text_b: (Optional) string. The untokenized text of the second sequence.
Only must be specified for sequence pair tasks.
label: (Optional) string. The label of the example. This should be
specified for train and dev examples, but not for test examples.
"""
self.guid = guid
self.text_a = text_a
self.text_b = text_b
self.label = label
self.image = image
class DataProcessor(object):
"""Base class for data converters for sequence classification data sets."""
def get_train_examples(self, data_dir):
"""Gets a collection of `InputExample`s for the train set."""
raise NotImplementedError()
def get_test_examples(self, data_dir):
"""Gets a collection of `InputExample`s for the test set."""
raise NotImplementedError()
def get_dev_examples(self, data_dir):
"""Gets a collection of `InputExample`s for the dev set."""
raise NotImplementedError()
def get_labels(self):
"""Gets the list of labels for this data set."""
raise NotImplementedError()
class DataProcessorNLP(DataProcessor, ABC):
@classmethod
def _read_tsv(cls, input_file):
"""Reads a tab separated value file."""
return readfile(input_file)
class BaseProcessorNLP(DataProcessorNLP):
def get_train_examples(self, data_dir, file_name="train.txt"):
"""See base class."""
return self._create_examples(
self._read_tsv(os.path.join(data_dir, file_name)), "train")
def get_dev_examples(self, data_dir, file_name="valid.txt"):
"""See base class."""
return self._create_examples(
self._read_tsv(os.path.join(data_dir, file_name)), "dev")
def get_test_examples(self, data_dir, file_name="test.txt"):
"""See base class."""
return self._create_examples(
self._read_tsv(os.path.join(data_dir, file_name)), "test")
def get_labels(self):
raise NotImplementedError()
def _create_examples(self, lines, set_type):
examples = []
for i, (sentence, label) in enumerate(lines):
guid = "%s-%s" % (set_type, i)
text_a = ' '.join(sentence)
text_b = None
label = label
examples.append(InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
return examples
class ConllNerProcessor(BaseProcessorNLP):
"""Processor for the CoNLL-2003 data set."""
def get_labels(self):
return ["O", "B-MISC", "I-MISC", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC", "[CLS]", "[SEP]"]
class JNLNerProcessor(BaseProcessorNLP):
def get_labels(self):
return ["O", "B-DNA", "I-DNA", "B-RNA", "I-RNA", "B-protein", "I-protein", "B-cell_type", "I-cell_type",
"B-cell_line", "I-cell_line", "[CLS]", "[SEP]"]
class WNUT17NerProcessor(BaseProcessorNLP):
def get_labels(self):
return ["O", "B-location", "I-location", "B-group", "I-group", "B-corporation", "I-corporation", "B-person", "I-person", "B-product",
"I-product", "B-creative-work", "I-creative-work", "[CLS]", "[SEP]"]
class SimplifiedNerProcessor(BaseProcessorNLP):
def get_labels(self):
return ["O", "B", "I", "[CLS]", "[SEP]"]
class BC2NerProcessor(BaseProcessorNLP):
def get_labels(self):
return ["O", "B-GENE", "I-GENE", "[CLS]", "[SEP]"]
class BC4NerProcessor(BaseProcessorNLP):
def get_labels(self):
return ["O", "B-Chemical", "I-Chemical", "[CLS]", "[SEP]"]
class GedProcessor(BaseProcessorNLP):
""" Processor for GED TSV data """
def get_labels(self):
return ["c", "i", "[CLS]", "[SEP]"]
class BaseNLIProcessor(BaseProcessorNLP):
def get_train_examples(self, data_dir, file_name="train.jsonl"):
"""See base class."""
return self._create_examples(
self._read_jsonl(os.path.join(data_dir, file_name)), "train")
def get_dev_examples(self, data_dir, file_name="multinli_1.0_dev_matched.jsonl"):
"""See base class."""
return self._create_examples(
self._read_jsonl(os.path.join(data_dir, file_name)), "dev")
def get_test_examples(self, data_dir, file_name="multinli_1.0_dev_mismatched.jsonl"):
"""See base class."""
return self._create_examples(
self._read_jsonl(os.path.join(data_dir, file_name)), "test")
def _create_examples(self, lines, set_type):
examples = []
for i, (sentence1, sentence2, label) in enumerate(lines):
guid = "%s-%s" % (set_type, i)
examples.append(InputExample(guid=guid, text_a=sentence1, text_b=sentence2, label=label))
return examples
@classmethod
def _read_jsonl(cls, input_file):
return read_jsonl_file(input_file)
class MNLIProcessor(BaseNLIProcessor):
def get_labels(self):
return ["neutral", "entailment", "contradiction"]
class HANSProcessor(BaseNLIProcessor):
def get_labels(self):
return ["entailment", "non-entailment"]
def get_dev_examples(self, data_dir, file_name="valid.jsonl"):
return super().get_dev_examples(data_dir, file_name)
def get_test_examples(self, data_dir, file_name="valid.jsonl"):
return super().get_test_examples(data_dir, file_name)
class BaseProcessorImageClassification(DataProcessor):
dataset_name = ""
def get_test_examples(self, data_dir):
return self._create_examples(self._get_dataset_examples("test"), "test")
def get_dev_examples(self, data_dir):
return self._create_examples(self._get_dataset_examples("dev"), "dev")
def get_train_examples(self, data_dir):
return self._create_examples(self._get_dataset_examples("train"), "train")
def get_labels(self):
raise NotImplementedError()
def _create_examples(self, data, set_type):
examples = []
for i, (image, label) in enumerate(data):
guid = "%s-%s" % (set_type, i)
examples.append(InputExample(guid=guid, image=image, label=label))
return examples
@classmethod
def _get_dataset_examples(cls, task="train"):
return get_dataset_examples(cls.dataset_name, task)
class MNISTProcessor(BaseProcessorImageClassification):
dataset_name = "MNIST"
def get_labels(self):
return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
class CIFAR10Processor(BaseProcessorImageClassification):
dataset_name = "CIFAR10"
def get_labels(self):
return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
class CIFAR100Processor(BaseProcessorImageClassification):
dataset_name = "CIFAR100"
def get_labels(self):
return list(range(100))