sileod commited on
Commit
7f2be74
·
1 Parent(s): b9727a5

Create blimp_classification.py

Browse files
Files changed (1) hide show
  1. blimp_classification.py +187 -0
blimp_classification.py ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """BLIMP Acceptability"""
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
+ _Blimp_CITATION = r"""
31
+ @article{warstadt2019blimp,
32
+ author = {Warstadt, Alex and Parrish, Alicia and Liu, Haokun and Mohananey, Anhad and Peng, Wei and Wang, Sheng-Fu and Bowman, Samuel R.},
33
+ title = {BLiMP: The Benchmark of Linguistic Minimal Pairs for English},
34
+ journal = {Transactions of the Association for Computational Linguistics},
35
+ volume = {8},
36
+ number = {},
37
+ pages = {377-392},
38
+ year = {2020},
39
+ doi = {10.1162/tacl\_a\_00321},
40
+ URL = {https://doi.org/10.1162/tacl_a_00321},
41
+ eprint = {https://doi.org/10.1162/tacl_a_00321},
42
+ abstract = { We introduce The Benchmark of Linguistic Minimal Pairs (BLiMP),1 a challenge set for evaluating the linguistic knowledge of language models (LMs) on major grammatical phenomena in English. BLiMP consists of 67 individual datasets, each containing 1,000 minimal pairs—that is, pairs of minimally different sentences that contrast in grammatical acceptability and isolate specific phenomenon in syntax, morphology, or semantics. We generate the data according to linguist-crafted grammar templates, and human aggregate agreement with the labels is 96.4\%. We evaluate n-gram, LSTM, and Transformer (GPT-2 and Transformer-XL) LMs by observing whether they assign a higher probability to the acceptable sentence in each minimal pair. We find that state-of-the-art models identify morphological contrasts related to agreement reliably, but they struggle with some subtle semantic and syntactic phenomena, such as negative polarity items and extraction islands. }
43
+ }
44
+ """
45
+
46
+ _Blimp_DESCRIPTION = """\
47
+ Acceptable/non acceptable sentences (recasted as a classification task)
48
+ """
49
+
50
+ DATA_URL = "https://www.dropbox.com/s/28s8qj97nuiwyoh/blimp.zip?dl=1"
51
+
52
+
53
+ def get_labels(task):
54
+ return ["acceptable", "unacceptable"]
55
+
56
+
57
+ class BlimpConfig(datasets.BuilderConfig):
58
+ """BuilderConfig for Blimp."""
59
+
60
+ def __init__(
61
+ self,
62
+ text_features,
63
+ label_classes=None,
64
+ process_label=lambda x: x,
65
+ **kwargs,
66
+ ):
67
+ """BuilderConfig for Blimp.
68
+ Args:
69
+ text_features: `dict[string, string]`, map from the name of the feature
70
+ dict for each text field to the name of the column in the tsv file
71
+ label_column: `string`, name of the column in the tsv file corresponding
72
+ to the label
73
+ data_url: `string`, url to download the zip file from
74
+ data_dir: `string`, the path to the folder containing the tsv files in the
75
+ downloaded zip
76
+ citation: `string`, citation for the data set
77
+ url: `string`, url for information about the data set
78
+ label_classes: `list[string]`, the list of classes if the label is
79
+ categorical. If not provided, then the label will be of type
80
+ `datasets.Value('float32')`.
81
+ process_label: `Function[string, any]`, function taking in the raw value
82
+ of the label and processing it to the form required by the label feature
83
+ **kwargs: keyword arguments forwarded to super.
84
+ """
85
+
86
+ super(BlimpConfig, self).__init__(
87
+ version=datasets.Version("1.0.0", ""), **kwargs
88
+ )
89
+
90
+ self.text_features = text_features
91
+ self.label_column = "label"
92
+ self.label_classes = get_labels(self.name)
93
+ self.data_url = DATA_URL
94
+ self.data_dir = os.path.join("blimp", self.name)
95
+ self.citation = textwrap.dedent(_Blimp_CITATION)
96
+ self.process_label = process_label
97
+ self.description = ""
98
+ self.url = ""
99
+
100
+
101
+ class Blimp(datasets.GeneratorBasedBuilder):
102
+
103
+ """The General Language Understanding Evaluation (Blimp) benchmark."""
104
+
105
+ BUILDER_CONFIG_CLASS = BlimpConfig
106
+
107
+ BUILDER_CONFIGS = [
108
+ BlimpConfig(
109
+ name="syntax_semantics",
110
+ text_features={"text": "text"},
111
+ ),
112
+ BlimpConfig(
113
+ name=r"syntax+semantics",
114
+ text_features={"text": "text"},
115
+ ),
116
+ BlimpConfig(
117
+ name="morphology",
118
+ text_features={"text": "text"},
119
+ ),
120
+ BlimpConfig(
121
+ name="syntax",
122
+ text_features={"text": "text"},
123
+ ),
124
+ BlimpConfig(
125
+ name="semantics",
126
+ text_features={"text": "text"},
127
+ ),
128
+ ]
129
+
130
+ def _info(self):
131
+ features = {
132
+ text_feature: datasets.Value("string")
133
+ for text_feature in six.iterkeys(self.config.text_features)
134
+ }
135
+ if self.config.label_classes:
136
+ features["label"] = datasets.features.ClassLabel(
137
+ names=self.config.label_classes
138
+ )
139
+ else:
140
+ features["label"] = datasets.Value("float32")
141
+ features["idx"] = datasets.Value("int32")
142
+ return datasets.DatasetInfo(
143
+ description=_Blimp_DESCRIPTION,
144
+ features=datasets.Features(features),
145
+ homepage=self.config.url,
146
+ citation=self.config.citation + "\n" + _Blimp_CITATION,
147
+ )
148
+
149
+ def _split_generators(self, dl_manager):
150
+ dl_dir = dl_manager.download_and_extract(self.config.data_url)
151
+ data_dir = os.path.join(dl_dir, self.config.data_dir)
152
+
153
+ return [
154
+ datasets.SplitGenerator(
155
+ name=datasets.Split.TRAIN,
156
+ gen_kwargs={
157
+ "data_file": os.path.join(data_dir or "", "train.tsv"),
158
+ "split": "train",
159
+ },
160
+ ),
161
+ ]
162
+
163
+ def _generate_examples(self, data_file, split):
164
+
165
+ process_label = self.config.process_label
166
+ label_classes = self.config.label_classes
167
+
168
+ with open(data_file) as f:
169
+ reader = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_ALL)
170
+
171
+ for n, row in enumerate(reader):
172
+
173
+ example = {
174
+ feat: row[col]
175
+ for feat, col in six.iteritems(self.config.text_features)
176
+ }
177
+ example["idx"] = n
178
+
179
+ if self.config.label_column in row:
180
+ label = row[self.config.label_column]
181
+ if label_classes and label not in label_classes:
182
+ print(row)
183
+ continue
184
+ example["label"] = process_label(label)
185
+ else:
186
+ example["label"] = process_label(-1)
187
+ yield example["idx"], example