数字格式的 iOS 语音到文本转换 [英] iOS speech to text conversion in number format

查看:25
本文介绍了数字格式的 iOS 语音到文本转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我使用默认的 iOS 语音到文本转换,而没有为其添加任何代码.当用户说五"时,显示为五"或5".但是,我需要始终将其转换为5".我可以使用 SFSpeechRecognizer 或任何其他方式来实现这一目标吗?

Currently I'm using default iOS speech to text conversion without adding any code for it. When the user says 'five', it is displayed as 'five' or '5'. But, I need it to be converted as '5' always. Is there anything I can do with SFSpeechRecognizer or any other way to achieve this?

推荐答案

这可以帮助您入门,但它无法处理包含数字和非数字的混合字符串.理想情况下,您需要在每个单词出现时对其进行处理,但这会对组合数字(例如 34)产生潜在影响.

This can get you started, but it is not able to handle mixed strings that contain a number AND a non-number. Ideally, you would need to process each word as it comes through, but then that has potential effects for combined numbers (thirty four) for example.

let fiveString = "five"
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .spellOut

print(numberFormatter.number(from: fiveString)?.stringValue) // 5

let combinedString = "five dogs"
print(numberFormatter.number(from: combinedString)?.stringValue) // nil

let cString = "five hundred"
print(numberFormatter.number(from: cString)?.stringValue) // 500

let dString = "five hundred and thirty-seven"
print(numberFormatter.number(from: dString)?.stringValue) // 537

这篇关于数字格式的 iOS 语音到文本转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆