Firoj commited on
Commit
eae4739
1 Parent(s): 1843be7

dataset loader script

Browse files
Files changed (1) hide show
  1. humaid.py +210 -0
humaid.py ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ @author: Firoj Alam
3
+ @email: firojalam@gmail.com
4
+ Modified:
5
+ """
6
+
7
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ # TODO: Address all TODOs and remove all explanatory comments
21
+ """
22
+ Data loader for HumAID dataset
23
+ """
24
+
25
+
26
+ import csv
27
+ import json
28
+ import os
29
+
30
+ import datasets
31
+
32
+
33
+ # TODO: Add BibTeX citation
34
+ # Find for instance the citation on arxiv or on the dataset repo/website
35
+ _CITATION = """\
36
+ @inproceedings{humaid2020,
37
+ Author = {Firoj Alam, Umair Qazi, Muhammad Imran, Ferda Ofli},
38
+ booktitle={Proceedings of the Fifteenth International AAAI Conference on Web and Social Media},
39
+ series={ICWSM~'21},
40
+ Keywords = {Social Media, Crisis Computing, Tweet Text Classification, Disaster Response},
41
+ Title = {HumAID: Human-Annotated Disaster Incidents Data from Twitter},
42
+ Year = {2021},
43
+ publisher={AAAI},
44
+ address={Online},
45
+ }
46
+ """
47
+
48
+ # TODO: Add description of the dataset here
49
+ # You can copy an official description
50
+ _DESCRIPTION = """\
51
+ The HumAID Twitter dataset consists of several thousands of manually annotated tweets that has been collected during 19 major natural disaster events including earthquakes, hurricanes, wildfires, and floods, which happened from 2016 to 2019 across different parts of the World. The annotations in the provided datasets consists of following humanitarian categories. The dataset consists only english tweets and it is the largest dataset for crisis informatics so far.
52
+ ** Humanitarian categories **
53
+ - Caution and advice
54
+ - Displaced people and evacuations
55
+ - Dont know cant judge
56
+ - Infrastructure and utility damage
57
+ - Injured or dead people
58
+ - Missing or found people
59
+ - Not humanitarian
60
+ - Other relevant information
61
+ - Requests or urgent needs
62
+ - Rescue volunteering or donation effort
63
+ - Sympathy and support
64
+ """
65
+
66
+ # TODO: Add a link to an official homepage for the dataset here
67
+ _HOMEPAGE = "https://crisisnlp.qcri.org/humaid_dataset"
68
+
69
+ # TODO: Add the licence for the dataset here if you can find it
70
+ _LICENSE = "https://crisisnlp.qcri.org/terms-of-use.html"
71
+
72
+ # TODO: Add link to the official dataset URLs here
73
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
74
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
75
+ _URLS = {
76
+ "humaid": "https://crisisnlp.qcri.org/data/humaid/humaid_data_all.zip",
77
+ }
78
+
79
+ class HumAIDConfig(datasets.BuilderConfig):
80
+ """BuilderConfig for SuperGLUE."""
81
+
82
+ def __init__(self, features, data_url, citation, url,label_classes=("False", "True"), **kwargs):
83
+ """BuilderConfig for SuperGLUE.
84
+ Args:
85
+ features: `list[string]`, list of the features that will appear in the
86
+ feature dict. Should not include "label".
87
+ data_url: `string`, url to download the zip file from.
88
+ citation: `string`, citation for the data set.
89
+ url: `string`, url for information about the data set.
90
+ label_classes: `list[string]`, the list of classes for the label if the
91
+ label is present as a string. Non-string labels will be cast to either
92
+ 'False' or 'True'.
93
+ **kwargs: keyword arguments forwarded to super.
94
+ """
95
+ # Version history:
96
+ # 1.0.2: Fixed non-nondeterminism in ReCoRD.
97
+ # 1.0.1: Change from the pre-release trial version of SuperGLUE (v1.9) to
98
+ # the full release (v2.0).
99
+ # 1.0.0: S3 (new shuffling, sharding and slicing mechanism).
100
+ # 0.0.2: Initial version.
101
+ super(HumAIDConfig, self).__init__(version=datasets.Version("1.1.0"), **kwargs)
102
+ self.features = features
103
+ self.label_classes = label_classes
104
+ self.data_url = data_url
105
+ self.citation = citation
106
+ self.url = url
107
+
108
+
109
+ # TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
110
+ class HumAID(datasets.GeneratorBasedBuilder):
111
+ """TODO: Short description of my dataset."""
112
+
113
+ VERSION = datasets.Version("1.1.0")
114
+
115
+ # This is an example of a dataset with multiple configurations.
116
+ # If you don't want/need to define several sub-sets in your dataset,
117
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
118
+
119
+ # If you need to make complex sub-parts in the datasets with configurable options
120
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
121
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
122
+
123
+ # You will be able to load one or the other configurations in the following list with
124
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
125
+
126
+ BUILDER_CONFIGS = [
127
+ HumAIDConfig(
128
+ name="humaid",
129
+ features = ["tweet_text", "class_label"],
130
+ data_url = "https://crisisnlp.qcri.org/data/humaid/humaid_data_all.zip",
131
+ citation = _CITATION,
132
+ url = "https://github.com/google"
133
+ )
134
+ ]
135
+ print(BUILDER_CONFIGS[0].name)
136
+ # DEFAULT_CONFIG_NAME = "humaid" # It's not mandatory to have a default configuration. Just use one if it make sense.
137
+
138
+ def _info(self):
139
+ features = datasets.Features(
140
+ {
141
+ "tweet_text": datasets.Value("string"),
142
+ "class_label": datasets.Value("string"),
143
+ }
144
+ )
145
+ return datasets.DatasetInfo(
146
+ # This is the description that will appear on the datasets page.
147
+ description=_DESCRIPTION,
148
+ # This defines the different columns of the dataset and their types
149
+ features=features, # Here we define them above because they are different between the two configurations
150
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
151
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
152
+ # supervised_keys=("sentence", "label"),
153
+ # Homepage of the dataset for documentation
154
+ supervised_keys=None,
155
+ homepage=_HOMEPAGE,
156
+ # License for the dataset if available
157
+ license=_LICENSE,
158
+ # Citation for the dataset
159
+ citation=_CITATION,
160
+ )
161
+
162
+ def _split_generators(self, dl_manager):
163
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
164
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
165
+
166
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
167
+ # 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.
168
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
169
+ urls = _URLS[self.config.name]
170
+ print(urls)
171
+ data_dir = dl_manager.download_and_extract(urls)
172
+ return [
173
+ datasets.SplitGenerator(
174
+ name=datasets.Split.TRAIN,
175
+ # These kwargs will be passed to _generate_examples
176
+ gen_kwargs={
177
+ "filepath": os.path.join(data_dir, "humaid_data_all/train.jsonl"),
178
+ "split": "train",
179
+ },
180
+ ),
181
+ datasets.SplitGenerator(
182
+ name=datasets.Split.TEST,
183
+ # These kwargs will be passed to _generate_examples
184
+ gen_kwargs={
185
+ "filepath": os.path.join(data_dir, "humaid_data_all/test.jsonl"),
186
+ "split": "test"
187
+ },
188
+ ),
189
+ datasets.SplitGenerator(
190
+ name=datasets.Split.VALIDATION,
191
+ # These kwargs will be passed to _generate_examples
192
+ gen_kwargs={
193
+ "filepath": os.path.join(data_dir, "humaid_data_all/dev.jsonl"),
194
+ "split": "dev",
195
+ },
196
+ ),
197
+ ]
198
+
199
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
200
+ def _generate_examples(self, filepath, split):
201
+ # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
202
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
203
+ with open(filepath, encoding="utf-8") as f:
204
+ for key, row in enumerate(f):
205
+ data = json.loads(row)
206
+ # Yields examples as (key, example) tuples
207
+ yield key, {
208
+ "tweet_text": data["tweet_text"],
209
+ "class_label": data["class_label"],
210
+ }