Skip to content

Commit

Permalink
Make sphinx engine call notify_recognition() before processing
Browse files Browse the repository at this point in the history
This avoids confusing log output where sphinx appears to recognise
speech *after* processing it. This is especially confusing if mimic
is used during rule processing.
  • Loading branch information
drmfinlay committed Apr 2, 2019
1 parent 46f3ad2 commit d332717
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
18 changes: 8 additions & 10 deletions dragonfly/engines/backend_sphinx/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def _validate_words(self, words, search_type):
"%s" % (search_type, ", ".join(unknown_words)))

def _build_grammar_wrapper(self, grammar):
return GrammarWrapper(grammar, self)
return GrammarWrapper(grammar, self, self._recognition_observer_manager)

def _set_grammar(self, wrapper, activate, partial=False):
if not wrapper:
Expand Down Expand Up @@ -772,6 +772,12 @@ def _process_key_phrases(self, speech, mimicking):
else:
speech = speech.rstrip() # remove trailing whitespace.

# Notify observers if a keyphrase was matched.
result = speech if speech in self._keyphrase_functions else ""
if result:
words = tuple(result.split())
self._recognition_observer_manager.notify_recognition(words)

# Call the registered function if there was a match and the function
# is callable.
func = self._keyphrase_functions.get(speech, None)
Expand All @@ -784,8 +790,6 @@ def _process_key_phrases(self, speech, mimicking):
"keyphrase '%s': %s" % (speech, e)
)

# Return speech if it matched a keyphrase.
result = speech if speech in self._keyphrase_functions else ""
return result

@classmethod
Expand Down Expand Up @@ -818,9 +822,7 @@ def _process_hypotheses(self, speech, mimicking):
# Check key phrases search first.
keyphrase = self._process_key_phrases(speech, mimicking)
if keyphrase:
# Keyphrase search matched. Notify observers and return True.
words = tuple(keyphrase.split())
self._recognition_observer_manager.notify_recognition(words)
# Keyphrase search matched.
return True, keyphrase

# Otherwise do grammar processing.
Expand Down Expand Up @@ -869,10 +871,6 @@ def _process_hypotheses(self, speech, mimicking):

processing_occurred = wrapper.process_words(words_rules)
if processing_occurred:
# Notify observers of the recognition.
self._recognition_observer_manager.notify_recognition(
tuple([word for word, _ in words_rules])
)
break

# Return whether processing occurred and the final speech hypothesis for
Expand Down
10 changes: 9 additions & 1 deletion dragonfly/engines/backend_sphinx/grammar_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ class GrammarWrapper(object):

_log = logging.getLogger("engine")

def __init__(self, grammar, engine):
def __init__(self, grammar, engine, observer_manager):
"""
:type grammar: Grammar
:type engine: SphinxEngine
"""
self.grammar = grammar
self.engine = engine
self._observer_manager = observer_manager

# Compile the grammar into a JSGF grammar and set the language.
self._jsgf_grammar = engine.compiler.compile_grammar(grammar)
Expand Down Expand Up @@ -124,9 +125,16 @@ def process_words(self, words):
s.initialize_decoding()
for _ in r.decode(s):
if s.finished():
# Notify observers using the manager *before* processing.
self._observer_manager.notify_recognition(
tuple([word for word, _ in words])
)

# Process the rule if not in training mode.
root = s.build_parse_tree()
if not self.engine.training_session_active:
r.process_recognition(root)

return True

self._log.debug("Grammar %s: failed to decode recognition %r."
Expand Down

0 comments on commit d332717

Please sign in to comment.