iSphinx 是基于 Pocketsphinx 引擎的离线语音识别 iOS 库。通过 Cocoapods 将语音识别功能添加到您的 iOS 应用程序中。iSphinx 为您的应用程序提供简单配置和实现,无需处理 Pocketsphinx 资产和配置。
我尝试以不同的词序说话
添加到 Podfile
pod 'iSphinx', '~> 1.2.0'
Info.plist
中添加录音请求权限首先,在您的类/ViewController 中实现 iSphinxDelegete
import iSphinx
class ViewController: UIViewController, iSphinxDelegete {
var isphinx: iSphinx = iSphinx()
override func viewDidLoad() {
super.viewDidLoad()
isphinx.delegete = self
}
}
iSphinxDelegete有一些在您的类/ViewController必须实现的方法
func iSphinxDidStop(reason: String, code: Int) {
if code == 500 { // 500 code for error
print(reason)
} else if code == 522 { // 522 code for timed out
print(reason)
} else if code == 200 { // 200 code for finish speech
print(reason)
}
}
func iSphinxFinalResult(result: String, hypArr: [String], scores: [Double]) {
print("Full Result : \(result)")
// NOTE :
// [x] parameter "result" : Give final response with ??? values when word out-of-vocabulary.
// [x] parameter "hypArr" : Give final response in original words without ??? values.
// Get score from every single word. hypArr length equal with scores length
for score in scores {
print(score)
}
// Get array word
for word in hypArr {
print(word)
}
}
func iSphinxPartialResult(partialResult: String) {
print(partialResult)
}
func iSphinxUnsupportedWords(words: [String]) {
var unsupportedWords = ""
for word in words {
unsupportedWords += word + ", "
}
print("Unsupported words : \(unsupportedWords)")
}
func iSphinxDidSpeechDetected() {
print("Speech detected!")
}
在使用之前,您需要准备语音识别。您可以在 onPreExecute 中添加新参数以增加准确度或性能。
isphinx.prepareISphinx(onPreExecute: { (config) in
// You can add new parameter pocketshinx here
self.isphinx.silentToDetect = 1.0
self.isphinx.isStopAtEndOfSpeech = true
// config.setString(key: "-parameter", value: "value")
}) { (isSuccess) in
if isSuccess {
print("Preparation success!")
}
}
您可以在不中断的情况下即时更新词汇表、语言模型或 JSGF 语法。确保在更新词汇/语法之前删除标点符号。iSphinx 中默认要移除的标点是 (.), (,), (?), (!), (_), (-), (\), (:)
// Update vocabulary with language model from single string
isphinx.updateVocabulary(text: "YOUR VOCABULARIES!", oovWords: ["WORDS DISTRUBER", ...]) {
print("Vocabulary updated!")
}
// Update vocabulary with language model from array string
isphinx.updateVocabulary(text: "YOUR VOCABULARIES!", oovWords: ["WORDS DISTRUBER", ...]) {
print("Vocabulary updated!")
}
// Update vocabulary with JSGF Grammar from string
isphinx.updateGrammar(text: "YOUR GRAMMAR", oogWords: ["WORDS DISTRUBER", ...]) {
print("Grammar updated!")
}
您可以有或没有超时地开始语音识别。
// Start speech recognition with Timeout in seconds
isphinx.startISphinx(timeoutInSec: 10)
// Start speech recognition without Timeout
isphinx.startISphinx()
您可以手动停止语音识别。
// Stop speech recognition manually.
isphinx.stopISphinx()
确保在语音识别完成后播放音频记录。
isphinx.getRecorder().play {
print("Play audio finish!")
}
Copyright (c) 2018 Saiful Irham Wicaksana
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.