Skip to content

Commit 05cb832

Browse files
authoredJul 20, 2023
Merge pull request #354 from cmusphinx/339-final-state
Do not attempt to align phones to impossibly short durations (fixes #339)
2 parents f899dfe + 6252f26 commit 05cb832

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed
 

‎cython/test/alignment_test.py

+50-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import os
44
from pocketsphinx import Decoder
55
import unittest
6+
import wave
67

78
DATADIR = os.path.join(os.path.dirname(__file__), "../../test/data")
89

910

1011
class TestAlignment(unittest.TestCase):
11-
def _run_decode(self, decoder, expect_fail=False):
12+
def _run_decode(self, decoder):
1213
with open(os.path.join(DATADIR, "goforward.raw"), "rb") as fh:
1314
buf = fh.read()
1415
decoder.start_utt()
@@ -45,6 +46,54 @@ def test_default_lm(self):
4546
self._run_decode(decoder)
4647
self.assertEqual(decoder.hyp().hypstr, "go forward ten meters")
4748

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+
4897

4998
if __name__ == "__main__":
5099
unittest.main()

‎src/state_align_search.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -457,13 +457,21 @@ state_align_search_init(const char *name,
457457
i < sas->n_phones && itor;
458458
++i, itor = ps_alignment_iter_next(itor)) {
459459
ps_alignment_entry_t *ent = ps_alignment_iter_get(itor);
460+
int min_nframes;
461+
460462
hmm_init(sas->hmmctx, &sas->hmms[i], FALSE,
461463
ent->id.pid.ssid, ent->id.pid.tmatid);
462-
if (ent-> start > 0)
464+
/* Can't align less than the number of frames in an HMM! */
465+
min_nframes = hmm_n_emit_state(&sas->hmms[i]);
466+
if (ent->duration < min_nframes)
467+
E_WARN("phone %d has impossible duration %d "
468+
"(consider disabling bestpath search)\n",
469+
i, ent->duration);
470+
if (ent->start > 0 && ent->duration >= min_nframes)
463471
sas->sf[i] = ent->start;
464472
else
465473
sas->sf[i] = 0; /* Always active */
466-
if (ent->duration > 0)
474+
if (ent->duration >= min_nframes)
467475
sas->ef[i] = ent->start + ent->duration;
468476
else
469477
sas->ef[i] = INT_MAX; /* Always active */

0 commit comments

Comments
 (0)
Please sign in to comment.