Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get last Spoken word from SFSpeechRecognitionResult

I am implementing a speech recognition process to convert using SFSpeechRecognizer. Need to implement erase option to remove the last character. But SFSpeechRecognitionResult, result.bestTranscription.formattedString always returns a whole string from the beginning to end. Is there any way to get the last spoken word from SFSpeechRecognitionResult without stop & start recognition?

My implementation code

- (void)startListening{
// Initialize the AVAudioEngine
audioEngine = [[AVAudioEngine alloc] init];
_speechSynthesizer  = [[AVSpeechSynthesizer alloc] init];

// Make sure there's not a recognition task already running
if (recognitionTask)
{
    [_SFSpeechAudioBufferRecRequest endAudio];
    [audioEngine stop];
    // [recognitionTask cancel];
    // recognitionTask = nil;
}
// Starts an AVAudio Session
NSError *error;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:&error];
[audioSession setMode:AVAudioSessionModeMeasurement error:&error];
[audioSession setActive:true withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation  error:&error];

// Starts a recognition process, in the block it logs the input or stops the audio
// process if there's an error.
_SFSpeechAudioBufferRecRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init];
AVAudioInputNode *inputNode = audioEngine.inputNode;
_SFSpeechAudioBufferRecRequest.shouldReportPartialResults = YES;
recognitionTask = [speechRecognizer recognitionTaskWithRequest:_SFSpeechAudioBufferRecRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error)
                   {
    if (result)
    {
        // Whatever you say in the microphone after pressing the button should be being logged
        // in the console.
        NSLog(@"RESULT:%@",result.bestTranscription.formattedString);
    }
    if (error)
    {
        NSLog(@"ERROR %@", error);
        @try
        {
            [audioEngine stop];
            [inputNode removeTapOnBus:0];
            _SFSpeechAudioBufferRecRequest = nil;
            recognitionTask = nil;
        }
        @catch (NSException *exception)
        {
            NSLog(@"EXCEPTION  ======== %@",exception);
        }
        @finally
        {
        }
    }
}];

// Sets the recording format
AVAudioFormat *recordingFormat = [inputNode outputFormatForBus:0];
[inputNode installTapOnBus:0 bufferSize:2048 format:recordingFormat block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
    [_SFSpeechAudioBufferRecRequest appendAudioPCMBuffer:buffer];
}];
// Starts the audio engine, i.e. it starts listening.
[audioEngine prepare];
[audioEngine startAndReturnError:&error];}

Thanks in advance!

like image 858
Ranjani Avatar asked Dec 07 '21 13:12

Ranjani


1 Answers

You can work on the output string and get the last word. Code will look something like below:

-(NSString *)getLastWord:(NSString *)outputString {
    NSRange range = [outputString rangeOfString: @" " options:NSBackwardsSearch];
    NSString *lastWord = [outputString substringFromIndex:range.location +1];
    return lastWord;
}

You can pass your result.bestTranscription.formattedString to above method and get the desired result.

NOTE: Just make sure you will call this method only when the length of result.bestTranscription.formattedString is greater than 0 and not NIL.

like image 129
Pushkraj P. Lanjekar Avatar answered Nov 04 '22 00:11

Pushkraj P. Lanjekar