@@ -361,6 +361,9 @@ def activate_search_if_necessary():
361
361
wrapper .set_search = False
362
362
363
363
def _unset_search (self , name ):
364
+ if not self ._decoder :
365
+ return
366
+
364
367
# Unset a Pocket Sphinx search with the given name.
365
368
# Do NOT unset the default search; this will cause a segfault!
366
369
if name == self ._default_search_name :
@@ -375,6 +378,9 @@ def _unset_search(self, name):
375
378
self ._set_default_search ()
376
379
377
380
def _set_default_search (self ):
381
+ if not self ._decoder :
382
+ return
383
+
378
384
# Ensure we're not processing.
379
385
self ._decoder .end_utterance ()
380
386
@@ -385,10 +391,13 @@ def check_valid_word(self, word):
385
391
"""
386
392
Check if a word is in the current Sphinx pronunciation dictionary.
387
393
394
+ This method must be called after :meth:`connect`.
395
+
388
396
:rtype: bool
389
397
"""
390
398
if not self ._decoder :
391
- self .connect ()
399
+ raise EngineError ("Calling check_valid_word is not allowed"
400
+ " before calling connect" )
392
401
393
402
word = _map_to_str (word )
394
403
return bool (self ._decoder .lookup_word (word .lower ()))
@@ -426,7 +435,11 @@ def _do_recognition(self):
426
435
self ._recorder .stop ()
427
436
428
437
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
+
430
443
# The *words* argument should be a string or iterable.
431
444
# Words are put into lowercase for consistency.
432
445
if isinstance (words , string_types ):
@@ -461,13 +474,14 @@ def process_buffer(self, buf):
461
474
This method is meant to be called sequentially with buffers from an
462
475
audio source, such as a microphone or wave file.
463
476
464
- This method will do nothing if :meth:`connect` has not been called .
477
+ This method must be called after :meth:`connect`.
465
478
466
479
:param buf: audio buffer
467
480
:type buf: str
468
481
"""
469
482
if not self ._decoder :
470
- return
483
+ raise EngineError ("Calling process_buffer is not allowed"
484
+ " before calling connect" )
471
485
472
486
# Keep a list of buffers for possible reprocessing later on.
473
487
self ._audio_buffers .append (buf )
@@ -553,9 +567,9 @@ def _process_hypotheses(self, hyp):
553
567
lm_hypothesis = hyp
554
568
555
569
# 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.
559
573
hypotheses = {}
560
574
for wrapper in wrappers :
561
575
if not self ._mimicking :
0 commit comments