Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple commands using the same callback #70

Closed
Nitzahon opened this issue Nov 17, 2020 · 18 comments
Closed

Multiple commands using the same callback #70

Nitzahon opened this issue Nov 17, 2020 · 18 comments

Comments

@Nitzahon
Copy link

What is the best way to do this, do I have to define multiple commands with the same callback, or can a command be an array of possible strings which will call the same callback?

@Nitzahon
Copy link
Author

so it seems you can put an array of commands to use the same callback

@JamesBrill
Copy link
Owner

Array commands are not actually supported (though they probably should be). See this comment on your other issue.

@JamesBrill
Copy link
Owner

Reopening to remind myself to add support for array commands.

@JamesBrill
Copy link
Owner

@Nitzahon Array-based commands are now supported in v3.4.0

@Nitzahon
Copy link
Author

You sir, are what is known as a mensch

@Nitzahon
Copy link
Author

Nitzahon commented Dec 15, 2020

One question though, how do you send the spoken command itself from the array as the callbacks argument? like this:

    {
      command: ['Hello', 'Hi'],
      callback: () => setMessage(command),
      matchInterim: true
    }

or like this:

    {
      command: ['Hello', 'Hi'],
      callback: () => setMessage(command, spokenPhrase),
      matchInterim: true
    }

or something else? will I still need to use the same solution you gave me in 3.3.0?


  const videoCommandCallback = (command, spokenPhrase) => {
    sendAns(command)
    handleReset()

  }
  const videoCommands = ['Hello', 'Hi'].map(phrase => ({
    command: phrase,
    callback: videoCommandCallback,
    isFuzzyMatch: true,
    fuzzyMatchingThreshold: 0.6
  }))

@Nitzahon
Copy link
Author

so this works:

  const videoCommandCallback = (command, spokenPhrase) => {
    sendAns(command)
    handleReset()

  }
const commands =[
    {
      command: ['Hello', 'Hi'],
      callback: videoCommandCallback,
      isFuzzyMatch: true,
      fuzzyMatchingThreshold: 0.6,
    }
]

There is only the issue, of, if multiple commands match the fuzzy requirements, all are called with the callback instead of the command which most closely matches.
i.e.: If my array contains the commands ["Everything is working", "Nothing is working"], my fuzzyMatchingThreshold is set to 0.7 or lower and I say "Everything is working" The callback will be called twice, once for "Everything is working", and once for "Nothing is working", even though "Everything is working" more closely matches.

@JamesBrill
Copy link
Owner

Ah yes, it would make sense to know which of the commands in the array was matched. I'll make a quick update.

@JamesBrill
Copy link
Owner

@Nitzahon Give v3.5.0 a try. The callback can get the matched command from the last argument, which will be an object with a new property: command.

@JamesBrill
Copy link
Owner

There is only the issue, of, if multiple commands match the fuzzy requirements, all are called with the callback instead of the command which most closely matches.
i.e.: If my array contains the commands ["Everything is working", "Nothing is working"], my fuzzyMatchingThreshold is set to 0.7 or lower and I say "Everything is working" The callback will be called twice, once for "Everything is working", and once for "Nothing is working", even though "Everything is working" more closely matches.

This is an interesting point. It may be desirable in some cases to just trigger the callback on the best match when fuzzy matching is used. If this would be a useful feature for you, let me know and I can look into it.

@Nitzahon
Copy link
Author

There is only the issue, of, if multiple commands match the fuzzy requirements, all are called with the callback instead of the command which most closely matches.
i.e.: If my array contains the commands ["Everything is working", "Nothing is working"], my fuzzyMatchingThreshold is set to 0.7 or lower and I say "Everything is working" The callback will be called twice, once for "Everything is working", and once for "Nothing is working", even though "Everything is working" more closely matches.

This is an interesting point. It may be desirable in some cases to just trigger the callback on the best match when fuzzy matching is used. If this would be a useful feature for you, let me know and I can look into it.

Letting you know, this would be useful ;-)

@Nitzahon
Copy link
Author

@Nitzahon Give v3.5.0 a try. The callback can get the matched command from the last argument, which will be an object with a new property: command.

tried this. Did not work:

    {
      command: videoCommandPhrases.commands,
      callback: ({ command }) => console.log("command is: "+command),
      isFuzzyMatch: true,
      fuzzyMatchingThreshold: 0.6,
    }

expected outout: "command is: Everything is Working"
actual output: "command is: undefined"

@JamesBrill
Copy link
Owner

I'll repro this and see what I can do.

@JamesBrill
Copy link
Owner

Initial tests indicate this issue is exclusive to fuzzy-matched commands. My bad for not testing that case when I made that release. Investigating.

@JamesBrill
Copy link
Owner

Ah wait, this does make sense. Fuzzy callbacks get three other arguments before that one.

@JamesBrill
Copy link
Owner

With fuzzy commands, you can use the first arg like before:

    {
      command: ["Everything is working", "Nothing is working"],
      callback: (command) => console.log("command is: "+command),
      isFuzzyMatch: true,
      fuzzyMatchingThreshold: 0.6,
    },

@JamesBrill
Copy link
Owner

JamesBrill commented Dec 16, 2020

This does make me want to clean up the API a bit. Ideally, there would just be one object arg for both fuzzy and non-fuzzy commands, and the callback could unpack whatever it needed. It feels messy having command in two places for fuzzy commands.

Before I do that, I'm going to look into your request for "closest match wins" for fuzzy commands.

@JamesBrill
Copy link
Owner

@Nitzahon Give v3.6.0 a try - it adds an option to only trigger the callback once, on the best fuzzy match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants