MosenA commited on
Commit
f2ad3a8
·
verified ·
1 Parent(s): ab6f2e5

Update joud_sample.py

Browse files
Files changed (1) hide show
  1. joud_sample.py +150 -0
joud_sample.py CHANGED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import csv
2
+ import json
3
+ import os
4
+
5
+ import datasets
6
+ import gzip
7
+
8
+ # TODO: Add BibTeX citation
9
+ # Find for instance the citation on arxiv or on the dataset repo/website
10
+ _CITATION = """\
11
+ @InProceedings{huggingface:dataset,
12
+ title = {A great new dataset},
13
+ author={huggingface, Inc.
14
+ },
15
+ year={2020}
16
+ }
17
+ """
18
+
19
+ # TODO: Add description of the dataset here
20
+ # You can copy an official description
21
+ _DESCRIPTION = """\
22
+ This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
23
+ """
24
+
25
+ # TODO: Add a link to an official homepage for the dataset here
26
+ _HOMEPAGE = ""
27
+
28
+ # TODO: Add the licence for the dataset here if you can find it
29
+ _LICENSE = ""
30
+
31
+ # TODO: Add link to the official dataset URLs here
32
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
33
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
34
+ _URLS = "https://huggingface.co/datasets/asas-ai/joud_sample/resolve/main/ar_{SUBSET}{split}_{index}.json"
35
+
36
+ _N_FILES_PER_SPLIT = {
37
+ 'abw': {'train':4 },
38
+ 'mc4': {'train':4 },
39
+ 'wiki':{'train':3 },
40
+ 'cc100': {'train': 4 },
41
+ 'hplt': {'train': 4},
42
+ 'opus': {'train': 6},
43
+ 'tashkeela': {'train': 1},
44
+ 'oscar': {'train': 4},
45
+ }
46
+
47
+ _SUBSETS = ['abw', 'mc4', 'wiki', 'cc100', 'hplt', 'opus', 'tashkeela', 'oscar' ]
48
+ # TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
49
+
50
+ class JoudConfig(datasets.BuilderConfig):
51
+ """ Builder config for Joud Dataset. """
52
+
53
+ def __init__(self, subset, **kwargs):
54
+ super(JoudConfig, self).__init__(**kwargs)
55
+
56
+ if subset !="all":
57
+
58
+ self.subset = [subset]
59
+ else:
60
+ self.subset = _SUBSETS
61
+
62
+ class Joud(datasets.GeneratorBasedBuilder):
63
+ """TODO: Short description of my dataset."""
64
+
65
+ VERSION = datasets.Version("1.1.0")
66
+ BUILDER_CONFIGS_CLASS = JoudConfig
67
+ BUILDER_CONFIGS = [
68
+ JoudConfig(name=subset,
69
+ subset=subset,
70
+ version=datasets.Version("1.1.0", ""),
71
+ description='')
72
+ for subset in _SUBSETS
73
+ ] + [
74
+ JoudConfig(
75
+ name = "all",
76
+ subset="all",
77
+ version=datasets.Version("1.1.0", ""),
78
+ description='')
79
+ ]
80
+
81
+
82
+ def _info(self):
83
+ # information about the datasets and feature type of the datasets items.
84
+
85
+ features = datasets.Features(
86
+ {
87
+ "text": datasets.Value("string"),
88
+ "meta": datasets.Value("string"),
89
+ }
90
+ )
91
+
92
+ return datasets.DatasetInfo(
93
+ description=_DESCRIPTION,
94
+ features=features,
95
+ homepage=_HOMEPAGE,
96
+ # License for the dataset if available
97
+ license=_LICENSE,
98
+ # Citation for the dataset
99
+ citation=_CITATION,
100
+ )
101
+
102
+ def _split_generators(self, dl_manager):
103
+ #split = 'train'
104
+ #print("Split")
105
+ data_urls = {}
106
+ for split in ['train']: #'validation']:
107
+ #if self.config.subset = "all":
108
+
109
+ data_urls[split] = [
110
+ _URLS.format(
111
+ SUBSET = subset,
112
+ split='validation' if split=='_val' else '',
113
+ index = i,
114
+ )
115
+ for subset in self.config.subset
116
+ for i in range(_N_FILES_PER_SPLIT[subset][split])
117
+ ]
118
+
119
+ train_downloaded_files = dl_manager.download(data_urls["train"])
120
+ #validation_downloaded_files = dl_manager.download(data_urls["validation"])
121
+ return [
122
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepaths": train_downloaded_files}),
123
+ #datasets.SplitGenerator(
124
+ # name=datasets.Split.VALIDATION, gen_kwargs={"filepaths": validation_downloaded_files}
125
+ #),
126
+ ]
127
+ return [
128
+ datasets.SplitGenerator(
129
+ name=datasets.Split.TRAIN,
130
+ # These kwargs will be passed to _generate_examples
131
+ gen_kwargs={
132
+ "filepaths": data_dir,
133
+ "split": "train",
134
+ },
135
+ ),
136
+
137
+ ]
138
+
139
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
140
+ def _generate_examples(self, filepaths):
141
+
142
+ id_ = 0
143
+ for filepath in filepaths:
144
+ with gzip.open(open(filepath,"rb"), "rt", encoding = "utf-8") as f:
145
+ for row in f:
146
+ if row:
147
+
148
+ data = json.loads(row)
149
+ yield id_, data
150
+ id_ +=1