首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Swift中使用CocoaPods实现Nuance Speechkit

如何在Swift中使用CocoaPods实现Nuance Speechkit
EN

Stack Overflow用户
提问于 2016-03-14 08:56:13
回答 1查看 951关注 0票数 1

我花了很长时间才弄明白如何使用SpeechKit + CocoaPod + Swift实现语音到文本的工作。终于成功了,所以我想我会帮助下一个寻求帮助的可怜人!:)

EN

回答 1

Stack Overflow用户

发布于 2016-03-14 08:56:13

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

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

代码语言:javascript
复制
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!) {  }



}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35983121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档