csdcorp / speech_to_text

A Flutter plugin that exposes device specific text to speech recognition capability.
BSD 3-Clause "New" or "Revised" License
351 stars 218 forks source link

Cannot retrieve more than 1 alternates in result (iOS/Android) #139

Closed Ccastillo06 closed 3 years ago

Ccastillo06 commented 3 years ago

Hi, I'm trying to listen to a word and validate if the user said the correct input. I tried this package in the past and I was able to get more than one alternate with different confidence levels.

Since I tried it again, I'm not being able to receive more than one alternate, being it the same word as the result. I tested in Android and iOS and it happens in both devices.

This is my listener:

void startListening() {
    lastWords = "";
    lastError = "";
    speech.listen(
        onResult: resultListener,
        listenFor: Duration(seconds: 10),
        pauseFor: Duration(seconds: 2),
        localeId: _currentLocaleId,
        onSoundLevelChange: soundLevelListener,
        cancelOnError: true,
        partialResults: false,
        onDevice: false,
        listenMode: ListenMode.confirmation);
    setState(() {});
}

And this is the resultListener:

void resultListener(SpeechRecognitionResult result) {
    print('The result is:');
    print(result);
​
    setState(() {
      lastWords = "${result.recognizedWords} - ${result.finalResult}";
    });
}

The result, for any test I try, even when changing the locale to English and trying to say MAZE looks like this:

I/flutter (30511): SpeechRecognitionResult words: [SpeechRecognitionWords words: mace,  confidence: 0.64350826], final: true

As you can see, I'm not getting MAZE and MACE (or even MAIZE when in UK), so I can't validate if the user had the correct answer. I tried this a few months ago and I was able to get multiple alternates, but can't anymore.

Does this have something to do with how the device Speech Recognition API works? Or was it a change in the package? I reviewed the whole changelog but can't find anything about it.

Thanks so much in advance

Ossyyrr commented 3 years ago

Please add in the SpeechToTextPlugin.kt file on line 468:

putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,10)

sowens-csd commented 3 years ago

That's odd, I don't believe I've made package changes in that area but I'll check. Thanks for the suggestion @Ossyyrr have you've tried that and it worked?

Ossyyrr commented 3 years ago

it works.

Ccastillo06 commented 3 years ago

Can confirm that @Ossyyrr suggestion works as expected 👍 !

sowens-csd commented 3 years ago

Thanks! I'll make that change. Sounds like there is still an iOS issue though?

Ccastillo06 commented 3 years ago

I'll test again and add some feedback from the logs, hope that helps!

sowens-csd commented 3 years ago

I just tested iOS and got what looked like reasonable results. I'm going to release the Android fix as 2.6.0 with a couple of other small changes. Here's the results I got from this result listener code:

    result.alternates.forEach((alternate) {
      print(alternate);
    });
flutter: SpeechRecognitionWords words: We'll go to the well without further ado,  confidence: 0.824
flutter: SpeechRecognitionWords words: We'll go to the well without further ado,  confidence: 0.824
flutter: SpeechRecognitionWords words: Will go to the well without further ado,  confidence: 0.764
flutter: SpeechRecognitionWords words: We will go to the well without further ado,  confidence: 0.761
Ossyyrr commented 3 years ago

That's great!

Thank you.

Ccastillo06 commented 3 years ago

@sowens-csd Here are some print results in Android (tested in emulator and in my device):

Flutter STT

Using the package in Android and trying to get MACE as a word:

I/flutter ( 4740): The result
I/flutter ( 4740): SpeechRecognitionResult words: [SpeechRecognitionWords words: maze,  confidence: 0.7582241], final: true

I/flutter ( 4740): The result
I/flutter ( 4740): SpeechRecognitionResult words: [SpeechRecognitionWords words: maze,  confidence: 0.76188755], final: true

I/flutter ( 4740): The result
I/flutter ( 4740): SpeechRecognitionResult words: [SpeechRecognitionWords words: maze,  confidence: 0.7798181], final: true

After editing the SpeechToTextPlugin.kt file:

I/flutter ( 5169): The result
I/flutter ( 5169): SpeechRecognitionResult words: [SpeechRecognitionWords words: maze,  confidence: 0.7115129, SpeechRecognitionWords words: Mase,  confidence: 0.8581462, SpeechRecognitionWords words: maize,  confidence: 0.8587986, SpeechRecognitionWords words: mace,  confidence: 0.9143559, SpeechRecognitionWords words: Mays,  confidence: 0.91716546, SpeechRecognitionWords words: mais,  confidence: 0.92167616, SpeechRecognitionWords words: Mae's,  confidence: 0.9222131, SpeechRecognitionWords words: maiz,  confidence: 0.9222131, SpeechRecognitionWords words: May's,  confidence: 0.9222131, SpeechRecognitionWords words: Mayes,  confidence: 0.9222131], final: true

There are now many results available 👏

sowens-csd commented 3 years ago

This fix is now live as 2.6.0 on pub.dev.

Thanks @Ossyyrr for your work on the fix.

Ccastillo06 commented 3 years ago

@sowens-csd It's working in iOS, I was having an unrelated error and now I can see many results too:

[  +43 ms] flutter: The result
[   +1 ms] flutter: SpeechRecognitionResult words: [SpeechRecognitionWords
words: Maze,  confidence: 0.106, SpeechRecognitionWords words: Main,
confidence: 0.057, SpeechRecognitionWords words: Mayonnaise,  confidence: 0.006,
SpeechRecognitionWords words: Maybe,  confidence: 0.006, SpeechRecognitionWords
words: May,  confidence: 0.006], final: true

Thanks so much for updating this soon 😄


On another point, I saw this issue (which is closed) #132 and I was having the same problem, which was happening because I had the listen config like this:

    speech.listen(
       // All config goes here...
        onDevice: true);

As you can see, I was using a simulator but having the option onDevice: true was throwing the same error, I changed it to onDevice: false and it's working fine in the simulator too.

I'm commenting this here as that PR i closed and I can't comment to help those users, so maybe you can tell them that maybe the problem they were having was related to this.

Thanks again!

sowens-csd commented 3 years ago

Thanks for pointing that out, I'll put it in the tips section. I'll close this issue, thanks for the feedback!

Ossyyrr commented 3 years ago

Could you add this functionality also on ios? on many occasions I only get one alternative, it would be nice to have at least an array of 10. Regards.