Is there any way of printing numbers into proper spellings instead of throwing numbers while recording voice via SFSpeechRecognizer
? I've tried to get the word format by implementing the code below:
if let resultString = result?.bestTranscription.formattedString {
if let number = Double(resultString) {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .spellOut
let numberString = numberFormatter.string(from: NSNumber(value: number))
let numberStringWithoutHyphen = numberString?.replacingOccurrences(of: "-", with: " ")
print(numberStringWithoutHyphen)
}
}
This solution works great if the user is speaking whole numbers or even decimal numbers but there are some cases where this solution doesn't work at all and makes this solution look dumb. For example, if the user says "Fifty five point zero", the speech recognizer picks it up as "55.0". But the number formatter returns "Fifty five". In an extreme case, if the user says "One two three four", the speech recognizer picks it up as "1234" but the number formatter returns "One thousand two hundred thirty four".
What I am aiming for is if the user says any number, the speech recognizer should return the same, word by word. If the user says "Fifty five point zero", it should return "Fifty five point zero". If the user says "One two three four", it should return "One two three four".