ncoop57 commited on
Commit
5db050e
·
1 Parent(s): 1267e99

Add dataloader

Browse files
Files changed (1) hide show
  1. completeformer.py +163 -0
completeformer.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The HuggingFace Datasets Authors and the Semeru Lab and SEART research group.
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
+ """TODO: Add a description here."""
16
+
17
+
18
+ import csv
19
+ import glob
20
+ import os
21
+
22
+ import datasets
23
+
24
+ import numpy as np
25
+
26
+ # TODO: Add BibTeX citation
27
+ # Find for instance the citation on arxiv or on the dataset repo/website
28
+ _CITATION = """\
29
+ @InProceedings{huggingface:dataset,
30
+ title = {A great new dataset},
31
+ author={huggingface, Inc.
32
+ },
33
+ year={2020}
34
+ }
35
+ """
36
+
37
+ # TODO: Add description of the dataset here
38
+ # You can copy an official description
39
+ _DESCRIPTION = """\
40
+ This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
41
+ """
42
+
43
+ # TODO: Add a link to an official homepage for the dataset here
44
+ _HOMEPAGE = ""
45
+
46
+ # TODO: Add the licence for the dataset here if you can find it
47
+ _LICENSE = ""
48
+
49
+ # TODO: Add link to the official dataset URLs here
50
+ # The HuggingFace dataset library don't host the datasets but only point to the original files
51
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
52
+ _DATA_URLs = {
53
+ "long": {
54
+ "train": "https://huggingface.co/datasets/semeru/completeformer_java_data/resolve/main/long/training_long.csv",
55
+ "valid": "https://huggingface.co/datasets/semeru/completeformer_java_data/resolve/main/long/validation_long.csv",
56
+ "test": "https://huggingface.co/datasets/semeru/completeformer_java_data/resolve/main/long/test_long.csv",
57
+ },
58
+ "medium": {
59
+ "train": "https://huggingface.co/datasets/semeru/completeformer_java_data/resolve/main/medium/training_medium.csv",
60
+ "valid": "https://huggingface.co/datasets/semeru/completeformer_java_data/resolve/main/medium/validation_medium.csv",
61
+ "test": "https://huggingface.co/datasets/semeru/completeformer_java_data/resolve/main/medium/test_medium.csv",
62
+ },
63
+ "short": {
64
+ "train": "https://huggingface.co/datasets/semeru/completeformer_java_data/resolve/main/short/training_short.csv",
65
+ "valid": "https://huggingface.co/datasets/semeru/completeformer_java_data/resolve/main/short/validation_short.csv",
66
+ "test": "https://huggingface.co/datasets/semeru/completeformer_java_data/resolve/main/short/test_short.csv",
67
+ },
68
+ }
69
+
70
+
71
+ # TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
72
+ class CSNCHumanJudgementDataset(datasets.GeneratorBasedBuilder):
73
+ """TODO: Short description of my dataset."""
74
+
75
+ VERSION = datasets.Version("1.1.0")
76
+
77
+ BUILDER_CONFIGS = [
78
+ datasets.BuilderConfig(
79
+ name="long",
80
+ version=VERSION,
81
+ description="",
82
+ ),
83
+ datasets.BuilderConfig(
84
+ name="medium",
85
+ version=VERSION,
86
+ description="",
87
+ ),
88
+ datasets.BuilderConfig(
89
+ name="short",
90
+ version=VERSION,
91
+ description="",
92
+ ),
93
+ ]
94
+
95
+ DEFAULT_CONFIG_NAME = "long"
96
+
97
+ def _info(self):
98
+ features = datasets.Features(
99
+ {
100
+ "idx": datasets.Value("int32"),
101
+ "input": datasets.Value("string"),
102
+ "target": datasets.Value("string"),
103
+ }
104
+ )
105
+
106
+ return datasets.DatasetInfo(
107
+ description=_DESCRIPTION,
108
+ features=features,
109
+ supervised_keys=None,
110
+ homepage=_HOMEPAGE,
111
+ license=_LICENSE,
112
+ citation=_CITATION,
113
+ )
114
+
115
+ def _split_generators(self, dl_manager):
116
+ """Returns SplitGenerators."""
117
+ my_urls = _DATA_URLs[self.config.name]
118
+ data_dirs = {}
119
+ for k, v in my_urls.items():
120
+ data_dirs[k] = dl_manager.download_and_extract(v)
121
+ return [
122
+ datasets.SplitGenerator(
123
+ name=datasets.Split.TRAIN,
124
+ # These kwargs will be passed to _generate_examples
125
+ gen_kwargs={
126
+ "file_path": data_dirs["train"],
127
+ },
128
+ ),
129
+ datasets.SplitGenerator(
130
+ name=datasets.Split.VALIDATION,
131
+ # These kwargs will be passed to _generate_examples
132
+ gen_kwargs={
133
+ "file_path": data_dirs["valid"],
134
+ },
135
+ ),
136
+ datasets.SplitGenerator(
137
+ name=datasets.Split.TEST,
138
+ # These kwargs will be passed to _generate_examples
139
+ gen_kwargs={
140
+ "file_path": data_dirs["test"],
141
+ },
142
+ ),
143
+ ]
144
+
145
+ def _generate_examples(
146
+ self,
147
+ file_path,
148
+ ):
149
+ """Yields examples as (key, example) tuples."""
150
+ # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
151
+ # The `key` is here for legacy reason (tfds) and is not important in itself.
152
+
153
+ with open(file_path, encoding="utf-8") as f:
154
+ csv_reader = csv.reader(f, quotechar='"', delimiter=",", quoting=csv.QUOTE_ALL, skipinitialspace=True)
155
+ next(csv_reader, None) # skip header
156
+
157
+ for row_id, row in enumerate(csv_reader):
158
+ _, idx, input, target = row
159
+ yield row_id, {
160
+ "idx": idx,
161
+ "input": input,
162
+ "target": target,
163
+ }