Debian Codegraph @

tests/tagger/__init__.py

raw
1 #!/usr/bin/python3
2 # -*- coding: utf-8 -*-
3
4 import functools
5 import unittest
6 import tempfile
7 from os import devnull
8 from os.path import join as pjoin
9 from os.path import abspath, dirname
10 from subprocess import check_call, Popen, PIPE, CalledProcessError
11 from sys import stderr
12
13
14 import signal
15
16
17 # Utilities
18 def tmp(contents):
19 t = tempfile.NamedTemporaryFile(mode='w', delete=False)
20 t.write(contents)
21 return t.name
22
23
24 def rel(fn):
25 return abspath(pjoin(dirname(abspath(__file__)), fn))
26
27
28 DEBUG = False
29 APERTIUM_TAGGER = rel("../../apertium/apertium-tagger")
30 APERTIUM_FILTER_AMBIGUITY = rel("../../apertium/apertium-filter-ambiguity")
31
32
33 def check_output(*popenargs, **kwargs):
34 # Essentially a copypasted version of check_output with input backported
35 # for Python 3.2 Can be significantly abridged with Python 3.5's run(...)
36 # or Python 3.3's check_output. Please remove and use stdlib version when
37 # Python 3.2/Wheezy is deprecated.
38 if 'stdout' in kwargs:
39 raise ValueError('stdout argument not allowed, it will be overridden.')
40 if 'input' in kwargs:
41 if 'stdin' in kwargs:
42 raise ValueError('stdin and input arguments may not both be used.')
43 inputdata = kwargs['input']
44 del kwargs['input']
45 kwargs['stdin'] = PIPE
46 else:
47 inputdata = None
48 with Popen(*popenargs, stdout=PIPE, **kwargs) as process:
49 try:
50 out, unused_err = process.communicate(inputdata)
51 except:
52 process.kill()
53 process.wait()
54 raise
55 retcode = process.poll()
56 if retcode:
57 raise CalledProcessError(retcode, process.args, output=out)
58 return out
59
60
61 def check_stderr(*popenargs, **kwargs):
62 # Essentially a copypasted version of check_output.
63 # Can be significantly abridged with Python 3.5's run(...)
64 if 'stderr' in kwargs:
65 raise ValueError('stderr argument not allowed, it will be overridden.')
66 if 'input' in kwargs:
67 if 'stdin' in kwargs:
68 raise ValueError('stdin and input arguments may not both be used.')
69 inputdata = kwargs['input']
70 del kwargs['input']
71 kwargs['stdin'] = PIPE
72 else:
73 inputdata = None
74 with Popen(*popenargs, stderr=PIPE, **kwargs) as process:
75 try:
76 unused_output, err = process.communicate(inputdata)
77 except:
78 process.kill()
79 process.wait()
80 raise
81 retcode = process.poll()
82 if retcode:
83 raise CalledProcessError(retcode, process.args, output=err)
84 return err
85
86
87 def trace_dec(f):
88 @functools.wraps(f)
89 def inner(*args, **kwargs):
90 if len(args) > 0 and DEBUG:
91 print("run " + " ".join(args[0]))
92 return f(*args, **kwargs)
93 return inner
94
95
96 def trace_plus_unicode(f):
97 return functools.partial(trace_dec(f), universal_newlines=True)
98
99
100 check_call = trace_plus_unicode(check_call)
101 check_output = trace_plus_unicode(check_output)
102 check_stderr = trace_plus_unicode(check_stderr)
103
104 # Test files
105 DIC = """
106 ^the/the<det><def><sp>$
107 ^books/book<n><pl>/book<vblex><pri><p3><sg>$
108 ^has/have<vbhaver><pres><p3><sg>$
109 ^booked/book<vblex><pp>/book<vblex><past>$
110 ^close/close<adj><sint>/close<n><sg>/close<vblex><inf>/close<vblex><pres>/close<vblex><imp>$
111 ^cat/cat<n><sg>$
112 ^room/room<n><sg>$
113 ^red/red<adj><sint>$
114 ^./.<sent>$
115 """.strip()
116
117 TSX = """
118 <?xml version="1.0" encoding="utf-8"?>
119 <tagger name="test">
120 <tagset>
121 <def-label name="DET" closed="true">
122 <tags-item tags="det.*"/>
123 <tags-item tags="det.*.*"/>
124 </def-label>
125 <def-label name="VERB">
126 <tags-item tags="vblex.*"/>
127 <tags-item tags="vbhaver.*"/>
128 </def-label>
129 <def-label name="NOUN">
130 <tags-item tags="n.*"/>
131 </def-label>
132 <def-label name="ADJ">
133 <tags-item tags="adj.*"/>
134 <tags-item tags="adj"/>
135 </def-label>
136 </tagset>
137 </tagger>
138 """.strip()
139
140 TRAIN_NO_PROBLEM_UNTAGGED = """
141 ^The/the<det><def><sp>$
142 ^cat/cat<n><sg>$
143 ^books/book<n><pl>/book<vblex><pri><p3><sg>$
144 ^the/the<det><def><sp>$
145 ^room/room<n><sg>$
146 ^./.<sent>$
147
148 ^The/the<det><def><sp>$
149 ^red/red<adj><sint>$
150 ^cat/cat<n><sg>$
151 ^books/book<n><pl>/book<vblex><pri><p3><sg>$
152 ^the/the<det><def><sp>$
153 ^red/red<adj><sint>$
154 ^room/room<n><sg>$
155 ^./.<sent>$
156
157 ^The/the<det><def><sp>$
158 ^red/red<adj><sint>$
159 ^cat/cat<n><sg>$
160 ^books/book<n><pl>/book<vblex><pri><p3><sg>$
161 ^the/the<det><def><sp>$
162 ^room/room<n><sg>$
163 ^./.<sent>$
164 """.strip()
165
166 TRAIN_NO_PROBLEM_TAGGED = """
167 ^The/the<det><def><sp>$
168 ^cat/cat<n><sg>$
169 ^books/book<vblex><pri><p3><sg>$
170 ^the/the<det><def><sp>$
171 ^room/room<n><sg>$
172 ^./.<sent>$
173
174 ^The/the<det><def><sp>$
175 ^red/red<adj><sint>$
176 ^cat/cat<n><sg>$
177 ^books/book<vblex><pri><p3><sg>$
178 ^the/the<det><def><sp>$
179 ^red/red<adj><sint>$
180 ^room/room<n><sg>$
181 ^./.<sent>$
182
183 ^The/the<det><def><sp>$
184 ^red/red<adj><sint>$
185 ^cat/cat<n><sg>$
186 ^books/book<vblex><pri><p3><sg>$
187 ^the/the<det><def><sp>$
188 ^room/room<n><sg>$
189 ^./.<sent>$
190 """.strip()
191
192 TRAIN_CAT_TO_BE_A_VERB_UNTAGGED = """
193 ^The/The<det><def><sp>$
194 ^falling/fall<vblex><pprs>/fall<vblex><ger>/fall<vblex><subs>$
195 ^cat/cat<n><sg>$
196 ^has/have<vbhaver><pres><p3><sg>$
197 ^booked/book<vblex><pp>/book<vblex><past>$
198 ^books/book<n><pl>/book<vblex><pres><p3><sg>$
199 ^./.<sent>$
200
201 ^Close/close<adj><sint>/close<n><sg>/close<vblex><inf>/close<vblex><pres>/close<vblex><imp>$
202 ^the/the<det><def><sp>$
203 ^books/book<n><pl>/book<vblex><pri><p3><sg>$
204 ^./.<sent>$
205
206 ^The/the<det><def><sp>$
207 ^falling/fall<vblex><pprs>/fall<vblex><ger>/fall<vblex><subs>$
208 ^cat/cat<n><sg>$
209 ^has/have<vbhaver><pres><p3><sg>$
210 ^books/book<n><pl>/book<vblex><pres><p3><sg>$
211 ^./.<sent>$
212 """.strip()
213
214 TRAIN_CAT_TO_BE_A_VERB_TAGGED = """
215 ^The/The<det><def><sp>$
216 ^falling/fall<vblex><pprs>$
217 ^cat/cat<n><sg>$
218 ^has/have<vbhaver><pres><p3><sg>$
219 ^booked/book<vblex><pp>$
220 ^books/book<n><pl>$
221 ^./.<sent>$
222
223 ^Close/close<vblex><imp>$
224 ^the/the<det><def><sp>$
225 ^books/book<n><pl>$
226 ^./.<sent>$
227
228 ^The/the<det><def><sp>$
229 ^falling/fall<vblex><pprs>$
230 ^cat/cat<n><sg>$
231 ^has/have<vbhaver><pres><p3><sg>$
232 ^books/book<n><pl>$
233 ^./.<sent>$
234 """.strip()
235
236 TEST_SUCCESS = """
237 ^The/the<det><def><sp>$
238 ^cat/cat<n><sg>$
239 ^books/book<n><pl>/book<vblex><pri><p3><sg>$
240 ^the/the<det><def><sp>$
241 ^room/room<n><sg>$
242 ^./.<sent>$
243 """.strip()
244
245 TEST_NEW_AMBG_CLASS = """
246 ^The/the<det><def><sp>$
247 ^cat/cat<n><sg>/cat<adj>$
248 ^books/book<n><pl>/book<vblex><pri><p3><sg>$
249 ^the/the<det><def><sp>$
250 ^room/room<n><sg>$
251 ^./.<sent>$
252 """.strip()
253
254 # Expected strings
255 EXPECTED_SUBST = """
256 Error: A new ambiguity class was found.
257 Retraining the tagger is necessary so as to take it into account.
258 Word 'cat'.
259 New ambiguity class: {NOUN,ADJ}
260 """.strip().split("\n")
261
262
263 # Tests
264 class AmbiguityClassTest(unittest.TestCase):
265 def setUp(self):
266 self.tsx_fn = tmp(TSX)
267 self.dic_fn = tmp(DIC)
268 self.devnull = open(devnull, 'w')
269
270 def changing_class_impl(self, flags, model_fn):
271 test1 = tmp(TEST_SUCCESS)
272 test2 = tmp(TEST_NEW_AMBG_CLASS)
273 success_stderr = check_stderr(
274 [APERTIUM_TAGGER, '-d'] + flags +
275 ['-g', model_fn, test1],
276 stdout=self.devnull)
277 self.assertEqual(success_stderr.strip(), "")
278 subst_stderr = check_stderr(
279 [APERTIUM_TAGGER, '-d'] + flags +
280 ['-g', model_fn, test2],
281 stdout=self.devnull)
282 subst_stderr = [line.strip()
283 for line in subst_stderr.strip().split("\n")]
284 self.assertEqual(subst_stderr, EXPECTED_SUBST)
285 ambg_class = check_output(
286 [rel('test-find-similar-ambiguity-class'), model_fn],
287 input="NOUN ADJ\n")
288 substituted_class = set(ambg_class.split(" "))
289 # Should get open class
290 self.assertSetEqual(substituted_class, set(("VERB", "NOUN", "ADJ")))
291
292 def test_changing_class_hmm_sup(self):
293 model_fn = tmp("")
294 untagged = tmp(TRAIN_NO_PROBLEM_UNTAGGED)
295 tagged = tmp(TRAIN_NO_PROBLEM_TAGGED)
296 check_call(
297 [APERTIUM_TAGGER, '-s', '0', self.dic_fn, untagged, self.tsx_fn,
298 model_fn, tagged, untagged],
299 stderr=self.devnull)
300 self.changing_class_impl([], model_fn)
301
302 def test_changing_class_hmm_unsup(self):
303 model_fn = tmp("")
304 untagged = tmp(TRAIN_NO_PROBLEM_UNTAGGED)
305 check_call(
306 [APERTIUM_TAGGER, '-t', '1', self.dic_fn, untagged, self.tsx_fn,
307 model_fn],
308 stderr=self.devnull)
309 self.changing_class_impl([], model_fn)
310
311 def test_changing_class_sliding_window(self):
312 model_fn = tmp("")
313 untagged = tmp(TRAIN_NO_PROBLEM_UNTAGGED)
314 check_call(
315 [APERTIUM_TAGGER, '--sliding-window', '-t', '1', self.dic_fn,
316 untagged, self.tsx_fn, model_fn],
317 stderr=self.devnull)
318 self.changing_class_impl(['--sliding-window'], model_fn)
319
320 def test_cat_is_a_verb(self):
321 model_fn = tmp("")
322 untagged = tmp(TRAIN_CAT_TO_BE_A_VERB_UNTAGGED)
323 tagged = tmp(TRAIN_CAT_TO_BE_A_VERB_TAGGED)
324 new_ambg_class = tmp(TEST_NEW_AMBG_CLASS)
325 check_call(
326 [APERTIUM_TAGGER, '-s', '0', self.dic_fn, untagged, self.tsx_fn,
327 model_fn, tagged, untagged],
328 stderr=self.devnull)
329 subst_stdout = check_output(
330 [APERTIUM_TAGGER, '-d', '-g', model_fn, new_ambg_class],
331 stderr=self.devnull)
332 acceptable = False
333 for line in subst_stdout.split("\n"):
334 if (line.startswith('^cat') and ('<adj>' in line or '<n>' in line)):
335 acceptable = True
336 self.assertTrue(
337 acceptable,
338 "'cat' must be output and tagged as an adjective or a noun.\n" +
339 "Actual output:\n{}".format(subst_stdout))
340
341 @unittest.expectedFailure
342 def test_g_vs_plus(self):
343 inp = tmp("""
344 ^TV-karriere/TV<np><al><cmp>+karriere<n><m><sg><ind>/Tv<n><m><sg><ind><cmp>+karriere<n><m><sg><ind>$
345 """.strip())
346 subst_stdout = check_output(
347 [APERTIUM_TAGGER, '-g', "data/nob.prob", inp],
348 stderr=self.devnull)
349 out = subst_stdout.split("\n")
350 wantanyof = {'^TV<np><al><cmp>+karriere<n><m><sg><ind>$',
351 '^Tv<n><m><sg><ind><cmp>+karriere<n><m><sg><ind>$'}
352 acceptable = set(out).intersection(wantanyof)
353 msg = ("Couldn't find any of \n{}\nin output.\n" +
354 "Actual output:\n {}\n").format(wantanyof,
355 subst_stdout)
356 self.assertTrue(bool(acceptable), msg)
357
358 def test_undefined_mult_vs_plus(self):
359 inp = tmp("""
360 ^yz/y<n><f><cmp>+z<n><ut>$
361 """.strip())
362 subst_stdout = check_output(
363 [APERTIUM_TAGGER, '-g', "data/nob.prob", inp],
364 stderr=self.devnull)
365 out = subst_stdout.split("\n")
366 wantanyof = {'^y<n><f><cmp>+z<n><ut>$',
367 }
368 acceptable = set(out).intersection(wantanyof)
369 msg = ("Couldn't find any of \n{}\nin output.\n" +
370 "Actual output:\n {}\n").format(wantanyof,
371 subst_stdout)
372 self.assertTrue(bool(acceptable), msg)
373
374
375 class FilterAmbiguityTest(unittest.TestCase):
376 def test_unicode_tsx(self):
377 stderrpath = tmp('')
378 inputpath = tmp('^아/아<noun>$\n')
379 output = check_output(
380 [APERTIUM_FILTER_AMBIGUITY, rel('unicode.tsx'), inputpath],
381 stderr=open(stderrpath, 'w'))
382 self.assertEqual('^아/아<noun>$\n', output)
383 stderr = open(stderrpath, 'r').read().strip()
384 self.assertEqual('', stderr)
385
386 class Alarm(Exception):
387 pass
388
389 class PerceptronNullFlushTest(unittest.TestCase):
390
391 inputs = [
392 "^this/this<prn><dem><mf><sg>$ ^here/here<adv>$",
393 "^is/be<vbser><pres><p3><sg>$ ^a/a<det><ind><sg>$",
394 "^little/little<adj>/little<adv>$",
395 "^flushing/*flushing$ ^test/test<n><sg>/test<vblex><inf>/test<vblex><pres>/test<vblex><imp>$"
396 ]
397
398 outputs = [
399 "^this<prn><dem><mf><sg>$ ^here<adv>$",
400 "^be<vbser><pres><p3><sg>$ ^a<det><ind><sg>$",
401 "^little<adv>$",
402 "^*flushing$ ^test<n><sg>$"
403 ]
404
405
406 def alarmHandler(self, signum, frame):
407 raise Alarm
408
409 def withTimeout(self, seconds, cmd, *args, **kwds):
410 signal.signal(signal.SIGALRM, self.alarmHandler)
411 signal.alarm(seconds)
412 ret = cmd(*args, **kwds)
413 signal.alarm(0) # reset the alarm
414 return ret
415
416 def communicateFlush(self, string, process):
417 if string:
418 process.stdin.write(string.encode('utf-8'))
419 process.stdin.write(b'\0')
420 process.stdin.flush()
421
422 output = []
423 char = None
424 try:
425 char = self.withTimeout(2, process.stdout.read, 1)
426 except Alarm:
427 print("Timeout before reading a single character!", file=stderr)
428 while char and char != b'\0':
429 output.append(char)
430 try:
431 char = self.withTimeout(2, process.stdout.read, 1)
432 except Alarm:
433 print("Timeout before reading %s chars" % len(output),
434 file=stderr)
435 break # send what we got up till now
436
437 return b"".join(output).decode('utf-8').replace('\r\n', '\n')
438
439 def test_null_flush(self):
440 try:
441 proc = Popen([APERTIUM_TAGGER, '-xg', '-z', "data/eng.prob"],
442 stdin=PIPE,
443 stdout=PIPE,
444 stderr=PIPE)
445
446 for inp, exp in zip(self.inputs, self.outputs):
447 output = self.communicateFlush(inp+"[][\n]", proc)
448 with self.subTest(input_line=inp):
449 self.assertEqual(output, exp+"[][\n]")
450
451 proc.communicate() # let it terminate
452 proc.stdin.close()
453 proc.stdout.close()
454 proc.stderr.close()
455 retCode = proc.poll()
456 self.assertEqual(retCode, 0)
457
458 finally:
459 pass