JamesBrill / react-speech-recognition

💬Speech recognition for your React app
https://webspeechrecognition.com/
MIT License
645 stars 116 forks source link

bestMatchOnly does not work correctly #99

Closed DmytroPolovyi closed 3 years ago

DmytroPolovyi commented 3 years ago

If you set the fuzzyMatchingThreshold: 0.7, bestMatchOnly: true, isFuzzyMatch: true parameters, if two phrases match, the callback can be executed more than once.

urbanL1fe commented 3 years ago

@JamesBrill I could look into it if you want

JamesBrill commented 3 years ago

@urbanL1fe Yes, by all means, thank you.

@DmitryRback Note that bestMatchOnly only applies to the phrases within a single command, not to all commands globally. Let's say you have this callback:

  const callback = (command) => {
    console.log(`Best matching command: ${command}`)
  }

Let's also say that you have two similar phrases that you want to listen for, "eat" and "beat", and that you want to call the above callback in response to either of them.

If you have one command with an array of phrases, bestMatchOnly should ensure that the callback is only ever called once on each match:

  const commands = [
    {
      command: ['eat', 'beat'],
      callback,
      isFuzzyMatch: true,
      fuzzyMatchingThreshold: 0.7,
      bestMatchOnly: true
    }
  ]

However, if you have multiple fuzzy commands using the same callback, the callback can be called multiple times, once for each command it matches:

  const commands = [
    {
      command: 'eat',
      callback,
      isFuzzyMatch: true,
      fuzzyMatchingThreshold: 0.7,
      bestMatchOnly: true
    },
    {
      command: 'beat',
      callback,
      isFuzzyMatch: true,
      fuzzyMatchingThreshold: 0.7,
      bestMatchOnly: true
    }
  ]

If your commands look like the second example, that may explain while your callback is being called multiple times. To solve this, you can combine commands into one command with an array of phrases.

Also note that in the second example, bestMatchOnly doesn't do anything as there is only one phrase that could possibly be matched for each command.

If that still doesn't work, I may be able to help more if I can see the full list of commands that you're using.

DmytroPolovyi commented 3 years ago

Thank you solved the problem)