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

问题描述 投票:0回答:1

我正在实现一个语音识别过程,以使用 SFSpeechRecognizer 进行转换。需要实现擦除选项来删除最后一个字符。但是 SFSpeechRecognitionResult, result.bestTranscription.formattedString 始终返回从头到尾的整个字符串。有没有办法从 SFSpeechRecognitionResult 获取最后一个说出的单词而不需要停止和开始识别?

我的实现代码

- (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];}
ios swift objective-c speech-recognition speech-to-text
1个回答
2
投票

您可以处理输出字符串并获取最后一个单词。代码如下所示:

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

您可以将您的

result.bestTranscription.formattedString
传递给上述方法并获得所需的结果。

注意:只需确保仅当

result.bestTranscription.formattedString
的长度大于 0 而不是 NIL 时才调用此方法。

© www.soinside.com 2019 - 2024. All rights reserved.