forked from bbc/citron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_trimmer.py
More file actions
286 lines (213 loc) · 9.13 KB
/
data_trimmer.py
File metadata and controls
286 lines (213 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Copyright 2021 BBC
# Authors: Chris Newell <chris.newell@bbc.co.uk>
#
# License: Apache-2.0
"""
This application removes sentences from Citron format data files which have missing
annotations. The sentences are identified by using a Cue Classifier to predict the
location of quote cues and then comparing these against the annotations.
The approach was suggested in the following paper to deal with missing annotations
data in the PARC 3.0 dataset:
Sylvia Pareti, Tim O'Keefe, Ioannis Konstas, James R Curran and Irena Koprinska. 2013.
“Automatically Detecting and Attributing Indirect Quotations”. Proceedings of the Conference
on Empirical Methods in Natural Language Processing (EMNLP), Seattle, U.S.
http://www.aclweb.org/anthology/D13-1101
"""
import argparse
import logging
import json
import sys
import os
from citron.data import CitronParser
from citron.cue import CueClassifier
from citron.logger import logger
from citron import utils
omitted_sentences = 0
def main():
global omitted_sentences
parser = argparse.ArgumentParser(
description="Data trimmer",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument("-v",
action = "store_true",
default = False,
help = "Verbose mode"
)
parser.add_argument("--input-path",
metavar = "input_path",
type = str,
required=True,
help = "Path to directory of Citron format files"
)
parser.add_argument("--output-path",
metavar = "output_path",
type = str,
required=True,
help = "Path of output directory"
)
parser.add_argument("--model-path",
metavar = "model_path",
type = str,
required=True,
help = "Path to Citron model directory"
)
args = parser.parse_args()
if args.v:
logger.setLevel(logging.DEBUG)
logger.info("Input path: %s", args.input_path)
logger.info("Output path: %s", args.output_path)
nlp = utils.get_parser()
parser = CitronParser(nlp)
cue_classifier = CueClassifier(args.model_path)
input_file_paths = utils.get_files(args.input_path)
logger.info("Found %s data files", len(input_file_paths))
omitted_sentences = 0
for input_file_path in input_file_paths:
logger.debug("Input file: %s", input_file_path)
subpath = input_file_path[len(args.input_path):]
if len(subpath) == 0:
output_file_path = args.output_path
else:
if subpath[0] == "/":
subpath = subpath[1:]
output_file_path = os.path.join(args.output_path, subpath)
end = output_file_path.rfind("/")
parent_directory = output_file_path[:end]
if not os.path.exists(parent_directory):
os.makedirs(parent_directory)
data = parser.parse(input_file_path)
old_to_new_index = get_old_to_new_index(data, cue_classifier)
revise_datafile(input_file_path, output_file_path, old_to_new_index)
logger.info("Omitted_sentences: %s", omitted_sentences)
def get_old_to_new_index(data, cue_classifier):
"""
Get an index mapping original character indices to new indices.
Args:
data: data extracted from a Citron format data file.
cue_classifier: A Cue Classifier object.
Returns:
A list of integers.
"""
global omitted_sentences
doc = data[0]
quotes = data[1]
actual_cue_labels = utils.get_cue_iob_labels(doc, quotes)
actual_source_labels = utils.get_source_iob_labels(doc, quotes)
actual_content_labels = utils.get_content_iob_labels(doc, quotes)
predicted_cue_labels = cue_classifier.predict_cues_and_labels(doc)[1]
old_to_new_index = [None] * len(doc.text)
i = 0
for sentence in doc.sents:
has_actual_cue = utils.span_has_labels(sentence, actual_cue_labels)
has_actual_source = utils.span_has_labels(sentence, actual_source_labels)
has_actual_content = utils.span_has_labels(sentence, actual_content_labels)
has_predicted_cue = utils.span_has_labels(sentence, predicted_cue_labels)
has_annotations = has_actual_cue or has_actual_source or has_actual_content
# Skip sentences with missing annotations
if not has_annotations and has_predicted_cue:
omitted_sentences += 1
continue
for j in range(0, len(sentence.text_with_ws)):
old = sentence.start_char + j
old_to_new_index[old] = i
i += 1
return old_to_new_index
def revise_datafile(input_file_path, output_file_path, old_to_new_index):
"""
Revise a data file to remove sentences and correct the character indices
in the annotations.
Args:
input_file_path: The path of the input file.
output_file_path: The path of the output file.
old_to_new_index: A list of integers mapping character indices to new indices.
"""
with open(input_file_path, encoding="utf-8") as infile:
data = json.load(infile)
corrected_data = {}
text = ""
for old, new in enumerate(old_to_new_index):
if new is not None:
text += data["text"][old]
# Ignore files that are now empty.
if len(text.strip()) == 0:
return
corrected_data["text"] = text
corrected_quotes = []
# Correct quote indices
for quote in data["quotes"]:
corrected_quote = {}
corrected_quote["id"] = quote["id"]
corrected_quote["cue"] = correct_span(quote["cue"], text, old_to_new_index)
sources = []
for source in quote["sources"]:
sources.append(correct_span(source, text, old_to_new_index))
corrected_quote["sources"] = sources
contents = []
for content in quote["contents"]:
contents.append(correct_span(content, text, old_to_new_index))
corrected_quote["contents"] = contents
if "coreferences" in quote:
corrected_quote["coreferences"] = correct_coreferences(quote["coreferences"], text, old_to_new_index)
corrected_quotes.append(corrected_quote)
corrected_data["quotes"] = corrected_quotes
# Correct coreference groups
if "coreference_groups" in data:
corrected_groups = []
for coref_group in data["coreference_groups"]:
corrected_group = correct_coreferences(coref_group, text, old_to_new_index)
if len(corrected_group) > 1:
corrected_groups.append(corrected_group)
corrected_data["coreference_groups"] = corrected_groups
with open(output_file_path, "w", encoding="utf-8") as outfile:
outfile.write(json.dumps(corrected_data, indent=4, sort_keys=False, ensure_ascii=False) + "\n")
def correct_span(span, text, old_to_new_index):
"""
Correct the character indices in a span using the supplied index.
Args:
span: A dict representing a text span.
text: The revised document text (string)
old_to_new_index: A list of integers mapping character indices to new indices.
Returns:
A dict representing a text span.
"""
start = old_to_new_index[span["start"]]
end = old_to_new_index[span["end"]]
span_text = text[start : end]
# Check new character indices refer to the same text.
if span_text != span["text"]:
logger.error("Text mismatch: old: %s new: %s", span["text"], text[start : end])
logger.error("Old start: %s end: %s", span["start"], span["end"])
logger.error("New start: %s end: %s", start, end)
sys.exit(1)
return {"start": start, "end": end, "text": span_text}
def correct_coreferences(coreferences, text, old_to_new_index):
"""
Revise a list of coreferences using the supplied index.
Args:
coreferences: a list of dicts representing text spans.
text: The revised document text (string)
old_to_new_index: A list of integers mapping character indices to new indices.
Returns:
A list of dicts representing text spans.
"""
corrected_coreferences = []
for coreference in coreferences:
start = old_to_new_index[coreference["start"]]
end = old_to_new_index[coreference["end"]]
span_text = text[start : end]
# Ignore coreferences which are in omitted text
if start is None or end is None:
continue
# Check the new character indices refer to the same text
if span_text != coreference["text"]:
print("Error mismatch!")
print("Old text:", coreference["text"])
print("New text:", text[start : end])
print("Old start:", coreference["start"], " end:", coreference["end"])
print("New start:", start, " end:", end)
sys.exit()
corrected_coreferences.append({"start": start, "end": end, "text": span_text})
return corrected_coreferences
if __name__ == '__main__':
main()