|
3 | 3 | import os
|
4 | 4 | from pocketsphinx import Decoder
|
5 | 5 | import unittest
|
| 6 | +import wave |
6 | 7 |
|
7 | 8 | DATADIR = os.path.join(os.path.dirname(__file__), "../../test/data")
|
8 | 9 |
|
9 | 10 |
|
10 | 11 | class TestAlignment(unittest.TestCase):
|
11 |
| - def _run_decode(self, decoder, expect_fail=False): |
| 12 | + def _run_decode(self, decoder): |
12 | 13 | with open(os.path.join(DATADIR, "goforward.raw"), "rb") as fh:
|
13 | 14 | buf = fh.read()
|
14 | 15 | decoder.start_utt()
|
@@ -45,6 +46,54 @@ def test_default_lm(self):
|
45 | 46 | self._run_decode(decoder)
|
46 | 47 | self.assertEqual(decoder.hyp().hypstr, "go forward ten meters")
|
47 | 48 |
|
| 49 | + def _run_phone_align(self, decoder, buf): |
| 50 | + decoder.start_utt() |
| 51 | + decoder.process_raw(buf, no_search=False, full_utt=True) |
| 52 | + decoder.end_utt() |
| 53 | + decoder.set_alignment() |
| 54 | + decoder.start_utt() |
| 55 | + decoder.process_raw(buf, no_search=False, full_utt=True) |
| 56 | + decoder.end_utt() |
| 57 | + |
| 58 | + def test_align_forever(self): |
| 59 | + decoder = Decoder(loglevel="INFO", backtrace=True, lm=None) |
| 60 | + decoder.set_align_text("feels like these days go on forever") |
| 61 | + with wave.open( |
| 62 | + os.path.join(DATADIR, "forever", "input_2_16k.wav"), "r" |
| 63 | + ) as infh: |
| 64 | + data = infh.readframes(infh.getnframes()) |
| 65 | + self._run_phone_align(decoder, data) |
| 66 | + alignment = decoder.get_alignment() |
| 67 | + phones = [entry.name for entry in alignment.phones()] |
| 68 | + self.assertEqual( |
| 69 | + phones, |
| 70 | + [ |
| 71 | + "F", |
| 72 | + "IY", |
| 73 | + "L", |
| 74 | + "Z", |
| 75 | + "L", |
| 76 | + "AY", |
| 77 | + "K", |
| 78 | + "DH", |
| 79 | + "IY", |
| 80 | + "Z", |
| 81 | + "D", |
| 82 | + "EY", |
| 83 | + "Z", |
| 84 | + "G", |
| 85 | + "OW", |
| 86 | + "AO", |
| 87 | + "N", |
| 88 | + "F", |
| 89 | + "ER", |
| 90 | + "EH", |
| 91 | + "V", |
| 92 | + "ER", |
| 93 | + "SIL", |
| 94 | + ], |
| 95 | + ) |
| 96 | + |
48 | 97 |
|
49 | 98 | if __name__ == "__main__":
|
50 | 99 | unittest.main()
|
0 commit comments