holylovenia
commited on
Commit
•
a5b8b50
1
Parent(s):
22d4069
Upload vimmrc.py with huggingface_hub
Browse files
vimmrc.py
ADDED
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
|
16 |
+
import json
|
17 |
+
from pathlib import Path
|
18 |
+
from typing import Dict, List, Tuple
|
19 |
+
|
20 |
+
import datasets
|
21 |
+
|
22 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
23 |
+
from seacrowd.utils.constants import (SCHEMA_TO_FEATURES, TASK_TO_SCHEMA,
|
24 |
+
Licenses, Tasks)
|
25 |
+
|
26 |
+
_CITATION = """
|
27 |
+
@ARTICLE{vimmrc,
|
28 |
+
author={Nguyen, Kiet Van and Tran, Khiem Vinh and Luu, Son T. and Nguyen, Anh Gia-Tuan and Nguyen, Ngan Luu-Thuy},
|
29 |
+
journal={IEEE Access},
|
30 |
+
title={Enhancing Lexical-Based Approach With External Knowledge for Vietnamese Multiple-Choice Machine Reading Comprehension},
|
31 |
+
year={2020},
|
32 |
+
volume={8},
|
33 |
+
pages={201404-201417},
|
34 |
+
doi={10.1109/ACCESS.2020.3035701}}
|
35 |
+
"""
|
36 |
+
|
37 |
+
_DATASETNAME = "vimmrc"
|
38 |
+
|
39 |
+
_DESCRIPTION = """
|
40 |
+
ViMMRC, a challenging machine comprehension corpus with multiple-choice questions,
|
41 |
+
intended for research on the machine comprehension of Vietnamese text. This corpus
|
42 |
+
includes 2,783 multiple-choice questions and answers based on a set of 417 Vietnamese
|
43 |
+
texts used for teaching reading comprehension for 1st to 5th graders.
|
44 |
+
"""
|
45 |
+
|
46 |
+
_HOMEPAGE = "https://sites.google.com/uit.edu.vn/kietnv/datasets#h.1qeaynfs79d1"
|
47 |
+
|
48 |
+
_LANGUAGES = ["vie"]
|
49 |
+
|
50 |
+
_LICENSE = f"{Licenses.UNKNOWN.value} | The corpus is freely available at our website for research purposes."
|
51 |
+
|
52 |
+
_LOCAL = False
|
53 |
+
|
54 |
+
_URL = "https://drive.google.com/file/d/14Rq-YANUv8qyi4Ze8ReEAEu_uxgcV_Yk/view" # ~2mb
|
55 |
+
|
56 |
+
_SUPPORTED_TASKS = [Tasks.COMMONSENSE_REASONING]
|
57 |
+
_SEACROWD_SCHEMA = f"seacrowd_{TASK_TO_SCHEMA[_SUPPORTED_TASKS[0]].lower()}" # qa
|
58 |
+
|
59 |
+
_SOURCE_VERSION = "1.0.0"
|
60 |
+
|
61 |
+
_SEACROWD_VERSION = "2024.06.20"
|
62 |
+
|
63 |
+
|
64 |
+
class ViMMRCDataset(datasets.GeneratorBasedBuilder):
|
65 |
+
"""A Vietnamese machine comprehension corpus with multiple-choice questions"""
|
66 |
+
|
67 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
68 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
69 |
+
|
70 |
+
BUILDER_CONFIGS = [
|
71 |
+
SEACrowdConfig(
|
72 |
+
name=f"{_DATASETNAME}_source",
|
73 |
+
version=SOURCE_VERSION,
|
74 |
+
description=f"{_DATASETNAME} source schema",
|
75 |
+
schema="source",
|
76 |
+
subset_id=_DATASETNAME,
|
77 |
+
),
|
78 |
+
SEACrowdConfig(
|
79 |
+
name=f"{_DATASETNAME}_{_SEACROWD_SCHEMA}",
|
80 |
+
version=SEACROWD_VERSION,
|
81 |
+
description=f"{_DATASETNAME} SEACrowd schema",
|
82 |
+
schema=_SEACROWD_SCHEMA,
|
83 |
+
subset_id=_DATASETNAME,
|
84 |
+
),
|
85 |
+
]
|
86 |
+
|
87 |
+
DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_source"
|
88 |
+
|
89 |
+
def _info(self) -> datasets.DatasetInfo:
|
90 |
+
if self.config.schema == "source":
|
91 |
+
features = datasets.Features(
|
92 |
+
{
|
93 |
+
"file_path": datasets.Value("string"),
|
94 |
+
"article": datasets.Value("string"),
|
95 |
+
"question": datasets.Value("string"),
|
96 |
+
"choices": datasets.Sequence(datasets.Value("string")),
|
97 |
+
"answer": datasets.Value("string"),
|
98 |
+
}
|
99 |
+
)
|
100 |
+
elif self.config.schema == _SEACROWD_SCHEMA:
|
101 |
+
features = SCHEMA_TO_FEATURES[TASK_TO_SCHEMA[_SUPPORTED_TASKS[0]]] # qa_features
|
102 |
+
|
103 |
+
return datasets.DatasetInfo(
|
104 |
+
description=_DESCRIPTION,
|
105 |
+
features=features,
|
106 |
+
homepage=_HOMEPAGE,
|
107 |
+
license=_LICENSE,
|
108 |
+
citation=_CITATION,
|
109 |
+
)
|
110 |
+
|
111 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
112 |
+
"""Returns SplitGenerators."""
|
113 |
+
# check if gdown is installed
|
114 |
+
try:
|
115 |
+
import gdown
|
116 |
+
except ImportError as err:
|
117 |
+
raise ImportError("Please install `gdown` to enable reliable data download from google drive.") from err
|
118 |
+
|
119 |
+
# download data from gdrive
|
120 |
+
output_dir = Path.cwd() / "data" / "vimmrc"
|
121 |
+
output_dir.mkdir(parents=True, exist_ok=True)
|
122 |
+
output_file = output_dir / "vimmrc.zip"
|
123 |
+
if not output_file.exists():
|
124 |
+
gdown.download(_URL, str(output_file), fuzzy=True)
|
125 |
+
else:
|
126 |
+
print(f"File already downloaded: {str(output_file)}")
|
127 |
+
|
128 |
+
# extract data
|
129 |
+
data_dir = Path(dl_manager.extract(output_file)) / "ViMMRC"
|
130 |
+
|
131 |
+
return [
|
132 |
+
datasets.SplitGenerator(
|
133 |
+
name=datasets.Split.TRAIN,
|
134 |
+
gen_kwargs={
|
135 |
+
"data_dir": data_dir / "train",
|
136 |
+
},
|
137 |
+
),
|
138 |
+
datasets.SplitGenerator(
|
139 |
+
name=datasets.Split.VALIDATION,
|
140 |
+
gen_kwargs={
|
141 |
+
"data_dir": data_dir / "dev",
|
142 |
+
},
|
143 |
+
),
|
144 |
+
datasets.SplitGenerator(
|
145 |
+
name=datasets.Split.TEST,
|
146 |
+
gen_kwargs={
|
147 |
+
"data_dir": data_dir / "test",
|
148 |
+
},
|
149 |
+
),
|
150 |
+
]
|
151 |
+
|
152 |
+
def _generate_examples(self, data_dir: Path) -> Tuple[int, Dict]:
|
153 |
+
"""Yields examples as (key, example) tuples."""
|
154 |
+
# a data_dir consists of several json files
|
155 |
+
json_files = sorted(list(data_dir.glob("*.json")))
|
156 |
+
|
157 |
+
key = 0
|
158 |
+
for json_file in json_files:
|
159 |
+
with open(json_file, "r", encoding="utf-8") as file:
|
160 |
+
# load per json file
|
161 |
+
data = json.load(file)
|
162 |
+
assert len(data["questions"]) == len(data["options"]) == len(data["answers"]), f"Mismatched data length on {str(json_file)}"
|
163 |
+
|
164 |
+
for idx, question in enumerate(data["questions"]):
|
165 |
+
|
166 |
+
# get answer based on the answer key
|
167 |
+
if data["answers"][idx] == "A":
|
168 |
+
answer = data["options"][idx][0]
|
169 |
+
elif data["answers"][idx] == "B":
|
170 |
+
answer = data["options"][idx][1]
|
171 |
+
elif data["answers"][idx] == "C":
|
172 |
+
answer = data["options"][idx][2]
|
173 |
+
elif data["answers"][idx] == "D":
|
174 |
+
answer = data["options"][idx][3]
|
175 |
+
|
176 |
+
if self.config.schema == "source":
|
177 |
+
yield key, {
|
178 |
+
"file_path": str(json_file),
|
179 |
+
"article": data["article"],
|
180 |
+
"question": question,
|
181 |
+
"choices": data["options"][idx],
|
182 |
+
"answer": answer,
|
183 |
+
}
|
184 |
+
key += 1
|
185 |
+
|
186 |
+
elif self.config.schema == _SEACROWD_SCHEMA:
|
187 |
+
yield key, {
|
188 |
+
"id": key,
|
189 |
+
"question_id": None,
|
190 |
+
"document_id": str(json_file),
|
191 |
+
"question": question,
|
192 |
+
"type": "multiple_choice",
|
193 |
+
"choices": data["options"][idx],
|
194 |
+
"context": data["article"],
|
195 |
+
"answer": [answer],
|
196 |
+
"meta": None,
|
197 |
+
}
|
198 |
+
key += 1
|