summaryrefslogtreecommitdiff
path: root/tests/test_vocabcompiler.py
blob: f9e427f30dec167beb633377cac27bb1c0e17fca (plain)
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
#!/usr/bin/env python2
# -*- coding: utf-8-*-
import unittest
import tempfile
import contextlib
import logging
import shutil
import mock
from client import vocabcompiler


class TestVocabCompiler(unittest.TestCase):

    def testPhraseExtraction(self):
        expected_phrases = ['MOCK']

        mock_module = mock.Mock()
        mock_module.WORDS = ['MOCK']

        with mock.patch('client.brain.Brain.get_modules',
                        classmethod(lambda cls: [mock_module])):
            extracted_phrases = vocabcompiler.get_all_phrases()
        self.assertEqual(expected_phrases, extracted_phrases)

    def testKeywordPhraseExtraction(self):
        expected_phrases = ['MOCK']

        with tempfile.TemporaryFile() as f:
            # We can't use mock_open here, because it doesn't seem to work
            # with the 'for line in f' syntax
            f.write("MOCK\n")
            f.seek(0)
            with mock.patch('%s.open' % vocabcompiler.__name__,
                            return_value=f, create=True):
                extracted_phrases = vocabcompiler.get_keyword_phrases()
        self.assertEqual(expected_phrases, extracted_phrases)


class TestVocabulary(unittest.TestCase):
    VOCABULARY = vocabcompiler.DummyVocabulary

    @contextlib.contextmanager
    def do_in_tempdir(self):
        tempdir = tempfile.mkdtemp()
        yield tempdir
        shutil.rmtree(tempdir)

    def testVocabulary(self):
        phrases = ['GOOD BAD UGLY']
        with self.do_in_tempdir() as tempdir:
            self.vocab = self.VOCABULARY(path=tempdir)
            self.assertIsNone(self.vocab.compiled_revision)
            self.assertFalse(self.vocab.is_compiled)
            self.assertFalse(self.vocab.matches_phrases(phrases))

            # We're now testing error handling. To avoid flooding the
            # output with error messages that are catched anyway,
            # we'll temporarly disable logging. Otherwise, error log
            # messages and traceback would be printed so that someone
            # might think that tests failed even though they succeeded.
            logging.disable(logging.ERROR)
            with self.assertRaises(OSError):
                with mock.patch('os.makedirs', side_effect=OSError('test')):
                    self.vocab.compile(phrases)
            with self.assertRaises(OSError):
                with mock.patch('%s.open' % vocabcompiler.__name__,
                                create=True,
                                side_effect=OSError('test')):
                    self.vocab.compile(phrases)

            class StrangeCompilationError(Exception):
                pass
            with mock.patch.object(self.vocab, '_compile_vocabulary',
                                   side_effect=StrangeCompilationError('test')
                                   ):
                with self.assertRaises(StrangeCompilationError):
                    self.vocab.compile(phrases)
                with self.assertRaises(StrangeCompilationError):
                    with mock.patch('os.remove',
                                    side_effect=OSError('test')):
                        self.vocab.compile(phrases)
            # Re-enable logging again
            logging.disable(logging.NOTSET)

            self.vocab.compile(phrases)
            self.assertIsInstance(self.vocab.compiled_revision, str)
            self.assertTrue(self.vocab.is_compiled)
            self.assertTrue(self.vocab.matches_phrases(phrases))
            self.vocab.compile(phrases)
            self.vocab.compile(phrases, force=True)


@unittest.skipUnless(hasattr(vocabcompiler, 'cmuclmtk'),
                     "CMUCLMTK not present")
class TestPocketsphinxVocabulary(TestVocabulary):

    VOCABULARY = vocabcompiler.PocketsphinxVocabulary

    def testVocabulary(self):
        super(TestPocketsphinxVocabulary, self).testVocabulary()
        self.assertIsInstance(self.vocab.decoder_kwargs, dict)
        self.assertIn('lm', self.vocab.decoder_kwargs)
        self.assertIn('dict', self.vocab.decoder_kwargs)


class TestPatchedPocketsphinxVocabulary(TestPocketsphinxVocabulary):

    def testVocabulary(self):

        def write_test_vocab(text, output_file):
            with open(output_file, "w") as f:
                for word in text.split(' '):
                    f.write("%s\n" % word)

        def write_test_lm(text, output_file, **kwargs):
            with open(output_file, "w") as f:
                f.write("TEST")

        class DummyG2P(object):
            def __init__(self, *args, **kwargs):
                pass

            @classmethod
            def get_config(self, *args, **kwargs):
                return {}

            def translate(self, *args, **kwargs):
                return {'GOOD': ['G UH D',
                                 'G UW D'],
                        'BAD': ['B AE D'],
                        'UGLY': ['AH G L IY']}

        with mock.patch('client.vocabcompiler.cmuclmtk',
                        create=True) as mocked_cmuclmtk:
            mocked_cmuclmtk.text2vocab = write_test_vocab
            mocked_cmuclmtk.text2lm = write_test_lm
            with mock.patch('client.vocabcompiler.PhonetisaurusG2P', DummyG2P):
                super(TestPatchedPocketsphinxVocabulary,
                      self).testVocabulary()