ios - 在 Swift 中使用 CocoaPods 时如何实现 Nuance Speechkit

标签 ios swift cocoapods speech-to-text speechkit

在 pod 规范和当前 S.O. 上的内容之间我很难弄清楚如何使用 SpeechKit + CocoaPod + Swift 进行语音到文本的工作。终于让它工作了,所以我想我会帮助下一个寻求帮助的可怜人! :)

最佳答案

  1. 首先安装 CocoaPod:https://cocoapods.org/pods/SpeechKit
  2. 添加#import <SpeechKit/SpeechKit.h>到你的桥接头
  3. 登录到 Nuance 的开发门户并创建一个应用程序:https://developer.nuance.com/
  4. 清理演示代码,使其更有条理。我只想将尽可能多的代码放在一个地方,这样您就可以看到一个完整的工作实现。

然后创建一个 UIViewController 并使用正确的凭据添加以下代码:

import UIKit
import SpeechKit

class SpeechKitDemo: UIViewController, SKTransactionDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

//!!link this to a corresponding button on the UIViewController in I.B.
@IBAction func tappedButton(sender: AnyObject) {

    // All fields are required.
    // Your credentials can be found in your Nuance Developers portal, under "Manage My Apps".
    let SKSAppKey = "[Get this from the nuance app info page]";
    let SKSAppId = "[Get this from the nuance app info page]";
    let SKSServerHost = "[Get this from the nuance app info page]";
    let SKSServerPort = "[Get this from the nuance app info page]";

    let SKSLanguage = "eng-USA";

    let SKSServerUrl = "nmsps://\(SKSAppId)@\(SKSServerHost):\(SKSServerPort)"

    let session = SKSession(URL: NSURL(string: SKSServerUrl), appToken: SKSAppKey)


    //this starts a transaction that listens for voice input
    let transaction = session.recognizeWithType(SKTransactionSpeechTypeDictation,
        detection: .Short,
        language: SKSLanguage,
        delegate: self)
    print(transaction)
}

//required delegate methods
func transactionDidBeginRecording(transaction: SKTransaction!) {  }
func transactionDidFinishRecording(transaction: SKTransaction!) {  }
func transaction(transaction: SKTransaction!, didReceiveRecognition recognition: SKRecognition!) {

    //Take the best result
    let topRecognitionText = recognition.text;

    print("Best rec test: \(topRecognitionText)")
    //Or iterate through the NBest list
    let nBest = recognition.details;
    for phrase in (nBest as! [SKRecognizedPhrase]!) {
        let text = phrase.text;
        let confidence = phrase.confidence;
        print("\(confidence): \(text)")
    }



}
func transaction(transaction: SKTransaction!, didReceiveServiceResponse response: [NSObject : AnyObject]!) {  }
func transaction(transaction: SKTransaction!, didFinishWithSuggestion suggestion: String!) {  }
func transaction(transaction: SKTransaction!, didFailWithError error: NSError!, suggestion: String!) {  }



}

关于ios - 在 Swift 中使用 CocoaPods 时如何实现 Nuance Speechkit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35983121/

相关文章:

iphone - 如何从 NSMutableArray (iPhone) 获取第一个值?

iphone - 如何将 cookie 从我的 ios 应用程序共享到移动 safari?

swift - 如何修复 Xcode 10.0 调试器,它没有完全运行?

ios - podfile 中带有 url 的 pod 名称

ios - 按下时快速更改按钮背景颜色和图像颜色

c# - 欧元货币字符不显示

swift - 在 .linear 类型的 iCarousel 中快速自动滚动无限效果

arrays - 如何比较一个数组中的对象

swift - 如何在 Podfile 中为每个 Pod 设置 Legacy Swift 版本

objective-c - 具有cocoapods obj-c依赖性的swift项目