我正在尝试使用在 Swift 2.2 (XCode 7.3.1) 中运行的 RapidEars 插件运行 OpenEars 的示例。但是,我怀疑在 Swift 项目中使用带有扩展的 Objective-C 接口时遇到了更大的问题(或者我对它的工作原理的理解)。
OpenEars 代码是 Obj-C。但是,我能够通过标准的 Obj-C -> Swift 翻译技术让它在我的 swift 项目中运行。
缩写代码如下。完整的例子在一个分叉的 Github 上并更新到 Swift-2.2:https ://github.com/SuperTango/OpenEars-with-Swift-
以下示例效果很好。您可以通过签出“working-opears-swift2.2”标签来查看整个项目。
OpenEarsTest-Bridging-Header.h:
#import <OpenEars/OELanguageModelGenerator.h>
#import <OpenEars/OEAcousticModel.h>
#import <OpenEars/OEPocketsphinxController.h>
#import <OpenEars/OEAcousticModel.h>
#import <OpenEars/OEEventsObserver.h>
ViewController.swift:
class ViewController: UIViewController, OEEventsObserverDelegate {
var openEarsEventsObserver = OEEventsObserver()
override func viewDidLoad() {
super.viewDidLoad()
loadOpenEars()
}
func loadOpenEars() {
self.openEarsEventsObserver = OEEventsObserver()
self.openEarsEventsObserver.delegate = self
var lmGenerator: OELanguageModelGenerator = OELanguageModelGenerator()
addWords()
var name = "LanguageModelFileStarSaver"
lmGenerator.generateLanguageModelFromArray(words, withFilesNamed: name, forAcousticModelAtPath: OEAcousticModel.pathToModel("AcousticModelEnglish"))
lmPath = lmGenerator.pathToSuccessfullyGeneratedLanguageModelWithRequestedName(name)
dicPath = lmGenerator.pathToSuccessfullyGeneratedDictionaryWithRequestedName(name)
}
func startListening() {
do {
try OEPocketsphinxController.sharedInstance().setActive(true)
OEPocketsphinxController.sharedInstance().startListeningWithLanguageModelAtPath(lmPath, dictionaryAtPath: dicPath, acousticModelAtPath: OEAcousticModel.pathToModel("AcousticModelEnglish"), languageModelIsJSGF: false)
} catch {
NSLog("Error!")
}
}
// A whole bunch more OEEventsObserverDelegate methods that are all working fine...
func pocketsphinxDidStartListening() {
print("Pocketsphinx is now listening.")
statusTextView.text = "Pocketsphinx is now listening."
}
到目前为止,一切都运行良好。
但是,为了使用“RapidEars”插件,文档 ( http://www.politepix.com/rapidears/ ) 说:
- 将框架添加到项目中并确保它被正确包含。
导入两个新文件(它们都是现有 OpenEars 类的“类别”):
#import <RapidEarsDemo/OEEventsObserver+RapidEars.h> #import <RapidEarsDemo/OEPocketsphinxController+RapidEars.h>
改变使用的方法:
startListeningWithLanguageModelAtPath
使用startRealtimeListeningWithLanguageModelAtPath
添加两个新的 OEEventsObservableDelegate 方法。
func rapidEarsDidReceiveLiveSpeechHypothesis(hypothesis: String!, recognitionScore: String!) func rapidEarsDidReceiveFinishedSpeechHypothesis(hypothesis: String!, recognitionScore: String!)
rapidears-notworking-stackoverflow
可以通过从上面的 github repo 中查看标签来找到新代码
问题1:
在 XCode 编辑器中执行完成时,编辑器会看到 WILL 对startRealtimeListeningWithLanguageModelAtPath
方法执行自动完成,但是当代码运行时,它总是失败并出现错误:
[OEPocketsphinxController startRealtimeListeningWithLanguageModelAtPath:dictionaryAtPath:acousticModelAtPath:]: unrecognized selector sent to instance 0x7fa27a7310e0
问题2:
在 XCode 编辑器中执行自动完成时,它看不到RapidEarsDemo/OEPocketsphinxController+RapidEars.h
.
我觉得这些是相关的,也与它们失败的方法被定义为 Objective-C 类的类别这一事实有关。但这只是目前的猜测。
我已确保 RapidEars 框架已导入并位于框架搜索路径中。
谁能告诉我为什么会这样?或者如果我错过了一些 Swift 魔法咒语?