我刚刚实现了 SFSpeechRecognizer,因为我希望它指定一些数字,但我遇到的问题是,如果我说“一”,则 result.bestTranscription.formattedString 是“一”,但如果我说“十”,结果抛出“10”,我怎样才能设法得到由实际数字而不是交响乐“一”表示的个位数数字。
You can use NumberFormatter setting numberStyle
to .spellOut
and use its number(from: String)
to convert your string to number. If you need a string from that number just initialise a new string from it. Make sure if you only want to detect English words (not locale sensitive) to set the formatter locale to "en_US_POSIX"
let string = "one"
let numberFormatter = NumberFormatter()
numberFormatter.locale = Locale(identifier: "en_US_POSIX") // if you dont set locale it will use current locale so it wont detect one if the language it is not english at the devices locale
numberFormatter.numberStyle = .spellOut
numberFormatter.number(from: string) // 1
Testing another language/locale
let string = "um"
let numberFormatter = NumberFormatter()
numberFormatter.locale = Locale(identifier: "pt_BR")
numberFormatter.numberStyle = .spellOut
numberFormatter.number(from: string) // 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.