0

I have implemented a recognizer intent like this.

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Tell me stuff");
    startActivityForResult(intent, REQUEST_CODE);

With a return like this

    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
    {

        ArrayList<String> matches = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);

    }

What I would like to do with this data is implement simple grammar rules with numbers. For example something like this

        if(matches.contains("my number is"))
        {

             string number = matches.getNextWord();

                 //Then parse the string into an integer    

        }

Obviously this code doesn't work but I'm wondering if anyone has a solution for this as a Google search yielded absolutely nothing. Thanks for any help

user1275331
  • 23
  • 1
  • 2
  • 5

1 Answers1

2

You don't need a grammar.

Check out how I do it in this code.

https://github.com/gast-lib/gast-lib/blob/master/app/src/root/gast/playground/speech/food/command/AskForCalories.java

The code within that library basically loops over all the words of all of the possible recognition results calling this method:

  private boolean isNumber(String word)
    {
        boolean isNumber = false;
        try
        {
            Integer.parseInt(word);
            isNumber = true;
        } catch (NumberFormatException e)
        {
            isNumber = false;
        }
        return isNumber;
    }

You may also want to have your code accept other words that sounds like numbers such as "too" "tree" "for" etc...

gregm
  • 12,019
  • 7
  • 56
  • 78