|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 3 | +# Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# Lint as: python3 |
| 18 | + |
| 19 | +import json |
| 20 | +import os |
| 21 | + |
| 22 | +import datasets |
| 23 | +from datasets.tasks import QuestionAnsweringExtractive |
| 24 | + |
| 25 | +logger = datasets.logging.get_logger(__name__) |
| 26 | + |
| 27 | +_DESCRIPTION = """\ |
| 28 | +Duconv is a chinese conversation \ |
| 29 | +dataset, designed to evaluate the dialogue models. |
| 30 | +""" |
| 31 | + |
| 32 | +_URL = "https://bj.bcebos.com/paddlenlp/datasets/DuConv.zip" |
| 33 | + |
| 34 | + |
| 35 | +class DuconvConfig(datasets.BuilderConfig): |
| 36 | + """BuilderConfig for Duconv.""" |
| 37 | + |
| 38 | + def __init__(self, **kwargs): |
| 39 | + """BuilderConfig for Duconv. |
| 40 | +
|
| 41 | + Args: |
| 42 | + **kwargs: keyword arguments forwarded to super. |
| 43 | + """ |
| 44 | + super(DuconvConfig, self).__init__(**kwargs) |
| 45 | + |
| 46 | + |
| 47 | +class Duconv(datasets.GeneratorBasedBuilder): |
| 48 | + BUILDER_CONFIGS = [ |
| 49 | + DuconvConfig( |
| 50 | + name="DuConv", |
| 51 | + version=datasets.Version("1.0.0", ""), |
| 52 | + description=_DESCRIPTION, |
| 53 | + ), |
| 54 | + ] |
| 55 | + |
| 56 | + def _info(self): |
| 57 | + return datasets.DatasetInfo( |
| 58 | + description=_DESCRIPTION, |
| 59 | + features=datasets.Features({ |
| 60 | + "id": |
| 61 | + datasets.Value("string"), |
| 62 | + "goal": |
| 63 | + datasets.Sequence(datasets.Sequence(datasets.Value("string"))), |
| 64 | + "knowledge": |
| 65 | + datasets.Sequence(datasets.Sequence(datasets.Value("string"))), |
| 66 | + "conversation": |
| 67 | + datasets.Sequence(datasets.Value("string")), |
| 68 | + "history": |
| 69 | + datasets.Sequence(datasets.Value("string")), |
| 70 | + "response": |
| 71 | + datasets.Value("string"), |
| 72 | + }), |
| 73 | + # No default supervised_keys (as we have to pass both question |
| 74 | + # and context as input). |
| 75 | + supervised_keys=None, |
| 76 | + homepage="https://arxiv.org/pdf/1906.05572.pdf", |
| 77 | + ) |
| 78 | + |
| 79 | + def _split_generators(self, dl_manager): |
| 80 | + dl_dir = dl_manager.download_and_extract(_URL) |
| 81 | + |
| 82 | + return [ |
| 83 | + datasets.SplitGenerator(name="train", |
| 84 | + gen_kwargs={ |
| 85 | + "filepath": |
| 86 | + os.path.join(dl_dir, 'DuConv', |
| 87 | + 'train.txt'), |
| 88 | + }), |
| 89 | + datasets.SplitGenerator(name="dev", |
| 90 | + gen_kwargs={ |
| 91 | + "filepath": |
| 92 | + os.path.join(dl_dir, 'DuConv', |
| 93 | + 'dev.txt'), |
| 94 | + }), |
| 95 | + datasets.SplitGenerator(name="test_1", |
| 96 | + gen_kwargs={ |
| 97 | + "filepath": |
| 98 | + os.path.join(dl_dir, 'DuConv', |
| 99 | + 'test_1.txt'), |
| 100 | + }), |
| 101 | + datasets.SplitGenerator(name="test_2", |
| 102 | + gen_kwargs={ |
| 103 | + "filepath": |
| 104 | + os.path.join(dl_dir, 'DuConv', |
| 105 | + 'test_2.txt'), |
| 106 | + }), |
| 107 | + ] |
| 108 | + |
| 109 | + def _generate_examples(self, filepath): |
| 110 | + """This function returns the examples in the raw (text) form.""" |
| 111 | + logger.info("generating examples from = %s", filepath) |
| 112 | + key = 0 |
| 113 | + with open(filepath, 'r', encoding="utf-8") as fin: |
| 114 | + for line in fin: |
| 115 | + duconv = json.loads(line) |
| 116 | + |
| 117 | + goal = duconv["goal"] if "goal" in duconv.keys() else [[]] |
| 118 | + knowledge = duconv["knowledge"] if "knowledge" in duconv.keys( |
| 119 | + ) else [[]] |
| 120 | + conversation = duconv[ |
| 121 | + "conversation"] if "conversation" in duconv.keys() else [] |
| 122 | + history = duconv["history"] if "history" in duconv.keys( |
| 123 | + ) else [] |
| 124 | + response = duconv["response"] if "response" in duconv.keys( |
| 125 | + ) else "" |
| 126 | + |
| 127 | + yield key, { |
| 128 | + "id": str(key), |
| 129 | + "goal": goal, |
| 130 | + "knowledge": knowledge, |
| 131 | + "conversation": conversation, |
| 132 | + "history": history, |
| 133 | + "response": response, |
| 134 | + } |
| 135 | + key += 1 |
0 commit comments