aih commited on
Commit
1a2d6bb
1 Parent(s): 3371cee

Add load_output_sample, update README

Browse files
Files changed (1) hide show
  1. load_output_sample.py +162 -0
load_output_sample.py ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # TODO: Address all TODOs and remove all explanatory comments
15
+ """Loading script for output_sample dataset, from the Historical American Buildings, Landscapes, and Engineering Records collection of the Library of Congress."""
16
+
17
+
18
+ import json
19
+ import os
20
+
21
+ import datasets
22
+
23
+
24
+ # TODO: Add BibTeX citation
25
+ # Find for instance the citation on arxiv or on the dataset repo/website
26
+ _CITATION = """\
27
+ """
28
+
29
+ _DESCRIPTION = """\
30
+ Sample dataset scraped from https://www.loc.gov/collections/historic-american-buildings-landscapes-and-engineering-records/?c=150&at!=content,pages&fo=json
31
+ The dataset contains images and metadata for historic buildings, landscapes, and engineering records.
32
+ """
33
+
34
+ _HOMEPAGE = "https://www.loc.gov/collections/historic-american-buildings-landscapes-and-engineering-records"
35
+
36
+ _LICENSE = "Creative Commons 1.0 Universal"
37
+
38
+
39
+ # TODO: Add link to the official dataset URLs here
40
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
41
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
42
+ _URLS = {
43
+ "first_domain": "https://huggingface.co/great-new-dataset-first_domain.zip",
44
+ "second_domain": "https://huggingface.co/great-new-dataset-second_domain.zip",
45
+ }
46
+
47
+
48
+ class HABLER_LOC(datasets.GeneratorBasedBuilder):
49
+ """Historical American Buildings, Landscapes, and Engineering Records dataset."""
50
+
51
+ VERSION = datasets.Version("1.1.0")
52
+
53
+ def _info(self):
54
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
55
+ features = datasets.Features(
56
+ {
57
+ "call_number": datasets.Value("string"),
58
+ "control_number": datasets.Value("string"),
59
+ "created": datasets.Value("string"),
60
+ "created_published": datasets.Value("string"),
61
+ "created_published_date": datasets.Value("string"),
62
+ "creators": datasets.Sequence(feature={"link": datasets.Value("string"), "role": datasets.Value("string"), "title": datasets.Value("string") }),
63
+ "date": datasets.Value("string"),
64
+ "display_offsite": datasets.Bool(),
65
+ "id": datasets.Value("string"),
66
+ "link": datasets.Value("string"),
67
+ "medium_brief": datasets.Value("string"),
68
+ "mediums": datasets.Sequence(datasets.Value("string")),
69
+ "modified": datasets.Value("string"),
70
+ "notes": datasets.Sequence(datasets.Value("string")),
71
+ "part_of": datasets.Value("string"),
72
+ "part_of_group": datasets.Value("string"),
73
+ "place": datasets.Sequence(features={
74
+ "latitude": datasets.Value("string"),
75
+ "link": datasets.Value("string"),
76
+ "longitude": datasets.Value("string"),
77
+ "title": datasets.Value("string")}),
78
+ "repository": datasets.Value("string"),
79
+ "resource_links": datasets.Sequence(datasets.Value("string")),
80
+ "rights_advisory": datasets.Value("string"),
81
+ "rights_information": datasets.Value("string"),
82
+ "service_low": datasets.Value("string"),
83
+ "service_medium": datasets.Value("string"),
84
+ "source_created": datasets.Value("string"),
85
+ "source_modified": datasets.Value("string"),
86
+ "subject_headings": datasets.Sequence(datasets.Value("string")),
87
+ "thumb_gallery": datasets.Value("string"),
88
+ "title": datasets.Value("string")
89
+ }
90
+ )
91
+ return datasets.DatasetInfo(
92
+ # This is the description that will appear on the datasets page.
93
+ description=_DESCRIPTION,
94
+ # This defines the different columns of the dataset and their types
95
+ features=features, # Here we define them above because they are different between the two configurations
96
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
97
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
98
+ # supervised_keys=("sentence", "label"),
99
+ # Homepage of the dataset for documentation
100
+ homepage=_HOMEPAGE,
101
+ # License for the dataset if available
102
+ license=_LICENSE,
103
+ # Citation for the dataset
104
+ citation=_CITATION,
105
+ )
106
+
107
+ def _split_generators(self, dl_manager):
108
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
109
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
110
+
111
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
112
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
113
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
114
+ urls = _URLS[self.config.name]
115
+ data_dir = dl_manager.download_and_extract(urls)
116
+ return [
117
+ datasets.SplitGenerator(
118
+ name=datasets.Split.TRAIN,
119
+ # These kwargs will be passed to _generate_examples
120
+ gen_kwargs={
121
+ "filepath": os.path.join(data_dir, "train.jsonl"),
122
+ "split": "train",
123
+ },
124
+ ),
125
+ datasets.SplitGenerator(
126
+ name=datasets.Split.VALIDATION,
127
+ # These kwargs will be passed to _generate_examples
128
+ gen_kwargs={
129
+ "filepath": os.path.join(data_dir, "dev.jsonl"),
130
+ "split": "dev",
131
+ },
132
+ ),
133
+ datasets.SplitGenerator(
134
+ name=datasets.Split.TEST,
135
+ # These kwargs will be passed to _generate_examples
136
+ gen_kwargs={
137
+ "filepath": os.path.join(data_dir, "test.jsonl"),
138
+ "split": "test"
139
+ },
140
+ ),
141
+ ]
142
+
143
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
144
+ def _generate_examples(self, filepath, split):
145
+ # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
146
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
147
+ with open(filepath, encoding="utf-8") as f:
148
+ for key, row in enumerate(f):
149
+ data = json.loads(row)
150
+ if self.config.name == "first_domain":
151
+ # Yields examples as (key, example) tuples
152
+ yield key, {
153
+ "sentence": data["sentence"],
154
+ "option1": data["option1"],
155
+ "answer": "" if split == "test" else data["answer"],
156
+ }
157
+ else:
158
+ yield key, {
159
+ "sentence": data["sentence"],
160
+ "option2": data["option2"],
161
+ "second_domain_answer": "" if split == "test" else data["second_domain_answer"],
162
+ }