File size: 5,675 Bytes
6dc56c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# TODO: Address all TODOs and remove all explanatory comments
"""ted2020_tw_mt"""


import csv
import json
import os
import datasets


# TODO: Add BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = """\
@InProceedings{huggingface:dataset,
title = {中文 Aya dataset},
author={Heng-Shiou Sheu
},
year={2024}
}
"""

# You can copy an official description
_DESCRIPTION = """\
是一個精心策劃的資料集,源自 CohereForAI 的綜合 Aya 集合,特別關注繁體中文資料。
此資料集聚合了 CohereForAI/aya_collection、CohereForAI/aya_dataset 和 CohereForAI/aya_evaluation_suite 中的內容,
過濾掉除中文內容之外的所有內容,包括繁體中文與簡體中文。
"""

# TODO 請使用 MAC 讀取資料夾內容來做更新
_Subset_names = [
    'default'
 ]

# TODO: Add a link to an official homepage for the dataset here
_HOMEPAGE = "https://huggingface.co/Heng666"

_LICENSE = "apache-2.0"

# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
_URLS = {
    "aya_collection": "https://huggingface.co/datasets/CohereForAI/aya_collection",
    "aya_dataset": "https://huggingface.co/datasets/CohereForAI/aya_dataset",
    "evaluation_suite": "https://huggingface.co/datasets/CohereForAI/aya_evaluation_suite"
}

class ChineseAyaDatasetConfig(datasets.BuilderConfig):
    """BuilderConfig for Chinese Aya"""

    def __init__(self, subset, **kwargs):
        super().__init__(**kwargs)
        """
        Args:
            subset: subset, you want to load
            **kwargs: keyword arguments forwarded to super.
        """
        self.subset = subset


class ChineseAyaDatasetDataset(datasets.GeneratorBasedBuilder):
    """TODO: Short description of my dataset."""

    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIG_CLASS = ChineseAyaDatasetConfig
    
    BUILDER_CONFIGS = [
        ChineseAyaDatasetConfig(
            name=subset, 
            description=_DESCRIPTION, 
            subset=subset 
        )
        for subset in _Subset_names
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features({
                "inputs": datasets.Value("string"),
                "targets": datasets.Value("string"),
                "language": datasets.Value("string"),
                "language_code": datasets.Value("string"),
                "annotation_type": datasets.Value("string"),
                "user_id": datasets.Value("string"),
            }),
            homepage=_HOMEPAGE,
            citation=_CITATION,
            license=_LICENSE
        )

    def _split_generators(self, dl_manager):

        subset = self.config.subset
        
        files = {}
    
        train_path = os.path.join("train/", f"CohereForAI-{subset}-train.csv")
        files["train"] = train_path
        test_path = os.path.join("test", f"CohereForAI-{subset}-test.csv")
        files["test"] = test_path
        validation_path = os.path.join("validation", f"CohereForAI-{subset}-validation.csv")
        files["validation"] = validation_path

        try:
            data_dir = dl_manager.download_and_extract(files)
        except:
            files.pop("test")
            files.pop("validation")
            data_dir = dl_manager.download_and_extract(files)
            
        output = []
        if "train" in files:
            train = datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "filepath": data_dir["train"]
                }
            )
            output.append(train)

        if "test" in files:
            test = datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "filepath": data_dir["test"]
                }
            )
            output.append(test)
        
        if "validation" in files:
            validation = datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "filepath": data_dir["validation"]
                }
            )
            output.append(validation)

        return output

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, filepath):
        """Yields examples."""
        with open(filepath, encoding="utf-8") as f:
            reader = csv.reader(f, delimiter=",", quotechar='"')
            for id_, row in enumerate(reader):
                if id_ == 0:
                    continue
                yield id_, {
                    "inputs": row[0],
                    "targets": row[1],
                    "language": row[2],
                    "language_code": row[3],
                    "annotation_type": row[4],
                    "user_id": row[5],
                }