|
| 1 | +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | + |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | + |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +from paddlenlp.transformers import RemBertTokenizer |
| 17 | +import csv |
| 18 | +from paddle.io import Dataset |
| 19 | + |
| 20 | +tokenization = RemBertTokenizer.from_pretrained('rembert') |
| 21 | + |
| 22 | + |
| 23 | +class InputExample(object): |
| 24 | + """ |
| 25 | + Use classes to store each example |
| 26 | + """ |
| 27 | + |
| 28 | + def __init__(self, guid, text_a, text_b=None, label=None): |
| 29 | + self.guid = guid |
| 30 | + self.text_a = text_a |
| 31 | + self.text_b = text_b |
| 32 | + self.label = label |
| 33 | + |
| 34 | + |
| 35 | +class MrpcProcessor(object): |
| 36 | + """Load the dataset and convert each example text to ids""" |
| 37 | + |
| 38 | + def get_train_examples(self, data_dir): |
| 39 | + return self._create_examples( |
| 40 | + self._read_tsv(os.path.join(data_dir, "train.tsv")), "train") |
| 41 | + |
| 42 | + def get_dev_examples(self, data_dir): |
| 43 | + return self._create_examples( |
| 44 | + self._read_tsv(os.path.join(data_dir, "dev_2k.tsv")), "dev") |
| 45 | + |
| 46 | + def get_test_examples(self, data_dir): |
| 47 | + return self._create_examples( |
| 48 | + self._read_tsv(os.path.join(data_dir, "test_2k.tsv")), "test") |
| 49 | + |
| 50 | + def get_labels(self): |
| 51 | + return ["0", "1"] |
| 52 | + |
| 53 | + def _create_examples(self, lines, set_type): |
| 54 | + examples = [] |
| 55 | + for (i, line) in enumerate(lines): |
| 56 | + if i == 0: |
| 57 | + continue |
| 58 | + guid = "%s-%s" % (set_type, i) |
| 59 | + text_a = tokenization(line[1])['input_ids'] |
| 60 | + text_b = tokenization(line[2])['input_ids'] |
| 61 | + label = int(line[3]) |
| 62 | + examples.append( |
| 63 | + InputExample( |
| 64 | + guid=guid, text_a=text_a, text_b=text_b, label=label)) |
| 65 | + return examples |
| 66 | + |
| 67 | + @classmethod |
| 68 | + def _read_tsv(cls, input_file, quotechar=None): |
| 69 | + """Reads a tab separated value file.""" |
| 70 | + with open(input_file, "r", encoding='utf-8') as f: |
| 71 | + reader = csv.reader(f, delimiter="\t", quotechar=quotechar) |
| 72 | + lines = [] |
| 73 | + for line in reader: |
| 74 | + lines.append(line) |
| 75 | + return lines |
| 76 | + |
| 77 | + |
| 78 | +class XNLIProcessor(object): |
| 79 | + """Load the dataset and convert each example text to ids""" |
| 80 | + |
| 81 | + def get_train_examples(self, data_dir): |
| 82 | + return self._create_examples( |
| 83 | + self._read_tsv(os.path.join(data_dir, "multinli.train.en.tsv")), |
| 84 | + "train") |
| 85 | + |
| 86 | + def get_dev_examples(self, data_dir): |
| 87 | + return self._create_examples( |
| 88 | + self._read_tsv(os.path.join(data_dir, "xnli.dev.tsv")), "dev") |
| 89 | + |
| 90 | + def get_test_examples(self, data_dir): |
| 91 | + return self._create_examples( |
| 92 | + self._read_tsv(os.path.join(data_dir, "xnli.test.tsv")), "test") |
| 93 | + |
| 94 | + def get_labels(self): |
| 95 | + return ["neutral", "entailment", "contradictory"] |
| 96 | + |
| 97 | + def _create_examples(self, lines, set_type): |
| 98 | + examples = [] |
| 99 | + for (i, line) in enumerate(lines): |
| 100 | + if i == 0: |
| 101 | + continue |
| 102 | + guid = "%s-%s" % (set_type, i) |
| 103 | + if set_type == 'train': |
| 104 | + text_a = ' '.join(line[0].strip().split(' ')) |
| 105 | + text_b = ' '.join(line[1].strip().split(' ')) |
| 106 | + text_a = tokenization(text_a)['input_ids'] |
| 107 | + text_b = tokenization(text_b)['input_ids'] |
| 108 | + label = self.get_labels().index(line[2].strip()) |
| 109 | + examples.append( |
| 110 | + InputExample( |
| 111 | + guid=guid, text_a=text_a, text_b=text_b, label=label)) |
| 112 | + else: |
| 113 | + text_a = ' '.join(line[6].strip().split(' ')) |
| 114 | + text_b = ' '.join(line[7].strip().split(' ')) |
| 115 | + if line[1] == 'contradiction': |
| 116 | + line[1] = 'contradictory' |
| 117 | + label = self.get_labels().index(line[1].strip()) |
| 118 | + text_a = tokenization(text_a)['input_ids'] |
| 119 | + text_b = tokenization(text_b)['input_ids'] |
| 120 | + examples.append( |
| 121 | + InputExample( |
| 122 | + guid=guid, text_a=text_a, text_b=text_b, label=label)) |
| 123 | + return examples |
| 124 | + |
| 125 | + @classmethod |
| 126 | + def _read_tsv(cls, input_file, quotechar=None): |
| 127 | + """Reads a tab separated value file.""" |
| 128 | + with open(input_file, "r", encoding='utf-8') as f: |
| 129 | + reader = csv.reader(f, delimiter="\t", quotechar=quotechar) |
| 130 | + lines = [] |
| 131 | + for line in reader: |
| 132 | + lines.append(line) |
| 133 | + return lines |
| 134 | + |
| 135 | + |
| 136 | +class DataGenerator(Dataset): |
| 137 | + """Data generator is used to feed features into dataloader.""" |
| 138 | + |
| 139 | + def __init__(self, features): |
| 140 | + super(DataGenerator, self).__init__() |
| 141 | + self.features = features |
| 142 | + |
| 143 | + def __getitem__(self, item): |
| 144 | + text_a = self.features[item].text_a |
| 145 | + text_b = self.features[item].text_b |
| 146 | + text_a_token_type_ids = [0] * len(text_a) |
| 147 | + text_b_token_type_ids = [1] * len(text_b) |
| 148 | + label = [self.features[item].label] |
| 149 | + |
| 150 | + return dict( |
| 151 | + text_a=text_a, |
| 152 | + text_b=text_b, |
| 153 | + text_a_token_type_ids=text_a_token_type_ids, |
| 154 | + text_b_token_type_ids=text_b_token_type_ids, |
| 155 | + label=label) |
| 156 | + |
| 157 | + def __len__(self): |
| 158 | + return len(self.features) |
0 commit comments