|
| 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 | +import numpy as np |
| 15 | + |
| 16 | +from paddle.metric import Metric, Accuracy |
| 17 | +from paddlenlp.transformers import ErnieForSequenceClassification, ErnieTokenizer |
| 18 | +from paddlenlp.transformers import BertForSequenceClassification, BertTokenizer |
| 19 | + |
| 20 | +MODEL_CLASSES = { |
| 21 | + "ernie": (ErnieForSequenceClassification, ErnieTokenizer), |
| 22 | + "bert": (BertForSequenceClassification, BertTokenizer) |
| 23 | +} |
| 24 | + |
| 25 | +METRIC_CLASSES = { |
| 26 | + "afqmc": Accuracy, |
| 27 | + "tnews": Accuracy, |
| 28 | + "iflytek": Accuracy, |
| 29 | + "ocnli": Accuracy, |
| 30 | + "cmnli": Accuracy, |
| 31 | + "cluewsc2020": Accuracy, |
| 32 | + "csl": Accuracy, |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +def convert_example(example, |
| 37 | + tokenizer, |
| 38 | + label_list, |
| 39 | + max_seq_length=512, |
| 40 | + is_test=False): |
| 41 | + """convert a glue example into necessary features""" |
| 42 | + if not is_test: |
| 43 | + # `label_list == None` is for regression task |
| 44 | + label_dtype = "int64" if label_list else "float32" |
| 45 | + # Get the label |
| 46 | + label = example['label'] |
| 47 | + label = np.array([label], dtype=label_dtype) |
| 48 | + # Convert raw text to feature |
| 49 | + if 'sentence' in example: |
| 50 | + example = tokenizer(example['sentence'], max_seq_len=max_seq_length) |
| 51 | + elif 'sentence1' in example: |
| 52 | + example = tokenizer( |
| 53 | + example['sentence1'], |
| 54 | + text_pair=example['sentence2'], |
| 55 | + max_seq_len=max_seq_length) |
| 56 | + elif 'keyword' in example: # CSL |
| 57 | + sentence1 = " ".join(example['keyword']) |
| 58 | + example = tokenizer( |
| 59 | + sentence1, text_pair=example['abst'], max_seq_len=max_seq_length) |
| 60 | + elif 'target' in example: # wsc |
| 61 | + text, query, pronoun, query_idx, pronoun_idx = example['text'], example[ |
| 62 | + 'target']['span1_text'], example['target']['span2_text'], example[ |
| 63 | + 'target']['span1_index'], example['target']['span2_index'] |
| 64 | + text_list = list(text) |
| 65 | + assert text[pronoun_idx:(pronoun_idx + len(pronoun) |
| 66 | + )] == pronoun, "pronoun: {}".format(pronoun) |
| 67 | + assert text[query_idx:(query_idx + len(query) |
| 68 | + )] == query, "query: {}".format(query) |
| 69 | + if pronoun_idx > query_idx: |
| 70 | + text_list.insert(query_idx, "_") |
| 71 | + text_list.insert(query_idx + len(query) + 1, "_") |
| 72 | + text_list.insert(pronoun_idx + 2, "[") |
| 73 | + text_list.insert(pronoun_idx + len(pronoun) + 2 + 1, "]") |
| 74 | + else: |
| 75 | + text_list.insert(pronoun_idx, "[") |
| 76 | + text_list.insert(pronoun_idx + len(pronoun) + 1, "]") |
| 77 | + text_list.insert(query_idx + 2, "_") |
| 78 | + text_list.insert(query_idx + len(query) + 2 + 1, "_") |
| 79 | + text = "".join(text_list) |
| 80 | + example = tokenizer(text, max_seq_len=max_seq_length) |
| 81 | + |
| 82 | + if not is_test: |
| 83 | + return example['input_ids'], example['token_type_ids'], label |
| 84 | + else: |
| 85 | + return example['input_ids'], example['token_type_ids'] |
0 commit comments