-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtranslationese_filter.py
267 lines (245 loc) · 8.69 KB
/
translationese_filter.py
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
# -*- coding: utf-8 -*-
import os
import argparse
from lxml import etree
import fnmatch # To match files by pattern
import time
import re
def timeit(method):
"""Time methods."""
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
print('%r %2.2f sec' %
(method.__name__, te-ts))
return result
return timed
class FilterOutTranslationese(object):
"""Get proceedings of the European Parliament."""
@timeit
def __init__(self):
self.langs = {
"bg",
"es",
"cs",
"da",
"de",
"et",
"el",
"en",
"fr",
"ga",
"hr",
"it",
"lv",
"lt",
"hu",
"mt",
"nl",
"pl",
"pt",
"ro",
"sk",
"sl",
"fi",
"sv",
}
self.nationalities = {
"bg": ['Bulgaria'],
"es": ['Spain'],
"cs": ['Czech Republic', 'Slovakia'],
"da": ['Denmark'],
"de": ['Germany', 'Austria', 'Belgium', 'Luxembourg', 'Italy'],
"et": ['Estonia'],
"el": ['Greece'],
"en": ['United Kingdom', 'Ireland', 'Malta'],
"fr": ['France', 'Belgium', 'Luxembourg', 'Italy'],
"ga": ['Ireland', 'United Kingdom'],
"hr": ['Croatia'],
"it": ['Italy', 'Croatia', 'Slovenia'],
"lv": ['Latvia'],
"lt": ['Lituania'],
"hu": ['Hungary'],
"mt": ['Malta'],
"nl": ['Netherlands'],
"pl": ['Poland'],
"pt": ['Portugal'],
"ro": ['Romania'],
"sk": ['Slovakia', 'Czech Republic'],
"sl": ['Slovenia'],
"fi": ['Finland'],
"sv": ['Sweden', 'Finland'],
}
self.cli()
self.infiles = self.get_files(self.indir, self.pattern)
self.n_proceedings = 0
self.main()
def __str__(self):
message = "{} EuroParl's {} proceedings filtered!".format(
str(self.n_proceedings),
self.language)
return message
def get_files(self, directory, fileclue):
"""Get all files in a directory matching a pattern.
Keyword arguments:
directory -- a string for the input folder path
fileclue -- a string as glob pattern
"""
matches = []
for root, dirnames, filenames in os.walk(directory):
for filename in fnmatch.filter(filenames, fileclue):
matches.append(os.path.join(root, filename))
return matches
def read_xml(self, infile):
"""Parse a XML file.
Keyword arguments:
infile -- a string for the path to the file to be read.
"""
parser = etree.XMLParser(remove_blank_text=True)
with open(infile, encoding='utf-8', mode='r') as input:
return etree.parse(input, parser)
def serialize(self, infile, root):
"""Serialize Element as XML file.
Keyword arguments:
infile -- a string for the path to the input file processed.
root -- Element to be serialized as XML.
"""
ofile_name = os.path.splitext(os.path.basename(infile))[0]
ofile_path = os.path.join(self.outdir, ofile_name+'.xml')
xml = self.unprettify(root)
with open(ofile_path, mode='w', encoding='utf-8') as ofile:
ofile.write(xml)
pass
def get_langs_to_be_removed(self):
langs_to_be_removed = {'unknown'}
if self.sl != 'all' and self.sl == self.language: # originals
langs_to_be_removed.update(self.langs.difference({self.sl}))
elif self.sl != 'all' and self.sl != self.language: # translations_SL
langs_to_be_removed.update({self.language})
langs_to_be_removed.update(self.langs.difference({self.sl}))
elif self.sl == 'all': # translations_all
langs_to_be_removed.update({self.language})
return langs_to_be_removed
def remove_element(self, element):
parent = element.getparent()
parent.remove(element)
if len(parent) == 0:
parent.getparent().remove(parent)
pass
def unprettify(self, tree):
"""Remove any indentation introduced by pretty print."""
tree = etree.tostring( # convert XML tree to string
tree,
encoding="utf-8",
method="xml",
xml_declaration=True).decode()
tree = re.sub( # remove trailing spaces before tag
r"(\n) +(<)",
r"\1\2",
tree)
tree = re.sub( # put each XML element in a different line
r"> *<",
r">\n<",
tree)
tree = re.sub( # put opening tag and FL output in different lines
r"(<.+?>)",
r"\1\n",
tree)
tree = re.sub( # put FL output and closing tag in different liens
r"(</.+?>)",
r"\n\1",
tree)
tree = re.sub(
r"(>)([^.])",
r"\1\n\2",
tree)
tree = re.sub( # remove unnecessary empty lines
r"\n\n+",
r"\n",
tree)
return tree
def main(self):
for infile in self.infiles:
print(infile)
tree = self.read_xml(infile)
root = tree.getroot()
self.language = root.attrib['lang']
self.langs_to_be_removed = self.get_langs_to_be_removed()
elements = tree.xpath('//{}'.format(self.element))
for e in elements:
if self.native is False:
if e.attrib['sl'].lower() in self.langs_to_be_removed:
self.remove_element(e)
else:
if self.sl != 'all':
if e.attrib['sl'].lower() in self.langs_to_be_removed:
self.remove_element(e)
elif 'nationality' not in e.getparent().attrib:
self.remove_element(e)
elif (e.attrib['sl'] == self.sl and
e.getparent().attrib['nationality']
not in self.nationalities[self.sl]):
self.remove_element(e)
else:
if e.attrib['sl'].lower() in self.langs_to_be_removed:
self.remove_element(e)
elif 'nationality' not in e.getparent().attrib:
self.remove_element(e)
else:
if (e.getparent().attrib['nationality'] not in
self.nationalities[e.attrib['sl'].lower()]):
self.remove_element(e)
self.serialize(infile, root)
self.n_proceedings += 1
pass
def cli(self):
"""CLI parses command-line arguments"""
parser = argparse.ArgumentParser()
parser.add_argument(
"-i", "--input",
required=True,
help="path to the input directory.")
parser.add_argument(
"-o", "--output", # originals
required=True,
help="path to the output directory.")
parser.add_argument(
"-l", "--language",
required=True,
choices=list(self.langs)+['all'],
help="source language(s).")
parser.add_argument(
"-n", "--native",
required=False,
default=False,
action="store_true",
help="filter out speeches by non-native speakers.")
parser.add_argument(
'-p', "--pattern",
required=False,
default="*.xml",
help="glob pattern to filter files.")
parser.add_argument(
'-e', "--element",
required=False,
default='p',
help="Element containing the language attribute.")
parser.add_argument(
'-u', "--unprettify",
required=False,
default=False,
action="store_true",
help="unprettify XML output to get one element per line and\
no indentation.")
args = parser.parse_args()
self.indir = args.input
self.outdir = args.output
if not os.path.exists(self.outdir):
os.makedirs(self.outdir)
self.sl = args.language
self.pattern = args.pattern
self.element = args.element
self.native = args.native
pass
print(FilterOutTranslationese())