Create data.py
Browse files
data.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
import tasksource
|
3 |
+
|
4 |
+
|
5 |
+
def parse(query: str):
|
6 |
+
"""
|
7 |
+
Parses a query of the form 'key1=value1,key2=value2,...' into a dictionary.
|
8 |
+
"""
|
9 |
+
result = {}
|
10 |
+
for kv in query.split(","):
|
11 |
+
parts = kv.split("=")
|
12 |
+
if parts[1].isdigit():
|
13 |
+
result[parts[0]] = int(parts[1])
|
14 |
+
elif parts[1].replace(".", "", 1).isdigit():
|
15 |
+
result[parts[0]] = float(parts[1])
|
16 |
+
|
17 |
+
result[parts[0]] = parts[1]
|
18 |
+
|
19 |
+
return result
|
20 |
+
|
21 |
+
|
22 |
+
class Dataset(datasets.GeneratorBasedBuilder):
|
23 |
+
|
24 |
+
builder_configs = {}
|
25 |
+
|
26 |
+
@property
|
27 |
+
def generators(self):
|
28 |
+
register_all_artifacts()
|
29 |
+
if not hasattr(self, "_generators") or self._generators is None:
|
30 |
+
recipe = fetch(self.config.name)
|
31 |
+
if recipe is None:
|
32 |
+
args = parse(self.config.name)
|
33 |
+
if "type" not in args:
|
34 |
+
args["type"] = "common_recipe"
|
35 |
+
recipe = Artifact.from_dict(args)
|
36 |
+
self._generators = recipe()
|
37 |
+
return self._generators
|
38 |
+
|
39 |
+
def _info(self):
|
40 |
+
return datasets.DatasetInfo()
|
41 |
+
|
42 |
+
def _split_generators(self, _):
|
43 |
+
return ["train","validation","test"]
|
44 |
+
|
45 |
+
def _generate_examples(self, split_name):
|
46 |
+
generator = self.generators[split_name]
|
47 |
+
for i, row in enumerate(generator):
|
48 |
+
yield i, row
|
49 |
+
|
50 |
+
def _download_and_prepare(self, dl_manager, verification_mode, **prepare_splits_kwargs):
|
51 |
+
result = super()._download_and_prepare(dl_manager, "no_checks", **prepare_splits_kwargs)
|
52 |
+
return result
|