Reputation: 2075
I'd like to use the ios SFSpeechRecognizer
to recognize a closed set of words and phrases. Is there a way to specify these and remove all other possibilities? I cannot seem to find a way to do this.
Upvotes: 2
Views: 566
Reputation: 2340
After some trials and error, the best solution is a mix between Mrsantateam and Giuseppe Garassino's answers.
you can first specify all commands that should be "prioritized" by setting contextualStrings
...
var contextualString = ["hello world", "goodbye world"]
recognitionRequest.contextualStrings = contextualString
... And later filter those results in recognitionTask
:
recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest) { result, error in
var isFinal = false
if let result = result {
for transcription in result.transcriptions {
print("Pondering \(result.bestTranscription.formattedString)")
if contextualString.contains(transcription.formattedString.lowercased()) {
print("Found \(transcription.formattedString)")
}
}
}
//...
}
Sample code can be found at https://developer.apple.com/documentation/speech/recognizing_speech_in_live_audio if you want to play with it and test its limits
Upvotes: 0
Reputation: 171
You can try to use contextualStrings property of SFSpeechAudioBufferRecognitionRequest to set high priority or to set some words that are not in vocabulary. For more info look here: https://developer.apple.com/documentation/speech/sfspeechrecognitionrequest/1649391-contextualstrings?language=objc
SFSpeechAudioBufferRecognitionRequest *recognitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init];
recognitionRequest.contextualStrings = @[@"Your specific word", @"short custom phrases that are unique to your app"];
Upvotes: 3
Reputation: 2292
I think you cannot limit SFSpeechRecognizer
's vocabulary, but you can fire action only if the words you want are recognized.
When you get your output, you can try something like this:
self.recognitionTask = speechRecognizer?.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in
if result == "Desired word one" {
// Do something
} else if result == "Desired word two" {
// Do something else
} else {
// Don't do anything
}
})
Upvotes: 1