huggingFaceUser02 commited on
Commit
b50c62c
1 Parent(s): 6716758

Create load_dataset.py

Browse files
Files changed (1) hide show
  1. load_dataset.py +154 -0
load_dataset.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 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
+ Exercise 2: Part 2 - Extractive QA 30 points ,
17
+ storing a dataset with HuggingFace
18
+ """
19
+
20
+ import csv
21
+ import json
22
+ import os
23
+
24
+ import datasets
25
+
26
+ # Find for instance the citation on arxiv or on the dataset repo/website
27
+ _CITATION = """\
28
+ @InProceedings{air21_grp13:tokenized_results,
29
+ title = {Adv. Information Retrieval - Exercise , Part 2: Storing Results with HuggingFace},
30
+ author={Alexander Genser, Lena Jiricka, Samuel Keller
31
+ },
32
+ year={2021}
33
+ }
34
+ """
35
+
36
+ # You can copy an official description
37
+ _DESCRIPTION = """\
38
+ This new dataset are results from extractive QA, using our top-1 re-ranking results from our implementation of part 1
39
+ and the fira gold label
40
+ """
41
+
42
+ _HOMEPAGE = "https://github.com/tuwien-information-retrieval/air-2021-group_13"
43
+
44
+ _LICENSE = "MIT"
45
+
46
+ # The HuggingFace dataset library don't host the datasets but only point to the original files
47
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
48
+ _URLs = {
49
+ 'first_domain': "https://huggingface.co/datasets/huggingFaceUser02/air21_grp13_tokenized_results.zip",
50
+ 'second_domain': "https://huggingface.co/datasets/huggingFaceUser02/air21_grp13_tokenized_results.zip",
51
+ }
52
+
53
+
54
+ class AIR21Grp13TokenizedResults(datasets.GeneratorBasedBuilder):
55
+ """
56
+ results from the top-1 re-ranking results from implementation of part 1 of Exercise 2, AIR 21.
57
+ These are compared with the FiRA gold label results, using the BERT Transformer
58
+ 'bert-large-uncased-whole-word-masking-finetuned-squad'
59
+ """
60
+
61
+ VERSION = datasets.Version("0.1.0")
62
+
63
+ # This is an example of a dataset with multiple configurations.
64
+ # If you don't want/need to define several sub-sets in your dataset,
65
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
66
+
67
+ # If you need to make complex sub-parts in the datasets with configurable options
68
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
69
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
70
+
71
+ # You will be able to load one or the other configurations in the following list with
72
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
73
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
74
+ BUILDER_CONFIGS = [
75
+ datasets.BuilderConfig(name="inference_results", version=VERSION,
76
+ description="This part of my dataset covers the results of the inference")
77
+ ]
78
+
79
+ DEFAULT_CONFIG_NAME = "inference_results" # It's not mandatory to have a default configuration. Just use one if it make sense.
80
+
81
+ def _info(self):
82
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
83
+ features = datasets.Features(
84
+ {
85
+ "doc_id": datasets.Value("long"),
86
+ "query_id": datasets.Value("long"),
87
+ "relevance": datasets.Value("float"),
88
+ "query_sentence": 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,
97
+ # specify them here. They'll be used if as_supervised=True in
98
+ # builder.as_dataset.
99
+ supervised_keys=None,
100
+ # Homepage of the dataset for documentation
101
+ homepage=_HOMEPAGE,
102
+ # License for the dataset if available
103
+ license=_LICENSE,
104
+ # Citation for the dataset
105
+ citation=_CITATION,
106
+ )
107
+
108
+ def _split_generators(self, dl_manager):
109
+ """Returns SplitGenerators."""
110
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
111
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
112
+
113
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
114
+ # 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.
115
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
116
+ my_urls = _URLs[self.config.name]
117
+ data_dir = dl_manager.download_and_extract(my_urls)
118
+ return [
119
+ datasets.SplitGenerator(
120
+ name=datasets.Split.VALIDATION,
121
+ # These kwargs will be passed to _generate_examples
122
+ gen_kwargs={
123
+ "filepath": os.path.join(data_dir, "top1_bert_large_uncased_answers_01.tsv"),
124
+ "split": "dev",
125
+ },
126
+ ),
127
+ ]
128
+
129
+ def _generate_examples(
130
+ self, filepath, split # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
131
+ ):
132
+ """ Yields examples as (key, example) tuples. """
133
+ # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
134
+ # The `key` is here for legacy reason (tfds) and is not important in itself.
135
+
136
+ with open(filepath, encoding="utf-8") as f:
137
+ # logger.info("Reading instances from lines in file at: %s", file_path)
138
+ for line_num, line in enumerate(f):
139
+ line = line.strip("\n")
140
+
141
+ if not line:
142
+ continue
143
+
144
+ line_parts = line.split('\t')
145
+
146
+ query_id, doc_id, relevance, answer = line_parts
147
+
148
+ yield {
149
+ "query_id": query_id,
150
+ "doc_id": doc_id,
151
+ "relevance": relevance,
152
+ "answer": answer
153
+ }
154
+