-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlangid_filter.py
More file actions
189 lines (167 loc) · 6.11 KB
/
langid_filter.py
File metadata and controls
189 lines (167 loc) · 6.11 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
# -*- coding: utf-8 -*-
import os
import argparse
from lxml import etree
import fnmatch
import time
from langdetect import detect_langs
from langdetect.lang_detect_exception import LangDetectException
from langid.langid import LanguageIdentifier, model
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 FilterOutUnexpectedLanguage(object):
"""Identify language of a given string."""
@timeit
def __init__(self):
self.cli()
self.infiles = self.get_files(self.indir, self.pattern)
self.n_proceedings = 0
self.identifier = LanguageIdentifier.from_modelstring(
model,
norm_probs=True)
self.main()
def __str__(self):
message = "{} EuroParl's {} proceedings language-identified!".format(
str(self.n_proceedings),
self.expected)
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 = etree.tostring(
root,
encoding='utf-8',
xml_declaration=True,
pretty_print=True).decode('utf-8')
with open(ofile_path, mode='w', encoding='utf-8') as ofile:
ofile.write(xml)
pass
def get_parent(self, element):
"""Get parent of a XML Element."""
parent = element.getparent()
text = etree.tostring(parent, method='text', encoding='utf-8').decode()
return text
def is_expected(self, element):
"""Test if languge of Element's text is the expected language.
Keyword arguments:
element -- Element whose text is to be analyzed.
"""
text = etree.tostring(
element,
method='text',
encoding='utf-8').decode()
try:
ld = detect_langs(text)
except LangDetectException:
if element.getparent().tag == 'intervention':
text = self.get_parent(element)
ld = detect_langs(text)
else:
output = None
return output
li = self.identifier.classify(text)
if (li[0] == ld[0].lang) and (li[0] == self.expected):
output = True
elif (li[0] != self.expected and li[1] == 1) and li[0] == ld[0].lang:
output = False
elif (li[0] != self.expected and li[1] > 0.99) and li[0] == ld[0].lang:
output = False
elif li[0] == self.expected and self.expected in [x.lang for x in ld]:
output = True
elif (li[0] == self.expected and self.expected not in
[x.lang for x in ld]):
output = True
elif (li[0] != self.expected and li[1] == 1) and li[0] != ld[0].lang:
output = False
elif ((li[0] != self.expected and li[1] < 0.9) and
ld[0].lang == self.expected):
output = True
else:
output = None
if output is None:
if element.getparent().tag == 'intervention':
output = self.is_expected(element.getparent())
else:
output = None
return output
def main(self):
for infile in self.infiles:
print(infile)
tree = self.read_xml(infile)
root = tree.getroot()
self.expected = root.attrib['lang']
elements = tree.xpath('//{}'.format(self.text))
for e in elements:
is_expected = self.is_expected(e)
if is_expected is False or is_expected is None:
parent = e.getparent()
parent.remove(e)
if len(parent) == 0:
parent.getparent().remove(parent)
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(
"-t", "--text",
required=False,
default="p",
help="name of the XML element containing the text to be analyzed.\
Default: 'p'.")
parser.add_argument(
'-p', "--pattern",
required=False,
default="*.xml",
help="glob pattern to filter files.")
args = parser.parse_args()
self.indir = args.input
self.outdir = args.output
if not os.path.exists(self.outdir):
os.makedirs(self.outdir)
self.pattern = args.pattern
self.text = args.text
self.pattern = args.pattern
pass
print(FilterOutUnexpectedLanguage())