Search code examples
pythonspeech-recognitionspeechpython-dragonfly

python dragonfly to recognize similar words


I am doing a program with dragon fly using wsr,where it has to analyse a word,any voice matching that word should output 'yes it matches'

If i say 'czechoslovakia' then it must print true even for all the similar matches of this world ,like words for 'circle slovakia, cat on slavia,seko vakia...'

What specific methods,should i use for this?

My program

from dragonfly.all import *
import pythoncom
import time
# Voice command rule combining spoken form and recognition processing.
class ExampleRule(CompoundRule):
    spec = "czechoslovakia|circle slovalia|sceko bakia|cat on ania"                 # Spoken form of command.

    def _process_recognition(self, node, extras):   # Callback when command is spoken.
         print "Voice command spoken."

# Create a grammar which contains and loads the command rule.
grammar = Grammar("example grammar")                # Create a grammar to contain the command    rule.
grammar.add_rule(ExampleRule())                     # Add the command rule to the grammar.
grammar.load()                                      # Load the grammar.

while True:
    pythoncom.PumpWaitingMessages()
    time.sleep(.1)

Solution

  • There is nothing built into Dragonfly to allow you to do this, but you have some other options.

    1. If you're looking to dynamically generate the spec, you might want to look at Fuzzy. You could give it a word and use it to generate other similar sounding words from that word. Then you could create the spec from them.
    2. Here is the WSR engine class in Dragonfly. I don't know much about SAPI5, but you might be able to ask it for alternatives. If you can, you might be able to extend the Dragonfly GrammarWrapper to expose the alternatives, and then use a catchall grammar to save all utterances and then filter out what you want (possibly using Fuzzy).
    3. If you were using Natlink, I would recommend looking at the results object. As you can see here, the results object has access to all of Dragon's different hypotheses for what you said in a given utterance. Just as with my second suggestion, you could catch everything and then filter what you wanted:

    .

    from natlinkutils import GrammarBase
    
    class CatchAll(GrammarBase):
    
        # this spec will catch everything
        gramSpec = """
            <start> exported = {emptyList};
        """
    
        def initialize(self):
            self.load(self.gramSpec, allResults=1)
            self.activateAll()
    
        def gotResultsObject(self, recogType, resObj):
            for x in range(0, 100):
                try:
                    possible_interpretation = resObj.getWords(x)
                    # do whatever sort of filtering you want here
                except Exception:
                    break
    
    c = CatchAll()
    c.initialize()
    
    def unload():
        global c
        if c:
            c.unload()
        c = None