leonweber commited on
Commit
ad2e798
1 Parent(s): 03031b1

Upload inconsistencies_flights.py

Browse files
Files changed (1) hide show
  1. inconsistencies_flights.py +194 -0
inconsistencies_flights.py ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
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
+ import random
16
+ from typing import List, Tuple, Dict
17
+ from pathlib import Path
18
+ import json
19
+
20
+ import datasets
21
+
22
+ _CITATION = """\
23
+ @inproceedings{larson-etal-2020-inconsistencies,
24
+ title = "Inconsistencies in Crowdsourced Slot-Filling Annotations: A Typology and Identification Methods",
25
+ author = "Larson, Stefan and
26
+ Cheung, Adrian and
27
+ Mahendran, Anish and
28
+ Leach, Kevin and
29
+ Kummerfeld, Jonathan K.",
30
+ booktitle = "Proceedings of the 28th International Conference on Computational Linguistics",
31
+ month = dec,
32
+ year = "2020",
33
+ address = "Barcelona, Spain (Online)",
34
+ publisher = "International Committee on Computational Linguistics",
35
+ url = "https://aclanthology.org/2020.coling-main.442",
36
+ doi = "10.18653/v1/2020.coling-main.442",
37
+ pages = "5035--5046",
38
+ abstract = "Slot-filling models in task-driven dialog systems rely on carefully annotated training data. However, annotations by crowd workers are often inconsistent or contain errors. Simple solutions like manually checking annotations or having multiple workers label each sample are expensive and waste effort on samples that are correct. If we can identify inconsistencies, we can focus effort where it is needed. Toward this end, we define six inconsistency types in slot-filling annotations. Using three new noisy crowd-annotated datasets, we show that a wide range of inconsistencies occur and can impact system performance if not addressed. We then introduce automatic methods of identifying inconsistencies. Experiments on our new datasets show that these methods effectively reveal inconsistencies in data, though there is further scope for improvement.",
39
+ }
40
+
41
+ """
42
+
43
+ _DATASETNAME = "inconsistencies_flights"
44
+
45
+ _DESCRIPTION = """\
46
+ This dataset is designed for Annotation Error Detection.
47
+ """
48
+
49
+ _HOMEPAGE = ""
50
+
51
+ _LICENSE = "CC-BY 4.0"
52
+
53
+ _URLS = {
54
+ _DATASETNAME: "https://drive.google.com/uc?export=download&id=1h6OB8rUmvjQNHl8L6erNjteXvRfI2sZF",
55
+ }
56
+
57
+ _SOURCE_VERSION = "1.0.0"
58
+
59
+ _SCHEMA = datasets.Features({
60
+ "id": datasets.Value("string"),
61
+ "tokens": datasets.Sequence(datasets.Value("string")),
62
+ "tags_gold": datasets.Sequence(datasets.Value("string")),
63
+ "tags": datasets.Sequence(datasets.Value("string")),
64
+ })
65
+
66
+
67
+
68
+ class InconsistenciesFlights(datasets.GeneratorBasedBuilder):
69
+
70
+ _VERSION = datasets.Version(_SOURCE_VERSION)
71
+
72
+ BUILDER_CONFIGS = [
73
+ datasets.BuilderConfig(
74
+ name="inconsistencies_flights"
75
+ ),
76
+ datasets.BuilderConfig(
77
+ name="inconsistencies_flights_random"
78
+ ),
79
+ ]
80
+ DEFAULT_CONFIG_NAME = "inconsistencies_flights"
81
+
82
+
83
+ def _info(self) -> datasets.DatasetInfo:
84
+ return datasets.DatasetInfo(
85
+ description=_DESCRIPTION,
86
+ features=_SCHEMA,
87
+ supervised_keys=None,
88
+ homepage=_HOMEPAGE,
89
+ citation=_CITATION,
90
+ license=_LICENSE,
91
+ )
92
+
93
+
94
+ def _split_generators(self, dl_manager) -> List[datasets.SplitGenerator]:
95
+ """Returns SplitGenerators."""
96
+ urls = _URLS[_DATASETNAME]
97
+ data_dir = dl_manager.download_and_extract(urls)
98
+
99
+ return [
100
+ datasets.SplitGenerator(
101
+ name=datasets.Split.TRAIN,
102
+ # Whatever you put in gen_kwargs will be passed to _generate_examples
103
+ gen_kwargs={
104
+ "data_dir": data_dir,
105
+ },
106
+ ),
107
+ ]
108
+
109
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
110
+
111
+ def _read_examples(self, file_path):
112
+ """
113
+ data looks like this:
114
+ purchase flight leaving { FROM_LOCATION montreal } to { TO_LOCATION yyz }
115
+
116
+ return tokens and tags separately
117
+ """
118
+ tokens = []
119
+ tags = []
120
+
121
+ with open(file_path, "r") as f:
122
+ cur_tag = "O"
123
+ for line in f:
124
+ line = line.strip()
125
+ if line == "":
126
+ continue
127
+ line_tokens = []
128
+ line_tags = []
129
+ token_iter = iter(line.split())
130
+ for token in token_iter:
131
+ if token == "{":
132
+ tag = next(token_iter).strip()
133
+ token = next(token_iter).strip()
134
+
135
+ line_tokens.append(token)
136
+ line_tags.append("B-" + tag)
137
+
138
+ cur_tag = "I-" + tag
139
+
140
+ elif token == "}":
141
+ cur_tag = "O"
142
+ else:
143
+ line_tokens.append(token)
144
+ line_tags.append(cur_tag)
145
+
146
+ tokens.append(line_tokens)
147
+ tags.append(line_tags)
148
+ return tokens, tags
149
+
150
+ def _generate_examples(self, data_dir) -> Tuple[int, Dict]:
151
+ """Yields examples as (key, example) tuples."""
152
+ data_dir = Path(data_dir)
153
+ gold_file = data_dir / "data" / "flights_gold.data"
154
+ crowd_file = data_dir / "data" / "flights_crowd.data"
155
+
156
+
157
+ all_gold_tokens, all_gold_tags = self._read_examples(gold_file)
158
+ all_crowd_tokens, all_crowd_tags = self._read_examples(crowd_file)
159
+
160
+ gold_tokens_to_tags = {tuple(tokens): tags for tokens, tags in zip(all_gold_tokens, all_gold_tags)}
161
+ crowd_tokens_to_tags = {tuple(tokens): tags for tokens, tags in zip(all_crowd_tokens, all_crowd_tags)}
162
+
163
+ tag_set = set()
164
+ for tags in gold_tokens_to_tags.values():
165
+ tag_set.update(tags)
166
+ tag_set = sorted(tag_set)
167
+
168
+ error_prob = 4.1 / 100 # from Klie et al.
169
+ for idx_example, (gold_tokens, gold_tags) in enumerate(gold_tokens_to_tags.items()):
170
+
171
+ if self.config.name.endswith("_random"):
172
+ crowd_tags = []
173
+ for tag in gold_tags:
174
+ if random.uniform(0, 1) < error_prob:
175
+ crowd_tags.append(random.choice(tag_set))
176
+ else:
177
+ crowd_tags.append(tag)
178
+ else:
179
+ crowd_tags = crowd_tokens_to_tags[gold_tokens]
180
+
181
+ yield idx_example, {
182
+ "id": str(idx_example),
183
+ "tokens": gold_tokens,
184
+ "tags_gold": gold_tags,
185
+ "tags": crowd_tags,
186
+ }
187
+
188
+ if __name__ == "__main__":
189
+ data = datasets.load_dataset(__file__, )
190
+ for i in range(10):
191
+ for token, tag in zip(data["train"][i]["tokens"], data["train"][i]["tags_gold"]):
192
+ print(token, tag)
193
+ print()
194
+