sileod commited on
Commit
340b7b6
1 Parent(s): 081b8a6

Create blimp_classification.py

Browse files
Files changed (1) hide show
  1. blimp_classification.py +174 -0
blimp_classification.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @inproceedings{sileo2021analysis,
45
+ title={Analysis and Prediction of NLP Models Via Task Embeddings},
46
+ author={Damien Sileo and Marie-Francine Moens},
47
+ booktitle = "Proceedings of the 12th Language Resources and Evaluation Conference",
48
+ year={2022},
49
+ }
50
+ """
51
+
52
+ _Blimp_DESCRIPTION = """\
53
+ Acceptable/non acceptable sentences (recasted as a classification task)
54
+ """
55
+
56
+ DATA_URL = "https://www.dropbox.com/s/28s8qj97nuiwyoh/blimp.zip?dl=1"
57
+
58
+
59
+ def get_labels(task):
60
+ return ["unacceptable","acceptable"]
61
+
62
+
63
+ class BlimpConfig(datasets.BuilderConfig):
64
+ """BuilderConfig for Blimp."""
65
+
66
+ def __init__(
67
+ self,
68
+ text_features,
69
+ label_classes=None,
70
+ **kwargs,
71
+ ):
72
+ """BuilderConfig for Blimp.
73
+ Args:
74
+ text_features: `dict[string, string]`, map from the name of the feature
75
+ dict for each text field to the name of the column in the tsv file
76
+ label_column: `string`, name of the column in the tsv file corresponding
77
+ to the label
78
+ data_url: `string`, url to download the zip file from
79
+ data_dir: `string`, the path to the folder containing the tsv files in the
80
+ downloaded zip
81
+ citation: `string`, citation for the data set
82
+ url: `string`, url for information about the data set
83
+ label_classes: `list[string]`, the list of classes if the label is
84
+ categorical. If not provided, then the label will be of type
85
+ `datasets.Value('float32')`.
86
+ process_label: `Function[string, any]`, function taking in the raw value
87
+ of the label and processing it to the form required by the label feature
88
+ **kwargs: keyword arguments forwarded to super.
89
+ """
90
+
91
+ super(BlimpConfig, self).__init__(
92
+ version=datasets.Version("1.0.0", ""), **kwargs
93
+ )
94
+
95
+ self.text_features = text_features
96
+ self.label_column = "label"
97
+ self.label_classes = get_labels(self.name)
98
+ self.data_url = DATA_URL
99
+ self.data_dir = os.path.join("blimp", self.name)
100
+ self.citation = textwrap.dedent(_Blimp_CITATION)
101
+ self.description = ""
102
+ self.url = ""
103
+
104
+
105
+ class Blimp(datasets.GeneratorBasedBuilder):
106
+
107
+ """The General Language Understanding Evaluation (Blimp) benchmark."""
108
+
109
+ BUILDER_CONFIG_CLASS = BlimpConfig
110
+
111
+ BUILDER_CONFIGS = [
112
+ BlimpConfig(
113
+ name=name,
114
+ text_features={"sentence": "sentence"},
115
+ ) for name in ["semantics","syntax","morphology","syntax+semantics","syntax_semantics"]
116
+ ]
117
+
118
+ def _info(self):
119
+ features = {
120
+ text_feature: datasets.Value("string")
121
+ for text_feature in six.iterkeys(self.config.text_features)
122
+ }
123
+ if self.config.label_classes:
124
+ features["label"] = datasets.features.ClassLabel(
125
+ names=self.config.label_classes
126
+ )
127
+ else:
128
+ features["label"] = datasets.Value("float32")
129
+ features["idx"] = datasets.Value("int32")
130
+ return datasets.DatasetInfo(
131
+ description=_Blimp_DESCRIPTION,
132
+ features=datasets.Features(features),
133
+ homepage=self.config.url,
134
+ citation=self.config.citation + "\n" + _Blimp_CITATION,
135
+ )
136
+
137
+ def _split_generators(self, dl_manager):
138
+ dl_dir = dl_manager.download_and_extract(self.config.data_url)
139
+ data_dir = os.path.join(dl_dir, self.config.data_dir)
140
+
141
+ return [
142
+ datasets.SplitGenerator(
143
+ name=datasets.Split.TRAIN,
144
+ gen_kwargs={
145
+ "data_file": os.path.join(data_dir or "", "train.tsv"),
146
+ "split": "train",
147
+ },
148
+ ),
149
+ ]
150
+
151
+ def _generate_examples(self, data_file, split):
152
+
153
+ label_classes = self.config.label_classes
154
+
155
+ with open(data_file) as f:
156
+ reader = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_ALL)
157
+
158
+ for n, row in enumerate(reader):
159
+
160
+ example = {
161
+ feat: row[col.replace("sentence","text")]
162
+ for feat, col in six.iteritems(self.config.text_features)
163
+ }
164
+ example["idx"] = n
165
+
166
+ if self.config.label_column in row:
167
+ label = row[self.config.label_column]
168
+ if label_classes and label not in label_classes:
169
+ print(row)
170
+ continue
171
+ example["label"] =label
172
+ else:
173
+ example["label"] = -1
174
+ yield example["idx"], example