holylovenia commited on
Commit
35e545a
·
verified ·
1 Parent(s): 54205fc

Upload tatabahasa.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. tatabahasa.py +156 -0
tatabahasa.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """
17
+ This test is a general test for Malay grammar. Contains 349 questions that may be reinforced with instructions.
18
+ """
19
+ import json
20
+ from pathlib import Path
21
+ from typing import Dict, List, Tuple
22
+
23
+ import datasets
24
+
25
+ from seacrowd.utils import schemas
26
+ from seacrowd.utils.configs import SEACrowdConfig
27
+ from seacrowd.utils.constants import Tasks, Licenses, TASK_TO_SCHEMA
28
+
29
+ _CITATION = None
30
+
31
+ _DATASETNAME = "tatabahasa"
32
+
33
+ _DESCRIPTION = """\
34
+ This test is a general test for Malay grammar. Contains 349 questions.
35
+ """
36
+
37
+ _HOMEPAGE = "https://github.com/mesolitica/malaysian-dataset/tree/master/llm-benchmark/tatabahasabm.tripod.com"
38
+
39
+ _LANGUAGES = ["zlm"]
40
+
41
+ _LICENSE = Licenses.UNLICENSE.value
42
+
43
+ _LOCAL = False
44
+
45
+ _URLS = {
46
+ _DATASETNAME: "https://raw.githubusercontent.com/mesolitica/malaysian-dataset/master/llm-benchmark/tatabahasabm.tripod.com/quiz-tatabahasa.jsonl",
47
+ }
48
+
49
+ _SUPPORTED_TASKS = [Tasks.COMMONSENSE_REASONING]
50
+
51
+ _SOURCE_VERSION = "1.0.0"
52
+
53
+ _SEACROWD_VERSION = "2024.06.20"
54
+
55
+
56
+ class TatabahasaDataset(datasets.GeneratorBasedBuilder):
57
+ """This test is a general test for Malay grammar. Contains 349 questions."""
58
+
59
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
60
+ SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
61
+
62
+ SEACROWD_SCHEMA = TASK_TO_SCHEMA[_SUPPORTED_TASKS[0]].lower()
63
+
64
+ BUILDER_CONFIGS = [
65
+ SEACrowdConfig(
66
+ name=f"{_DATASETNAME}_source",
67
+ version=SOURCE_VERSION,
68
+ description=f"{_DATASETNAME} source schema",
69
+ schema="source",
70
+ subset_id=f"{_DATASETNAME}",
71
+ ),
72
+ SEACrowdConfig(
73
+ name=f"{_DATASETNAME}_seacrowd_{SEACROWD_SCHEMA}",
74
+ version=SEACROWD_VERSION,
75
+ description=f"{_DATASETNAME} SEACrowd schema",
76
+ schema=f"seacrowd_{SEACROWD_SCHEMA}",
77
+ subset_id=f"{_DATASETNAME}",
78
+ ),
79
+ ]
80
+
81
+ DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_source"
82
+
83
+ def _info(self) -> datasets.DatasetInfo:
84
+
85
+ if self.config.schema == "source":
86
+ multi_choice = {"text" : datasets.Value("string"), "answer": datasets.Value("bool")}
87
+ features = datasets.Features({
88
+ "question" : datasets.Value("string"),
89
+ "instruction": datasets.Value("string"),
90
+ "choices": {
91
+ "A": multi_choice,
92
+ "B": multi_choice,
93
+ "C": multi_choice,
94
+ "D": multi_choice,
95
+ },
96
+ "website": datasets.Value("string")
97
+ })
98
+
99
+ elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA}":
100
+ features = schemas.qa_features
101
+ features["meta"] = {"website": datasets.Value("string")}
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
+ urls = _URLS[_DATASETNAME]
113
+ data_dir = dl_manager.download_and_extract(urls)
114
+
115
+ return [
116
+ datasets.SplitGenerator(
117
+ name=datasets.Split.TRAIN,
118
+ gen_kwargs={
119
+ "filepath": data_dir,
120
+ },
121
+ ),
122
+ ]
123
+
124
+ def _generate_examples(self, filepath: Path) -> Tuple[int, Dict]:
125
+ with open(filepath ,'r') as f:
126
+ data = [json.loads(line) for line in f]
127
+
128
+ if self.config.schema == "source":
129
+ for i in range(len(data)):
130
+ out = {
131
+ "question": data[i]["question"],
132
+ "instruction": data[i]["instruction"],
133
+ "choices": data[i]["choices"],
134
+ "website": data[i]["website"]
135
+ }
136
+ yield i, out
137
+
138
+ elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA}":
139
+ for i in range(len(data)):
140
+ out = {
141
+ "id": i + 1,
142
+ "question_id": None,
143
+ "document_id": None,
144
+ "question": data[i]["question"],
145
+ "type": "multiple_choice",
146
+ "choices": [
147
+ data[i]["choices"]["A"]["text"],
148
+ data[i]["choices"]["B"]["text"],
149
+ data[i]["choices"]["C"]["text"],
150
+ data[i]["choices"]["D"]["text"],
151
+ ],
152
+ "context": data[i]["instruction"],
153
+ "answer": [choice["text"] for choice in data[i]["choices"].values() if choice["answer"]],
154
+ "meta": {"website": data[i]["website"]},
155
+ }
156
+ yield i, out