简体   繁体   English

如何从 SFSpeechRecognitionResult 获取最后一个口语单词

[英]How get last Spoken word from SFSpeechRecognitionResult

I am implementing a speech recognition process to convert using SFSpeechRecognizer.我正在实现一个语音识别过程以使用 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.但是 SFSpeechRecognitionResult、 result.bestTranscription.formattedString总是从头到尾返回一个完整的字符串。 Is there any way to get the last spoken word from SFSpeechRecognitionResult?有什么方法可以从 SFSpeechRecognitionResult 中获取最后一个口语?

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!提前致谢!

You can work on the output string and get the last word.您可以处理 output 字符串并获得最后一个字。 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.您可以将result.bestTranscription.formattedString传递给上述方法并获得所需的结果。

NOTE: Just make sure you will call this method only when the length of result.bestTranscription.formattedString is greater than 0 and not NIL.注意:请确保仅当result.bestTranscription.formattedString的长度大于 0 而不是 NIL 时才调用此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM