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

时间:2016-03-14 08:56:13

标签: ios swift cocoapods speech-to-text speechkit

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

1 个答案:

答案 0 :(得分:2)

  1. 首先安装CocoaPod:https://cocoapods.org/pods/SpeechKit
  2. #import <SpeechKit/SpeechKit.h>添加到您的桥接标题
  3. 登录Nuance的开发门户并创建应用:https://developer.nuance.com/
  4. 清理演示代码,使其更有条理。我只想尽可能多地将代码放在一个地方,这样你就可以看到一个完全正常的实现。
  5. 然后创建一个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!) {  }
    
    
    
    }