kleinay commited on
Commit
05317e6
1 Parent(s): 5e3dfb6

Datasets script for QA-Align

Browse files
Files changed (1) hide show
  1. qa_align.py +156 -0
qa_align.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 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
+ """A Dataset loading script for the QA-Align dataset (Brook Weiss et. al., EMNLP 2021)."""
16
+
17
+
18
+ import datasets
19
+ from pathlib import Path
20
+ from typing import List
21
+ import pandas as pd
22
+ import json
23
+
24
+
25
+ _CITATION = """\
26
+ @inproceedings{brook-weiss-etal-2021-qa,
27
+ title = "{QA}-Align: Representing Cross-Text Content Overlap by Aligning Question-Answer Propositions",
28
+ author = "Brook Weiss, Daniela and
29
+ Roit, Paul and
30
+ Klein, Ayal and
31
+ Ernst, Ori and
32
+ Dagan, Ido",
33
+ booktitle = "Proceedings of the 2021 Conference on Empirical Methods in Natural Language Processing",
34
+ month = nov,
35
+ year = "2021",
36
+ address = "Online and Punta Cana, Dominican Republic",
37
+ publisher = "Association for Computational Linguistics",
38
+ url = "https://aclanthology.org/2021.emnlp-main.778",
39
+ pages = "9879--9894",
40
+ abstract = "Multi-text applications, such as multi-document summarization, are typically required to model redundancies across related texts. Current methods confronting consolidation struggle to fuse overlapping information. In order to explicitly represent content overlap, we propose to align predicate-argument relations across texts, providing a potential scaffold for information consolidation. We go beyond clustering coreferring mentions, and instead model overlap with respect to redundancy at a propositional level, rather than merely detecting shared referents. Our setting exploits QA-SRL, utilizing question-answer pairs to capture predicate-argument relations, facilitating laymen annotation of cross-text alignments. We employ crowd-workers for constructing a dataset of QA-based alignments, and present a baseline QA alignment model trained over our dataset. Analyses show that our new task is semantically challenging, capturing content overlap beyond lexical similarity and complements cross-document coreference with proposition-level links, offering potential use for downstream tasks.",
41
+ }"""
42
+
43
+
44
+ _DESCRIPTION = """\
45
+ This dataset contains QA-Alignments - annotations of cross-text content overlap.
46
+ The task input is two sentences from two documents, roughly talking about the same event, along with their QA-SRL annotations
47
+ which capture verbal predicate-argument relations in question-answer format. The output is a cross-sentence alignment between sets of QAs which denote the same information.
48
+ See the paper for details: QA-Align: Representing Cross-Text Content Overlap by Aligning Question-Answer Propositions, Brook Weiss et. al., EMNLP 2021.
49
+ Here we provide both the QASRL annotations and the QA-Align annotations for the target sentences.
50
+ """
51
+
52
+ _HOMEPAGE = "https://github.com/DanielaBWeiss/QA-ALIGN"
53
+
54
+ _LICENSE = """Resources on this page are licensed CC-BY 4.0, a Creative Commons license requiring Attribution (https://creativecommons.org/licenses/by/4.0/)."""
55
+
56
+
57
+ _URLs = {
58
+ "train": "https://github.com/DanielaBWeiss/QA-ALIGN/raw/master/data/official_qa_alignments/qaalign_train_silver_official.csv",
59
+ "dev": "https://github.com/DanielaBWeiss/QA-ALIGN/raw/master/data/official_qa_alignments/qaalign_dev_gold_official.csv",
60
+ "test": "https://github.com/DanielaBWeiss/QA-ALIGN/raw/master/data/official_qa_alignments/qaalign_test_gold_official.csv",
61
+ }
62
+
63
+ # TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
64
+ class QaAlign(datasets.GeneratorBasedBuilder):
65
+ """QA-Align: Representing Cross-Text Content Overlap by Aligning Question-Answer Propositions. """
66
+
67
+ VERSION = datasets.Version("1.0.0")
68
+
69
+ BUILDER_CONFIGS = [
70
+ datasets.BuilderConfig(
71
+ name="plain_text", version=VERSION, description="This provides the QA-Align dataset"
72
+ ),
73
+ ]
74
+
75
+ DEFAULT_CONFIG_NAME = (
76
+ "plain_text" # It's not mandatory to have a default configuration. Just use one if it make sense.
77
+ )
78
+
79
+ def _info(self):
80
+ # We keep the exact same fields (columns) as the original dataset files (CSV)
81
+ # See https://github.com/DanielaBWeiss/QA-ALIGN/blob/main/data/official_qa_alignments/readme.md for format description
82
+ features = datasets.Features(
83
+ {
84
+ "text_1": datasets.Value("string"),
85
+ "text_2": datasets.Value("string"),
86
+ "prev_text_2": datasets.Value("string"),
87
+ "prev_text_1": datasets.Value("string"),
88
+ "abs_sent_id_2": datasets.Value("string"),
89
+ "abs_sent_id_1": datasets.Value("string"),
90
+ "qas_1": dict,
91
+ "qas_2": dict,
92
+ "alignments": dict,
93
+ }
94
+ )
95
+ return datasets.DatasetInfo(
96
+ # This is the description that will appear on the datasets page.
97
+ description=_DESCRIPTION,
98
+ # This defines the different columns of the dataset and their types
99
+ features=features, # Here we define them above because they are different between the two configurations
100
+ # If there's a common (input, target) tuple from the features,
101
+ # specify them here. They'll be used if as_supervised=True in
102
+ # builder.as_dataset.
103
+ supervised_keys=None,
104
+ # Homepage of the dataset for documentation
105
+ homepage=_HOMEPAGE,
106
+ # License for the dataset if available
107
+ license=_LICENSE,
108
+ # Citation for the dataset
109
+ citation=_CITATION,
110
+ )
111
+
112
+ def _split_generators(self, dl_manager: datasets.utils.download_manager.DownloadManager):
113
+ """Returns SplitGenerators."""
114
+
115
+ # Download and prepare all files - keep same structure as _URLs
116
+ corpora = {section: Path(dl_manager.download_and_extract(_URLs[section]))
117
+ for section in _URLs}
118
+
119
+ return [
120
+ datasets.SplitGenerator(
121
+ name=datasets.Split.TRAIN,
122
+ # These kwargs will be passed to _generate_examples
123
+ gen_kwargs={
124
+ "filepath": corpora["train"]
125
+ },
126
+ ),
127
+ datasets.SplitGenerator(
128
+ name=datasets.Split.VALIDATION,
129
+ # These kwargs will be passed to _generate_examples
130
+ gen_kwargs={
131
+ "filepath": corpora["dev"]
132
+ },
133
+ ),
134
+ datasets.SplitGenerator(
135
+ name=datasets.Split.TEST,
136
+ # These kwargs will be passed to _generate_examples
137
+ gen_kwargs={
138
+ "filepath": corpora["test"]
139
+ },
140
+ ),
141
+ ]
142
+
143
+ def _generate_examples(self, filepath: List[str]):
144
+
145
+ """ Yields QA-Align examples from a csv file. Each instance contains all the alignment for a pair of sentences. """
146
+
147
+ # merge annotations from sections
148
+ df = pd.read_csv(filepath)
149
+ for counter, dic in enumerate(df.to_dict('records')):
150
+ columns_to_load_into_object = ["qas_1", "qas_2", "alignments"]
151
+ for key in columns_to_load_into_object:
152
+ dic[key] = json.loads(dic[key])
153
+ columns_to_remove = ["worker_id","assignment_id","work_time_in_seconds", "year","topic","key","source","source-data","tokens_1","tokens_2"]
154
+ for key in columns_to_remove:
155
+ del dic[key]
156
+ yield counter, dic