Datasets:
Create crowdflower.py
Browse files- crowdflower.py +210 -0
crowdflower.py
ADDED
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
|
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 |
+
# Lint as: python3
|
17 |
+
"""Crowdflower datasets"""
|
18 |
+
|
19 |
+
from __future__ import absolute_import, division, print_function
|
20 |
+
|
21 |
+
import csv
|
22 |
+
import os
|
23 |
+
import textwrap
|
24 |
+
|
25 |
+
import six
|
26 |
+
|
27 |
+
import datasets
|
28 |
+
|
29 |
+
|
30 |
+
_crowdflower_CITATION = r"""
|
31 |
+
@inproceedings{van2012designing,
|
32 |
+
title={Designing a scalable crowdsourcing platform},
|
33 |
+
author={Van Pelt, Chris and Sorokin, Alex},
|
34 |
+
booktitle={Proceedings of the 2012 ACM SIGMOD International Conference on Management of Data},
|
35 |
+
pages={765--766},
|
36 |
+
year={2012}
|
37 |
+
}
|
38 |
+
"""
|
39 |
+
|
40 |
+
_crowdflower_DESCRIPTION = """
|
41 |
+
Collection of crowdflower classification datasets
|
42 |
+
"""
|
43 |
+
|
44 |
+
DATA_URL = "https://www.dropbox.com/s/ldrcdsv8d9qiwg0/crowdflower.zip?dl=1"
|
45 |
+
|
46 |
+
TASK_TO_LABELS = {'airline-sentiment': ['neutral', 'positive', 'negative'],
|
47 |
+
'corporate-messaging': ['Information', 'Action', 'Exclude', 'Dialogue'],
|
48 |
+
'economic-news': ['not sure', 'yes', 'no'],
|
49 |
+
'political-media-audience': ['constituency', 'national'],
|
50 |
+
'political-media-bias': ['partisan', 'neutral'],
|
51 |
+
'political-media-message': ['information',
|
52 |
+
'support',
|
53 |
+
'policy',
|
54 |
+
'constituency',
|
55 |
+
'personal',
|
56 |
+
'other',
|
57 |
+
'media',
|
58 |
+
'mobilization',
|
59 |
+
'attack'],
|
60 |
+
'sentiment_nuclear_power': ['Neutral / author is just sharing information',
|
61 |
+
'Negative',
|
62 |
+
'Tweet NOT related to nuclear energy',
|
63 |
+
'Positive'],
|
64 |
+
'text_emotion': ['sadness',
|
65 |
+
'empty',
|
66 |
+
'relief',
|
67 |
+
'hate',
|
68 |
+
'worry',
|
69 |
+
'enthusiasm',
|
70 |
+
'happiness',
|
71 |
+
'neutral',
|
72 |
+
'love',
|
73 |
+
'fun',
|
74 |
+
'anger',
|
75 |
+
'surprise',
|
76 |
+
'boredom'],
|
77 |
+
'tweet_global_warming': ['Yes', 'No']}
|
78 |
+
|
79 |
+
def get_labels(task):
|
80 |
+
return TASK_TO_LABELS[task]
|
81 |
+
|
82 |
+
class crowdflowerConfig(datasets.BuilderConfig):
|
83 |
+
"""BuilderConfig for crowdflower."""
|
84 |
+
|
85 |
+
def __init__(
|
86 |
+
self,
|
87 |
+
text_features,
|
88 |
+
label_classes=None,
|
89 |
+
process_label=lambda x: x,
|
90 |
+
**kwargs,
|
91 |
+
):
|
92 |
+
"""BuilderConfig for crowdflower.
|
93 |
+
Args:
|
94 |
+
text_features: `dict[string, string]`, map from the name of the feature
|
95 |
+
dict for each text field to the name of the column in the tsv file
|
96 |
+
label_column: `string`, name of the column in the tsv file corresponding
|
97 |
+
to the label
|
98 |
+
data_url: `string`, url to download the zip file from
|
99 |
+
data_dir: `string`, the path to the folder containing the tsv files in the
|
100 |
+
downloaded zip
|
101 |
+
citation: `string`, citation for the data set
|
102 |
+
url: `string`, url for information about the data set
|
103 |
+
label_classes: `list[string]`, the list of classes if the label is
|
104 |
+
categorical. If not provided, then the label will be of type
|
105 |
+
`datasets.Value('float32')`.
|
106 |
+
process_label: `Function[string, any]`, function taking in the raw value
|
107 |
+
of the label and processing it to the form required by the label feature
|
108 |
+
**kwargs: keyword arguments forwarded to super.
|
109 |
+
"""
|
110 |
+
|
111 |
+
super(crowdflowerConfig, self).__init__(version=datasets.Version("1.0.0", ""), **kwargs)
|
112 |
+
|
113 |
+
self.text_features = text_features
|
114 |
+
self.label_column = "label"
|
115 |
+
self.label_classes = get_labels(self.name)
|
116 |
+
self.data_url = DATA_URL
|
117 |
+
self.data_dir = os.path.join("crowdflower", self.name)
|
118 |
+
self.citation = textwrap.dedent(_crowdflower_CITATION)
|
119 |
+
def process_label(x):
|
120 |
+
x=str(x)
|
121 |
+
if x=="Y":
|
122 |
+
return "Yes"
|
123 |
+
if x=="N":
|
124 |
+
return "No"
|
125 |
+
return x
|
126 |
+
self.process_label = process_label
|
127 |
+
self.description = ""
|
128 |
+
self.url = ""
|
129 |
+
|
130 |
+
|
131 |
+
class crowdflower(datasets.GeneratorBasedBuilder):
|
132 |
+
|
133 |
+
"""The General Language Understanding Evaluation (crowdflower) benchmark."""
|
134 |
+
|
135 |
+
BUILDER_CONFIG_CLASS = crowdflowerConfig
|
136 |
+
|
137 |
+
BUILDER_CONFIGS = [
|
138 |
+
crowdflowerConfig(name="sentiment_nuclear_power",
|
139 |
+
text_features={"text": "text"},),
|
140 |
+
crowdflowerConfig(name="tweet_global_warming",
|
141 |
+
text_features={"text": "text"},),
|
142 |
+
crowdflowerConfig(name="airline-sentiment",
|
143 |
+
text_features={"text": "text"},),
|
144 |
+
crowdflowerConfig(name="corporate-messaging",
|
145 |
+
text_features={"text": "text"},),
|
146 |
+
crowdflowerConfig(name="economic-news",
|
147 |
+
text_features={"text": "text"},),
|
148 |
+
crowdflowerConfig(name="political-media-audience",
|
149 |
+
text_features={"text": "text"},),
|
150 |
+
crowdflowerConfig(name="political-media-bias",
|
151 |
+
text_features={"text": "text"},),
|
152 |
+
crowdflowerConfig(name="political-media-message",
|
153 |
+
text_features={"text": "text"},),
|
154 |
+
crowdflowerConfig(name="text_emotion",
|
155 |
+
text_features={"text": "text"},),
|
156 |
+
]
|
157 |
+
|
158 |
+
def _info(self):
|
159 |
+
features = {text_feature: datasets.Value("string") for text_feature in six.iterkeys(self.config.text_features)}
|
160 |
+
if self.config.label_classes:
|
161 |
+
features["label"] = datasets.features.ClassLabel(names=self.config.label_classes)
|
162 |
+
else:
|
163 |
+
features["label"] = datasets.Value("float32")
|
164 |
+
features["idx"] = datasets.Value("int32")
|
165 |
+
return datasets.DatasetInfo(
|
166 |
+
description=_crowdflower_DESCRIPTION,
|
167 |
+
features=datasets.Features(features),
|
168 |
+
homepage=self.config.url,
|
169 |
+
citation=self.config.citation + "\n" + _crowdflower_CITATION,
|
170 |
+
)
|
171 |
+
|
172 |
+
def _split_generators(self, dl_manager):
|
173 |
+
dl_dir = dl_manager.download_and_extract(self.config.data_url)
|
174 |
+
data_dir = os.path.join(dl_dir, self.config.data_dir)
|
175 |
+
|
176 |
+
return [
|
177 |
+
datasets.SplitGenerator(
|
178 |
+
name=datasets.Split.TRAIN,
|
179 |
+
gen_kwargs={
|
180 |
+
"data_file": os.path.join(data_dir or "", "train.tsv"),
|
181 |
+
"split": "train",
|
182 |
+
},
|
183 |
+
),
|
184 |
+
]
|
185 |
+
|
186 |
+
def _generate_examples(self, data_file, split):
|
187 |
+
|
188 |
+
process_label = self.config.process_label
|
189 |
+
label_classes = self.config.label_classes
|
190 |
+
|
191 |
+
with open(data_file, encoding="latin-1") as f:
|
192 |
+
reader = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
|
193 |
+
|
194 |
+
for n, row in enumerate(reader):
|
195 |
+
|
196 |
+
example = {feat: row[col] for feat, col in six.iteritems(self.config.text_features)}
|
197 |
+
example["idx"] = n
|
198 |
+
#print(row)
|
199 |
+
|
200 |
+
if self.config.label_column in row:
|
201 |
+
label = row[self.config.label_column]
|
202 |
+
label = process_label(label)
|
203 |
+
if label_classes and label not in label_classes:
|
204 |
+
continue
|
205 |
+
example["label"] = label
|
206 |
+
else:
|
207 |
+
example["label"] = process_label(-1)
|
208 |
+
if not example["label"] or not example["text"]:
|
209 |
+
continue
|
210 |
+
yield example["idx"], example
|