zhengxuanzenwu commited on
Commit
30bd47b
1 Parent(s): 8910c0a

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +326 -0
README.md ADDED
@@ -0,0 +1,326 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # DynaSent: Dynamic Sentiment Analysis Dataset
2
+
3
+ DynaSent is an English-language benchmark task for ternary (positive/negative/neutral) sentiment analysis. This dataset card is forked from the original [DynaSent Repository](https://github.com/cgpotts/dynasent).
4
+
5
+ ## Contents
6
+
7
+ * [Citation](#Citation)
8
+ * [Dataset files](#dataset-files)
9
+ * [Quick start](#quick-start)
10
+ * [Data format](#data-format)
11
+ * [Models](#models)
12
+ * [Other files](#other-files)
13
+ * [License](#license)
14
+
15
+
16
+ ## Citation
17
+
18
+ [Christopher Potts](http://web.stanford.edu/~cgpotts/), [Zhengxuan Wu](http://zen-wu.social), Atticus Geiger, and [Douwe Kiela](https://douwekiela.github.io). 2020. [DynaSent: A dynamic benchmark for sentiment analysis](https://arxiv.org/abs/2012.15349). Ms., Stanford University and Facebook AI Research.
19
+
20
+ ```stex
21
+ @article{potts-etal-2020-dynasent,
22
+ title={{DynaSent}: A Dynamic Benchmark for Sentiment Analysis},
23
+ author={Potts, Christopher and Wu, Zhengxuan and Geiger, Atticus and Kiela, Douwe},
24
+ journal={arXiv preprint arXiv:2012.15349},
25
+ url={https://arxiv.org/abs/2012.15349},
26
+ year={2020}}
27
+ ```
28
+
29
+ ## Dataset files
30
+
31
+ The dataset is [dynasent-v1.1.zip](dynasent-v1.1.zip), which is included in this repository. `v1.1` differs from `v1` only in that `v1.1` has proper unique ids for Round 1 and corrects a bug that led to some non-unique ids in Round 2. There are no changes to the examples or other metadata.
32
+
33
+ The dataset consists of two rounds, each with a train/dev/test split:
34
+
35
+
36
+ ### Round 1: Naturally occurring sentences
37
+
38
+ * `dynasent-v1.1-round01-yelp-train.jsonl`
39
+ * `dynasent-v1.1-round01-yelp-dev.jsonl`
40
+ * `dynasent-v1.1-round01-yelp-test.jsonl`
41
+
42
+
43
+ ### Round 1: Sentences crowdsourced using Dynabench
44
+
45
+ * `dynasent-v1.1-round02-dynabench-train.jsonl`
46
+ * `dynasent-v1.1-round02-dynabench-dev.jsonl`
47
+ * `dynasent-v1.1-round02-dynabench-test.jsonl`
48
+
49
+
50
+ ### SST-dev revalidation
51
+
52
+ The dataset also contains a version of the [Stanford Sentiment Treebank](https://nlp.stanford.edu/sentiment/) dev set in our format with labels from our validation task:
53
+
54
+ * `sst-dev-validated.jsonl`
55
+
56
+
57
+ ## Quick start
58
+
59
+ This function can be used to load any subset of the files:
60
+
61
+ ```python
62
+ import json
63
+
64
+ def load_dataset(*src_filenames, labels=None):
65
+ data = []
66
+ for filename in src_filenames:
67
+ with open(filename) as f:
68
+ for line in f:
69
+ d = json.loads(line)
70
+ if labels is None or d['gold_label'] in labels:
71
+ data.append(d)
72
+ return data
73
+ ```
74
+
75
+ For example, to create a Round 1 train set restricting to examples with ternary gold labels:
76
+
77
+ ```python
78
+ import os
79
+
80
+ r1_train_filename = os.path.join('dynasent-v1.1', 'dynasent-v1.1-round01-yelp-train.jsonl')
81
+
82
+ ternary_labels = ('positive', 'negative', 'neutral')
83
+
84
+ r1_train = load_dataset(r1_train_filename, labels=ternary_labels)
85
+
86
+ X_train, y_train = zip(*[(d['sentence'], d['gold_label']) for d in r1_train])
87
+ ```
88
+
89
+ ## Data format
90
+
91
+ ### Round 1 format
92
+
93
+ ```python
94
+ {'hit_ids': ['y5238'],
95
+ 'sentence': 'Roto-Rooter is always good when you need someone right away.',
96
+ 'indices_into_review_text': [0, 60],
97
+ 'model_0_label': 'positive',
98
+ 'model_0_probs': {'negative': 0.01173639390617609,
99
+ 'positive': 0.7473671436309814,
100
+ 'neutral': 0.24089649319648743},
101
+ 'text_id': 'r1-0000001',
102
+ 'review_id': 'IDHkeGo-nxhqX4Exkdr08A',
103
+ 'review_rating': 1,
104
+ 'label_distribution': {'positive': ['w130', 'w186', 'w207', 'w264', 'w54'],
105
+ 'negative': [],
106
+ 'neutral': [],
107
+ 'mixed': []},
108
+ 'gold_label': 'positive'}
109
+ ```
110
+
111
+ Details:
112
+
113
+ * `'hit_ids'`: List of Amazon Mechanical Turk Human Interface Tasks (HITs) in which this example appeared during validation. The values are anonymized but used consistently throughout the dataset.
114
+ * `'sentence'`: The example text.
115
+ * `'indices_into_review_text':` indices of `'sentence'` into the original review in the [Yelp Academic Dataset](https://www.yelp.com/dataset).
116
+ * `'model_0_label'`: prediction of Model 0 as described in the paper. The possible values are `'positive'`, `'negative'`, and `'neutral'`.
117
+ * `'model_0_probs'`: probability distribution predicted by Model 0. The keys are `('positive', 'negative', 'neutral')` and the values are floats.
118
+ * `'text_id'`: unique identifier for this entry.
119
+ * `'review_id'`: review-level identifier for the review from the [Yelp Academic Dataset](https://www.yelp.com/dataset) containing `'sentence'`.
120
+ * `'review_rating'`: review-level star-rating for the review containing `'sentence'` in the [Yelp Academic Dataset](https://www.yelp.com/dataset). The possible values are `1`, `2`, `3`, `4`, and `5`.
121
+ * `'label_distribution':` response distribution from the MTurk validation task. The keys are `('positive', 'negative', 'neutral')` and the values are lists of anonymized MTurk ids, which are used consistently throughout the dataset.
122
+ * `'gold_label'`: the label chosen by at least three of the five workers if there is one (possible values: `'positive'`, `'negative'`, '`neutral'`, and `'mixed'`), else `None`.
123
+
124
+ Here is some code one could use to augment a dataset, as loaded by `load_dataset`, with a field giving the full review text from the [Yelp Academic Dataset](https://www.yelp.com/dataset):
125
+
126
+ ```python
127
+ import json
128
+
129
+ def index_yelp_reviews(yelp_src_filename='yelp_academic_dataset_review.json'):
130
+ index = {}
131
+ with open(yelp_src_filename) as f:
132
+ for line in f:
133
+ d = json.loads(line)
134
+ index[d['review_id']] = d['text']
135
+ return index
136
+
137
+ yelp_index = index_yelp_reviews()
138
+
139
+ def add_review_text_round1(dataset, yelp_index):
140
+ for d in dataset:
141
+ review_text = yelp_index[d['text_id']]
142
+ # Check that we can find the sentence as expected:
143
+ start, end = d['indices_into_review_text']
144
+ assert review_text[start: end] == d['sentence']
145
+ d['review_text'] = review_text
146
+ return dataset
147
+ ```
148
+
149
+ ### Round 2 format
150
+
151
+ ```python
152
+ {'hit_ids': ['y22661'],
153
+ 'sentence': "We enjoyed our first and last meal in Toronto at Bombay Palace, and I can't think of a better way to book our journey.",
154
+ 'sentence_author': 'w250',
155
+ 'has_prompt': True,
156
+ 'prompt_data': {'indices_into_review_text': [2093, 2213],
157
+ 'review_rating': 5,
158
+ 'prompt_sentence': "Our first and last meals in Toronto were enjoyed at Bombay Palace and I can't think of a better way to bookend our trip.",
159
+ 'review_id': 'Krm4kSIb06BDHternF4_pA'},
160
+ 'model_1_label': 'positive',
161
+ 'model_1_probs': {'negative': 0.29140257835388184,
162
+ 'positive': 0.6788994669914246,
163
+ 'neutral': 0.029697999358177185},
164
+ 'text_id': 'r2-0000001',
165
+ 'label_distribution': {'positive': ['w43', 'w26', 'w155', 'w23'],
166
+ 'negative': [],
167
+ 'neutral': [],
168
+ 'mixed': ['w174']},
169
+ 'gold_label': 'positive'}
170
+ ```
171
+
172
+ Details:
173
+
174
+ * `'hit_ids'`: List of Amazon Mechanical Turk Human Interface Tasks (HITs) in which this example appeared during validation. The values are anonymized but used consistently throughout the dataset.
175
+ * `'sentence'`: The example text.
176
+ * `'sentence_author'`: Anonymized MTurk id of the worker who wrote `'sentence'`. These are from the same family of ids as used in `'label_distribution'`, but this id is never one of the ids in `'label_distribution'` for this example.
177
+ * `'has_prompt'`: `True` if the `'sentence'` was written with a Prompt else `False`.
178
+ * `'prompt_data'`: None if `'has_prompt'` is False, else:
179
+ * `'indices_into_review_text'`: indices of `'prompt_sentence'` into the original review in the [Yelp Academic Dataset](https://www.yelp.com/dataset).
180
+ * `'review_rating'`: review-level star-rating for the review containing `'sentence'` in the [Yelp Academic Dataset](https://www.yelp.com/dataset).
181
+ * `'prompt_sentence'`: The prompt text.
182
+ * `'review_id'`: review-level identifier for the review from the [Yelp Academic Dataset](https://www.yelp.com/dataset) containing `'prompt_sentence'`.
183
+ * `'model_1_label'`: prediction of Model 1 as described in the paper. The possible values are `'positive'`, `'negative'`, and '`neutral'`.
184
+ * `'model_1_probs'`: probability distribution predicted by Model 1. The keys are `('positive', 'negative', 'neutral')` and the values are floats.
185
+ * `'text_id'`: unique identifier for this entry.
186
+ * `'label_distribution'`: response distribution from the MTurk validation task. The keys are `('positive', 'negative', 'neutral')` and the values are lists of anonymized MTurk ids, which are used consistently throughout the dataset.
187
+ * `'gold_label'`: the label chosen by at least three of the five workers if there is one (possible values: `'positive'`, `'negative'`, '`neutral'`, and `'mixed'`), else `None`.
188
+
189
+ To add the review texts to the `'prompt_data'` field, one can extend the code above for Round 1 with the following function:
190
+
191
+ ```python
192
+ def add_review_text_round2(dataset, yelp_index):
193
+ for d in dataset:
194
+ if d['has_prompt']:
195
+ prompt_data = d['prompt_data']
196
+ review_text = yelp_index[prompt_data['review_id']]
197
+ # Check that we can find the sentence as expected:
198
+ start, end = prompt_data['indices_into_review_text']
199
+ assert review_text[start: end] == prompt_data['prompt_sentence']
200
+ prompt_data['review_text'] = review_text
201
+ return dataset
202
+ ```
203
+
204
+ ### SST-dev format
205
+
206
+ ```python
207
+ {'hit_ids': ['s20533'],
208
+ 'sentence': '-LRB- A -RRB- n utterly charming and hilarious film that reminded me of the best of the Disney comedies from the 60s.',
209
+ 'tree': '(4 (2 (1 -LRB-) (2 (2 A) (3 -RRB-))) (4 (4 (2 n) (4 (3 (2 utterly) (4 (3 (4 charming) (2 and)) (4 hilarious))) (3 (2 film) (3 (2 that) (4 (4 (2 (2 reminded) (3 me)) (4 (2 of) (4 (4 (2 the) (4 best)) (2 (2 of) (3 (2 the) (3 (3 Disney) (2 comedies))))))) (2 (2 from) (2 (2 the) (2 60s)))))))) (2 .)))',
210
+ 'text_id': 'sst-dev-validate-0000437',
211
+ 'sst_label': '4',
212
+ 'label_distribution': {'positive': ['w207', 'w3', 'w840', 'w135', 'w26'],
213
+ 'negative': [],
214
+ 'neutral': [],
215
+ 'mixed': []},
216
+ 'gold_label': 'positive'}
217
+ ```
218
+
219
+ Details:
220
+
221
+ * `'hit_ids'`: List of Amazon Mechanical Turk Human Interface Tasks (HITs) in which this example appeared during validation. The values are anonymized but used consistently throughout the dataset.
222
+ * `'sentence'`: The example text.
223
+ * `'tree'`: The parsetree for the example as given in the SST distribution.
224
+ * `'text_id'`: A new identifier for this example.
225
+ * `'sst_label'`: The root-node label from the SST. Possible values `'0'`, `'1'` `'2'`, `'3'`, and `'4'`.
226
+ * `'label_distribution':` response distribution from the MTurk validation task. The keys are `('positive', 'negative', 'neutral')` and the values are lists of anonymized MTurk ids, which are used consistently throughout the dataset.
227
+ * `'gold_label'`: the label chosen by at least three of the five workers if there is one (possible values: `'positive'`, `'negative'`, '`neutral'`, and `'mixed'`), else `None`.
228
+
229
+
230
+ ## Models
231
+
232
+ Model 0 and Model 1 from the paper are available here:
233
+
234
+ https://drive.google.com/drive/folders/1dpKrjNJfAILUQcJPAFc5YOXUT51VEjKQ?usp=sharing
235
+
236
+ This repository includes a Python module `dynasent_models.py` that provides a [Hugging Face](https://huggingface.co)-based wrapper around these ([PyTorch](https://pytorch.org)) models. Simple examples:
237
+
238
+ ```python
239
+ import os
240
+ from dynasent_models import DynaSentModel
241
+
242
+ # `dynasent_model0` should be downloaded from the above Google Drive link and
243
+ # placed in the `models` directory. `dynasent_model1` works the same way.
244
+ model = DynaSentModel(os.path.join('models', 'dynasent_model0.bin'))
245
+
246
+ examples = [
247
+ "superb",
248
+ "They said the experience would be amazing, and they were right!",
249
+ "They said the experience would be amazing, and they were wrong!"]
250
+
251
+ model.predict(examples)
252
+ ```
253
+ This should return the list `['positive', 'positive', 'negative']`.
254
+
255
+ The `predict_proba` method provides access to the predicted distribution over the class labels; see the demo at the bottom of `dynasent_models.py` for details.
256
+
257
+ The following code uses `load_dataset` from above to reproduce the Round 2 dev-set report on Model 0 from the paper:
258
+
259
+ ```python
260
+ import os
261
+ from sklearn.metrics import classification_report
262
+ from dynasent_models import DynaSentModel
263
+
264
+ dev_filename = os.path.join('dynasent-v1.1', 'dynasent-v1.1-round02-dynabench-dev.jsonl')
265
+
266
+ dev = load_dataset(dev_filename)
267
+
268
+ X_dev, y_dev = zip(*[(d['sentence'], d['gold_label']) for d in dev])
269
+
270
+ model = DynaSentModel(os.path.join('models', 'dynasent_model0.bin'))
271
+
272
+ preds = model.predict(X_dev)
273
+
274
+ print(classification_report(y_dev, preds, digits=3))
275
+ ```
276
+ For a fuller report on these models, see our paper and [our model card](dynasent_modelcard.md).
277
+
278
+
279
+ ## Other files
280
+
281
+
282
+ ### Analysis notebooks
283
+
284
+ The following notebooks reproduce the dataset statistics, figures, and random example selections from the paper:
285
+
286
+ * `analyses_comparative.ipynb`
287
+ * `analysis_round1.ipynb`
288
+ * `analysis_round2.ipynb`
289
+ * `analysis_sst_dev_revalidate.ipynb`
290
+
291
+ The Python module `dynasent_utils.py` contains functions that support those notebooks, and `dynasent.mplstyle` helps with styling the plots.
292
+
293
+
294
+ ### Datasheet
295
+
296
+ The [Datasheet](https://arxiv.org/abs/1803.09010) for our dataset:
297
+
298
+ * [dynasent_datasheet.md](dynasent_datasheet.md)
299
+
300
+
301
+ ### Model Card
302
+
303
+ The [Model Card](https://arxiv.org/pdf/1810.03993.pdf) for our models:
304
+
305
+ * [dynasent_modelcard.md](dynasent_modelcard.md)
306
+
307
+
308
+ ### Tests
309
+
310
+ The module `test_dataset.py` contains PyTest tests for the dataset. To use it, run
311
+
312
+ ```
313
+ py.test -vv test_dataset.py
314
+ ```
315
+
316
+ in the root directory of this repository.
317
+
318
+
319
+ ### Validation HIT code
320
+
321
+ The file `validation-hit-contents.html` contains the HTML/Javascript used in the validation task. It could be used directly on Amazon Mechanical Turk, by simply pasting its contents into the usual HIT creation window.
322
+
323
+
324
+ ## License
325
+
326
+ DynaSent has a [Creative Commons Attribution 4.0 International License](https://creativecommons.org/licenses/by/4.0/).