leonweber commited on
Commit
3ab5d30
1 Parent(s): 74c3ff1

Upload inconsistencies_companies.py

Browse files
Files changed (1) hide show
  1. inconsistencies_companies.py +151 -0
inconsistencies_companies.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
16
+ """
17
+ This template serves as a starting point for contributing a dataset to the BigScience Biomedical repo.
18
+
19
+ When modifying it for your dataset, look for TODO items that offer specific instructions.
20
+
21
+ Full documentation on writing dataset loading scripts can be found here:
22
+ https://huggingface.co/docs/datasets/add_dataset.html
23
+
24
+ To create a dataset loading script you will create a class and implement 3 methods:
25
+ * `_info`: Establishes the schema for the dataset, and returns a datasets.DatasetInfo object.
26
+ * `_split_generators`: Downloads and extracts data for each split (e.g. train/val/test) or associate local data with each split.
27
+ * `_generate_examples`: Creates examples from data on disk that conform to each schema defined in `_info`.
28
+
29
+ TODO: Before submitting your script, delete this doc string and replace it with a description of your dataset.
30
+
31
+ [bigbio_schema_name] = (kb, pairs, qa, text, t2t, entailment)
32
+ """
33
+
34
+ from typing import List, Tuple, Dict
35
+ from pathlib import Path
36
+ import json
37
+
38
+ import datasets
39
+
40
+ _CITATION = """\
41
+ @inproceedings{larson-etal-2020-inconsistencies,
42
+ title = "Inconsistencies in Crowdsourced Slot-Filling Annotations: A Typology and Identification Methods",
43
+ author = "Larson, Stefan and
44
+ Cheung, Adrian and
45
+ Mahendran, Anish and
46
+ Leach, Kevin and
47
+ Kummerfeld, Jonathan K.",
48
+ booktitle = "Proceedings of the 28th International Conference on Computational Linguistics",
49
+ month = dec,
50
+ year = "2020",
51
+ address = "Barcelona, Spain (Online)",
52
+ publisher = "International Committee on Computational Linguistics",
53
+ url = "https://aclanthology.org/2020.coling-main.442",
54
+ doi = "10.18653/v1/2020.coling-main.442",
55
+ pages = "5035--5046",
56
+ 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.",
57
+ }
58
+
59
+ """
60
+
61
+ _DATASETNAME = "inconsistencies_companies"
62
+
63
+ _DESCRIPTION = """\
64
+ This dataset is designed for Annotation Error Detection.
65
+ """
66
+
67
+ _HOMEPAGE = ""
68
+
69
+ _LICENSE = "CC-BY 4.0"
70
+
71
+ _URLS = {
72
+ _DATASETNAME: "https://drive.google.com/uc?export=download&id=1h6OB8rUmvjQNHl8L6erNjteXvRfI2sZF",
73
+ }
74
+
75
+ _SOURCE_VERSION = "1.0.0"
76
+
77
+ _SCHEMA = datasets.Features({
78
+ "id": datasets.Value("string"),
79
+ "official_split": datasets.Value("string"),
80
+ "tokens": datasets.Sequence(datasets.Value("string")),
81
+ "tags_gold": datasets.Sequence(datasets.Value("string")),
82
+ "tags": datasets.Sequence(datasets.Value("string")),
83
+ "classification": datasets.Value("string"),
84
+ "sentence": datasets.Value("string"),
85
+ })
86
+
87
+
88
+
89
+ class InconsistenciesCompanies(datasets.GeneratorBasedBuilder):
90
+
91
+ _VERSION = datasets.Version(_SOURCE_VERSION)
92
+
93
+ def _info(self) -> datasets.DatasetInfo:
94
+ return datasets.DatasetInfo(
95
+ description=_DESCRIPTION,
96
+ features=_SCHEMA,
97
+ supervised_keys=None,
98
+ homepage=_HOMEPAGE,
99
+ citation=_CITATION,
100
+ license=_LICENSE,
101
+ )
102
+
103
+
104
+ def _split_generators(self, dl_manager) -> List[datasets.SplitGenerator]:
105
+ """Returns SplitGenerators."""
106
+ urls = _URLS[_DATASETNAME]
107
+ data_dir = dl_manager.download_and_extract(urls)
108
+
109
+ return [
110
+ datasets.SplitGenerator(
111
+ name=datasets.Split.TRAIN,
112
+ # Whatever you put in gen_kwargs will be passed to _generate_examples
113
+ gen_kwargs={
114
+ "data_dir": data_dir,
115
+ },
116
+ ),
117
+ ]
118
+
119
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
120
+
121
+ def _generate_examples(self, data_dir) -> Tuple[int, Dict]:
122
+ """Yields examples as (key, example) tuples."""
123
+ data_dir = Path(data_dir)
124
+ gold_file = data_dir / "data" / "companies_gold.json"
125
+ crowd_file = data_dir / "data" / "companies_crowd.json"
126
+
127
+ with open(gold_file, "r") as f:
128
+ gold = json.load(f)["sentences"]
129
+
130
+ with open(crowd_file, "r") as f:
131
+ crowd = json.load(f)["sentences"]
132
+
133
+
134
+ for idx_example, (gold, crowd) in enumerate(zip(gold, crowd)):
135
+ assert gold["sentence"] == crowd["sentence"]
136
+
137
+ yield idx_example, {
138
+ "id": str(idx_example),
139
+ "official_split": gold["official_split"],
140
+ "tokens": [i["word"] for i in gold["svpLabels"]],
141
+ "tags_gold": [i["label"] for i in gold["svpLabels"]],
142
+ "tags": [i["label"] for i in crowd["svpLabels"]],
143
+ "classification": gold["classification"],
144
+ "sentence": gold["sentence"],
145
+ }
146
+
147
+ if __name__ == '__main__':
148
+ breakpoint()
149
+ data = datasets.load_dataset(__file__)
150
+ import numpy as np
151
+ np.mean([i['tags_gold'] == i["tags_crowd"] for i in data['train']])