Unable to obtain correct path to language model

Home Forums OpenEars Unable to obtain correct path to language model

Viewing 4 posts - 1 through 4 (of 4 total)

  • Author
  • #1032667
    Mike Stein

    Hello, OpenEars Forum Members.

    I’d appreciate any help you could provide me in helping me get my OpenEars application to work by obtaining returning the correct path to the language model that the LanguageModelGenerator generated. Please bear with me in my attempts to provider enough information for your assistance; I’m excited to see my app come to fruition but am not in the computer field and recently just taught myself Swift and how to use Xcode (I began programming my app in Swift playgrounds and imported my code into an Xcode project). The app’s main purpose is to make a simple speech to text dictation device. Here is what I believe to be the relevant sections of my Swift code that relates to the problem I’m having:

    let lmGenerator = OELanguageModelGenerator()
    //*Disable* next line when actually running App: used for debugging
    lmGenerator.verboseLanguageModelGenerator = true
    let content = manager.contents(atPath: filePath!)
    inputDict = String(data: content!, encoding: .utf8)!
    //Clean up string file of words in preparation for converting to array; program reads vocab words from a string and formats to array
    inputDict = inputDict.replacingOccurrences(of: “[“, with: “”)
    inputDict = inputDict.replacingOccurrences(of: “]”, with: “”)
    inputDict = inputDict.replacingOccurrences(of: “\n”, with: “”)
    inputDictArray = inputDict.components(separatedBy: “, “)
    langModelFile = “langModelFile”
    let err:Error! = lmGenerator.generateLanguageModel(from: inputDictArray, withFilesNamed: langModelFile, forAcousticModelAtPath: OEAcousticModel.path(toModel: “AcousticModelEnglish”))
    if(err != nil) {
    statusLabel.text = “Error while creating initial language model”
    } else {
    lmPath?.text = lmGenerator.pathToSuccessfullyGeneratedLanguageModel(withRequestedName: langModelFile)
    dicPath?.text = lmGenerator.pathToSuccessfullyGeneratedDictionary(withRequestedName: langModelFile)
    statusLabel.text = “Language model successfully created”
    do {
    try OEPocketsphinxController.sharedInstance().setActive(true)
    } catch {
    statusLabel.text = “Error: unable to activate shared instance to record”
    print(“Error: it wasn’t possible to set the shared instance to active: \”\(error)\””)}

    **The above code seems to work well; my own statusLabel tells me that the language model was created. In addition, when I read the “verbose” comments from the LanguageModelGenerator it seems to indicate that it took the vocab words I entered and made a model (no errors). However, when I then attempt to call my dictation function and actually get OEPocketsphinxController to actually listen with the model above it turns out the two objects’s properties (lmPath.text and dicPath.text) I use to store the paths to both the successfully generated language model and dictionary are actually both “nil” and thus OEPocketsphinxController doesn’t attempt to listen/transcribe any of my speech. Here is the section of the code that doesn’t work (doesn’t actually start listening to my speech–instead it “hangs” in a pre-listening mode) presumably due to both of the paths above being “nil” and required to start listening:

    @objc func dictatePressed(_sender:UIButton){
    if OEPocketsphinxController.sharedInstance().isListening == true {
    statusLabel.text = “Already in dictation session”
    return } //failsafe don’t restart dictation if so
    else {
    statusLabel.text = “Sphinx preparing to listen”
    OEPocketsphinxController.sharedInstance().startListeningWithLanguageModel(atPath: lmPath?.text, dictionaryAtPath: dicPath?.text, acousticModelAtPath: OEAcousticModel.path(toModel: “AcousticModelEnglish”), languageModelIsJSGF: false)

    The verbose error message I get during the above code is below:

    2018-12-26 17:28:54.796646-0800 SphinxTranscriber3[1369:495485] Attempting to start listening session from startListeningWithLanguageModelAtPath:
    2018-12-26 17:28:54.796921-0800 SphinxTranscriber3[1369:495485] Error: you have invoked the method:

    startListeningWithLanguageModelAtPath:(NSString *)languageModelPath dictionaryAtPath:(NSString *)dictionaryPath acousticModelAtPath:(NSString *)acousticModelPath languageModelIsJSGF:(BOOL)languageModelIsJSGF

    with a languageModelPath which is nil. If your call to OELanguageModelGenerator did not return an error when you generated this language model, that means the correct path to your language model that you should pass to this method’s languageModelPath argument is as follows:

    NSString *correctPathToMyLanguageModelFile = [myLanguageModelGenerator pathToSuccessfullyGeneratedLanguageModelWithRequestedName:@”TheNameIChoseForMyVocabulary”];

    Feel free to copy and paste this code for your path to your language model, but remember to replace the part that says “TheNameIChoseForMyVocabulary” with the name you actually chose for your language model or you will get this error again (and replace myLanguageModelGenerator with the name of your OELanguageModelGenerator instance). Since this file is required, expect an exception or undocumented behavior shortly.

    *My interpretation of the above error message is that I should use some different code then what I used to attempt to obtain the paths to the language model and dictionary generated. Unfortunately, I have not learned Objective C, and if that is what the error message means, I cannot determine what I should write differently in Swift.

    I am currently testing my App on my iPadPro (9.7 inch) using iOS 12.1 and Xcode 10.1 on my iMac. Otherwise, my App builds and the test app installs on my iPadPro fine.

    I appreciate any help you can provide me on this matter.




    Halle Winkler

    Welcome Mike,

    If you check out the Swift tutorial tool (using the first switch for OpenEars alone), it will show you how to use the functions which give you the path to the generated language models:


    Since lmPath is storing a returned string, it seems unlikely that lmPath.text is what you want. Another working example of obtaining this path in Swift can be seen in the Swift sample app that is in the OpenEars distribution folder.

    If the issue is due to something else, it is necessary to turn on OpenEarsLogging and show all of the logging from the beginning to the end of the app run so I can help. It is possible that the language model generation isn’t working (it looks like this model is being generated out of a document, so I can imagine some complications there), so after the failed generation there is nothing to pass to the string, and the full logging output will indicate this if so.

    Mike Stein

    Thanks, Halle.

    I looked at the Swift sample app and did figure it out, and now it works fine. It worked just defining a view controller property “var pathToFirstDynamicallyGeneratedLanguageMode:String!” like you did in the sample App and then setting that variable = to the OELanguageModelGenerator method of obtaining the path to the language model in my main program. In retrospect it seemed inefficient of me to try and use the text property of a UITextField control property I assigned to my view controller as I forgot that any view controller property should be available for use in any of my view controller methods (although still not sure why assigning the value to a UIControlTextField.text property like I did wouldn’t also have worked). Still learning…

    Happy upcoming New Year,


    Halle Winkler

    Good fix! Happy New Year to you as well.

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘Unable to obtain correct path to language model’ is closed to new replies.