commit a29686d3907313bf12fc08ecfe5c64d44f65b7c8 Author: Amr Keleg Date: Tue Aug 20 15:55:58 2019 +0200 Add unit tests for lt-weight module diff --git a/tests/data/weightlists/analyses-comp-mono b/tests/data/weightlists/analyses-comp-mono new file mode 100644 index 0000000..f559431 --- /dev/null +++ b/tests/data/weightlists/analyses-comp-mono @@ -0,0 +1 @@ +[b % %]::0.003 \ No newline at end of file diff --git a/tests/data/weightlists/default b/tests/data/weightlists/default new file mode 100644 index 0000000..d7475aa --- /dev/null +++ b/tests/data/weightlists/default @@ -0,0 +1 @@ +[?*]::0.005 \ No newline at end of file diff --git a/tests/data/weightlists/tags-cat-multiple-fst b/tests/data/weightlists/tags-cat-multiple-fst new file mode 100644 index 0000000..9bf157f --- /dev/null +++ b/tests/data/weightlists/tags-cat-multiple-fst @@ -0,0 +1,2 @@ +[?* %+ n]::0.004 +[?* %+ v]::0.0045 \ No newline at end of file diff --git a/tests/data/weightlists/tags-comp-mono b/tests/data/weightlists/tags-comp-mono new file mode 100644 index 0000000..e73f01f --- /dev/null +++ b/tests/data/weightlists/tags-comp-mono @@ -0,0 +1 @@ +[?* %]::0.004 \ No newline at end of file diff --git a/tests/lt_weight/__init__.py b/tests/lt_weight/__init__.py new file mode 100644 index 0000000..876e41f --- /dev/null +++ b/tests/lt_weight/__init__.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +import sys +import unittest +from weighttest import WeightTest + +class WeigthedTransducerMatchSameInput(unittest.TestCase, WeightTest): + weightlists = ["data/weightlists/default"] + inputs = ["ab", + "ABC jg", + "jh kg", + "y n"] + expectedOutputs = ["^ab/ab$", + "^ABC/AB$ ^jg/j+g$", + "^jh/j+h$ ^kg/k+g$", + "^y/y$ ^n/n$"] + + +class WeigthedTransducerMatchSameInputWithCorrectWeight(unittest.TestCase, WeightTest): + # TODO: Investigate why is this case failing? + # Is hfst approximating the floating values? + weightlists = ["data/weightlists/default"] + procflags = ["-z", "-W"] + inputs = ["ab"] + expectedOutputs = ["^ab/ab$"] + +class MultipleWeightlists(unittest.TestCase, WeightTest): + # Fails because lt-print fails to print the base un-weighted fst + compdix = "data/cat-multiple-fst.att" + weightlists = ["data/weightlists/tags-cat-multiple-fst", + "data/weightlists/default"] + procflags = ["-z", "-W"] + inputs = ["cat", + "cats"] + expectedOutputs = ["^cat/cat+n/cat+v$", + "^cats/cat+n+$"] + +class WeightAnalysisUsingTheFirstMatchingWeightlist(unittest.TestCase, WeightTest): + compdix = 'data/cmp-mono.dix' + weightlists =["data/weightlists/analyses-comp-mono", + "data/weightlists/tags-comp-mono"] + inputs = ["b"] + expectedOutputs = ["^b/b/b$"] diff --git a/tests/run_tests.py b/tests/run_tests.py index c394422..205dbab 100755 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -5,12 +5,12 @@ import os sys.path.append(os.path.realpath(".")) import unittest -import lt_proc, lt_trim, lt_print +import lt_proc, lt_trim, lt_print, lt_weight if __name__ == "__main__": os.chdir(os.path.dirname(__file__)) failures = 0 - for module in [lt_trim, lt_proc, lt_print]: + for module in [lt_trim, lt_proc, lt_print, lt_weight]: suite = unittest.TestLoader().loadTestsFromModule(module) res = unittest.TextTestRunner(verbosity = 2).run(suite) failures += len(res.failures) diff --git a/tests/weighttest.py b/tests/weighttest.py new file mode 100644 index 0000000..55cf6aa --- /dev/null +++ b/tests/weighttest.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +from subprocess import Popen, PIPE, call +from tempfile import mkdtemp +from shutil import rmtree +from pathlib import Path +import itertools +from basictest import BasicTest + +class WeightTest(BasicTest): + compdix = "data/minimal-mono.dix" + compdir = "lr" + weightlists = [] + procflags = ["-z"] + inputs = itertools.repeat("") + expectedOutputs = itertools.repeat("") + + def compileTest(self, compiled_dix): + self.assertEqual(0, call(["../lttoolbox/lt-comp", + self.compdir, + self.compdix, + compiled_dix], + stdout=PIPE)) + def runTest(self): + tmpd = mkdtemp() + compiled_dix = Path(tmpd, "compiled.bin") + weighted_dix = Path(tmpd, "weighted.bin") + + try: + self.compileTest(compiled_dix) + self.assertEqual(0, call(["../scripts/lt-weight"] + + [compiled_dix, weighted_dix] + + self.weightlists, + stdout=PIPE)) + self.proc = Popen(["../lttoolbox/lt-proc"] + + self.procflags + + [weighted_dix], + stdin=PIPE, + stdout=PIPE, + stderr=PIPE) + + self.assertEqual(len(self.inputs), + len(self.expectedOutputs)) + + for inp, exp in zip(self.inputs, self.expectedOutputs): + output = self.communicateFlush(inp+"[][\n]", self.proc) + self.assertEqual(output, + exp+"[][\n]") + + self.proc.communicate() # let it terminate + self.proc.stdin.close() + self.proc.stdout.close() + self.proc.stderr.close() + retCode = self.proc.poll() + + finally: + rmtree(tmpd) + pass