Skip to content

Commit 5dcd851

Browse files
committed
Fix some minor issues with the Sphinx engine code
1 parent 7119d67 commit 5dcd851

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

dragonfly/engines/backend_sphinx/compiler.py

-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ def _compile_impossible(self, element, *args, **kwargs):
303303

304304
return jsgf.NamedRuleRef("_impossible")
305305

306-
# TODO Change this to allow dictation elements to work.
307306
def _compile_dictation(self, element, *args, **kwargs):
308307
return self.compile_element(
309308
elements_.Impossible(), *args, **kwargs

dragonfly/engines/backend_sphinx/engine.py

+21-7
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ def activate_search_if_necessary():
361361
wrapper.set_search = False
362362

363363
def _unset_search(self, name):
364+
if not self._decoder:
365+
return
366+
364367
# Unset a Pocket Sphinx search with the given name.
365368
# Do NOT unset the default search; this will cause a segfault!
366369
if name == self._default_search_name:
@@ -375,6 +378,9 @@ def _unset_search(self, name):
375378
self._set_default_search()
376379

377380
def _set_default_search(self):
381+
if not self._decoder:
382+
return
383+
378384
# Ensure we're not processing.
379385
self._decoder.end_utterance()
380386

@@ -385,10 +391,13 @@ def check_valid_word(self, word):
385391
"""
386392
Check if a word is in the current Sphinx pronunciation dictionary.
387393
394+
This method must be called after :meth:`connect`.
395+
388396
:rtype: bool
389397
"""
390398
if not self._decoder:
391-
self.connect()
399+
raise EngineError("Calling check_valid_word is not allowed"
400+
" before calling connect")
392401

393402
word = _map_to_str(word)
394403
return bool(self._decoder.lookup_word(word.lower()))
@@ -426,7 +435,11 @@ def _do_recognition(self):
426435
self._recorder.stop()
427436

428437
def mimic(self, words):
429-
""" Mimic a recognition of the given *words* """
438+
""" Mimic a recognition of the given *words*. """
439+
if not self._decoder:
440+
raise EngineError("Calling mimic is not allowed before calling"
441+
" connect")
442+
430443
# The *words* argument should be a string or iterable.
431444
# Words are put into lowercase for consistency.
432445
if isinstance(words, string_types):
@@ -461,13 +474,14 @@ def process_buffer(self, buf):
461474
This method is meant to be called sequentially with buffers from an
462475
audio source, such as a microphone or wave file.
463476
464-
This method will do nothing if :meth:`connect` has not been called.
477+
This method must be called after :meth:`connect`.
465478
466479
:param buf: audio buffer
467480
:type buf: str
468481
"""
469482
if not self._decoder:
470-
return
483+
raise EngineError("Calling process_buffer is not allowed"
484+
" before calling connect")
471485

472486
# Keep a list of buffers for possible reprocessing later on.
473487
self._audio_buffers.append(buf)
@@ -553,9 +567,9 @@ def _process_hypotheses(self, hyp):
553567
lm_hypothesis = hyp
554568

555569
# Get the hypothesis for each active grammar.
556-
# If this is a regular recognition, switch to each gramar search and
557-
# re-process the audio to obtain a closer match. Otherwise, use
558-
# the mimicked words for each grammar's hypothesis.
570+
# If this is a regular recognition, switch to each grammar search
571+
# and re-process the audio to obtain a closer match. Otherwise,
572+
# use the mimicked words for each grammar's hypothesis.
559573
hypotheses = {}
560574
for wrapper in wrappers:
561575
if not self._mimicking:

0 commit comments

Comments
 (0)