iink SDK on Device

Answered

Words from lexicon not being recognized

Hi,


I followed instructions for creating and adding new lexicon into iOS application.

My lexicon contains few words: "boja moja tvoja sunce olovka papir curica juhica". IInkSDK is configured to work with en_us and this words are in Croatian.

When I write this words with pen I don't get them recognized all the time. Sometimes they are recognized and sometimes they aren't.

After reading lexicon description I assumed that I can inject words from any language into lexicon in order to expand recognition but this just doesn't work for me. Is this possible overall?


This is my config file:

    

Bundle-Version: 1.0
Bundle-Name: en_US
Configuration-Script:
 AddResDir ../resources/

Name: text
Type: Text
Configuration-Script:
 AddResource en_US/en_US-ak-cur.res
 AddResource en_US/en_US-lk-text.res
 EnableAlienCharacters
 SetTextListSize 1
 SetWordListSize 5
 SetCharListSize 1

Name: textwithMyResource
Type: Text
Configuration-Script:
 AddResource en_US/en_US-ak-cur.res
 AddResource en_US/en_US-lk-text.res
 AddResource en_US/lexicon.res
 EnableAlienCharacters
 SetTextListSize 1
 SetWordListSize 5
 SetCharListSize 1 

  Here is my engine initialization:

 

lazy var engine: IINKEngine? = {
        // Check that the MyScript certificate is present

        guard myCertificate.length > 0 else {
            self.engineErrorMessage = "Please replace the content of MyCertificate.c with the certificate you received from the developer portal"
            return nil
        }

        // Create the iink runtime environment
        let data = Data(bytes: myCertificate.bytes, count: myCertificate.length)
        guard let engine = IINKEngine(certificate: data) else {
            self.engineErrorMessage = "Invalid certificate"
            return nil
        }

        // Configure the iink runtime environment
        let configurationPath = Bundle.main.bundlePath.appending("/recognition-assets/conf")
        print(FileManager.default.fileExists(atPath: configurationPath))
        do {
            // Tells the engine where to load the recognition assets from.
            try engine.configuration.setStringArray([configurationPath], forKey:"configuration-manager.search-path")
        } catch {
            print("Should not happen, please check your resources assets : " + error.localizedDescription)
            return nil
        }

        // Set the temporary directory
        do {
            try engine.configuration.setString(NSTemporaryDirectory(), forKey: "content-package.temp-folder")
        } catch {
            print("Failed to set temporary folder: " + error.localizedDescription)
            return nil
        }
        return engine
    }()

 And later after initialization of EditViewController I set configuration for new lexicon:

 

editorViewController.engine = engine
            try! editorViewController.editor.configuration.setString("textwithMyResource", forKey: "text.configuration.name");

 Am I doing something wrong?


Thanks

 




Best Answer

Dear Josip,

currently, you have to way to build your own lexicon:
1-Either you use the online tools: https://developer.myscript.com/support/recognition-assets
2-Or the assets builder API on device: https://developer.myscript.com/refguides/interactive-ink/android/1.3/com/myscript/iink/RecognitionAssetsBuilder.html

1-You shall deploy the compiled resource either in your favorite directory on your device or in the existing "resource" directory of your application (e.g. en_US)

If in your "myDirectory", then your conf file shall be as follows:

Bundle-Version: 1.0
Bundle-Name: en_US
Configuration-Script:
AddResDir ../resources/
AddResDir myDirectory

Name: text
Type: Text
Configuration-Script:
AddResource en_US/en_US-ak-cur.res
AddResource en_US/en_US-lk-text.res
EnableAlienCharacters
SetTextListSize 1
SetWordListSize 5
SetCharListSize 1

Name: textwithMyResource
Type: Text
Configuration-Script:
AddResource en_US/en_US-ak-cur.res
AddResource en_US/en_US-lk-text.res
AddResource lexicon.res
EnableAlienCharacters
SetTextListSize 1
SetWordListSize 5
SetCharListSize 1


if in the existing "resource/en_US" directory of your application:
Bundle-Version: 1.0
Bundle-Name: en_US
Configuration-Script:
AddResDir ../resources/

Name: text
Type: Text
Configuration-Script:
AddResource en_US/en_US-ak-cur.res
AddResource en_US/en_US-lk-text.res
EnableAlienCharacters
SetTextListSize 1
SetWordListSize 5
SetCharListSize 1

Name: textwithMyResource
Type: Text
Configuration-Script:
AddResource en_US/en_US-ak-cur.res
AddResource en_US/en_US-lk-text.res
AddResource en_USlexicon.res
EnableAlienCharacters
SetTextListSize 1
SetWordListSize 5
SetCharListSize 1


2-If using the assets builder API, the compiled resource shall indeed be stored using the "store" function. If any io error is raised, this means the resource cannot be saved in the given directory. You shall then use another directory, or look for the reason why it cannot be saved (there can have many reasons, most of these are iOS related, I will therefore not talk about these).


If the resoource is store in the "myResources" directory, you conf file shall be as follows (ensure you provide with the correct path for myResources):


Bundle-Version: 1.0
Bundle-Name: en_US
Configuration-Script:
AddResDir ../resources/
AddResDir myResources/

Name: text
Type: Text
Configuration-Script:
AddResource en_US/en_US-ak-cur.res
AddResource en_US/en_US-lk-text.res
EnableAlienCharacters
SetTextListSize 1
SetWordListSize 5
SetCharListSize 1

Name: textwithMyResource
Type: Text
Configuration-Script:
AddResource en_US/en_US-ak-cur.res
AddResource en_US/en_US-lk-text.res
AddResource lexicon.res
EnableAlienCharacters
SetTextListSize 1
SetWordListSize 5
SetCharListSize 1


You shall then call your resource as follows: try! editorViewController.editor.configuration.setString("textwithMyResource", forKey: "text.configuration.name");

Please check all the above points, as there should not have any other difficulty.

Best regards,

Olivier


Dear Josip,

Currently, when creating a text custom configuration with custom resources, it applies to both "Text" and "Text Document" parts. I am then puzzled by the behavior you are describing. Are you doing something particular in your application? are you in batch mode? or anything else?

As stated in our documentation, in a "Text Document" part, the text configuration will apply if you set it properly when starting your application:  editorViewController.editor.configuration.setString("textwithMyResource", forKey: "text.configuration.name");

Please note you shall ensure your configuration is set before creating your contentPackage: https://developer.myscript.com/docs/interactive-ink/1.3/overview/content-types//#supported-content-types

But then I don't know how to reference this inside .config file? I can't write absolute paths there neither I can write path to Temp / Documents directory.
Currently, in your conf file, you shall set the directory path of your custom resource using the "AddResDir" instruction. The problem is that it only accepts relative paths to your application. You shall then manage by yourself to get it.

Best regards,

Olivier

Hi Oliver,

Thank you for your answers but unfortunately I still have two problems.

First thing that I noticed is that when I use on device resources (the one that I created using online tool). 

Then new lexicon only works with part type "Text" but I want it to work with "Text Document".

try uPackage.createPart("Text") // I want "Text Document"

Inside docs I see that only possible values for Type inside config are following: "Possible values for the Type key are: Text, Math, Shape and Analyzer. They correspond to the types of content that the core MyScript technology is able to recognize.". Do you know why I don't get lexicon work with Text Document type? This is my configuration file:

Name: text2
Type: Text
Configuration-Script:
 AddResource en_US/en_US-ak-cur.res
 AddResource en_US/en_US-lk-text.res
 AddResource en_US/lexicon.res
 EnableAlienCharacters
 SetTextListSize 1
 SetWordListSize 5
 SetCharListSize 1

 

Second problem is related to on device created lexicon. Only way to save newly created resource is to provide full path on disk such as: 

let lexPath = NSTemporaryDirectory().appending("lexicon2.res")
try assetsBuilder?.store(lexPath)

 But then I don't know how to reference this inside .config file? I can't write absolute paths there neither I can write path to Temp / Documents directory.


Thanks,

Josip

Answer

Dear Josip,

currently, you have to way to build your own lexicon:
1-Either you use the online tools: https://developer.myscript.com/support/recognition-assets
2-Or the assets builder API on device: https://developer.myscript.com/refguides/interactive-ink/android/1.3/com/myscript/iink/RecognitionAssetsBuilder.html

1-You shall deploy the compiled resource either in your favorite directory on your device or in the existing "resource" directory of your application (e.g. en_US)

If in your "myDirectory", then your conf file shall be as follows:

Bundle-Version: 1.0
Bundle-Name: en_US
Configuration-Script:
AddResDir ../resources/
AddResDir myDirectory

Name: text
Type: Text
Configuration-Script:
AddResource en_US/en_US-ak-cur.res
AddResource en_US/en_US-lk-text.res
EnableAlienCharacters
SetTextListSize 1
SetWordListSize 5
SetCharListSize 1

Name: textwithMyResource
Type: Text
Configuration-Script:
AddResource en_US/en_US-ak-cur.res
AddResource en_US/en_US-lk-text.res
AddResource lexicon.res
EnableAlienCharacters
SetTextListSize 1
SetWordListSize 5
SetCharListSize 1


if in the existing "resource/en_US" directory of your application:
Bundle-Version: 1.0
Bundle-Name: en_US
Configuration-Script:
AddResDir ../resources/

Name: text
Type: Text
Configuration-Script:
AddResource en_US/en_US-ak-cur.res
AddResource en_US/en_US-lk-text.res
EnableAlienCharacters
SetTextListSize 1
SetWordListSize 5
SetCharListSize 1

Name: textwithMyResource
Type: Text
Configuration-Script:
AddResource en_US/en_US-ak-cur.res
AddResource en_US/en_US-lk-text.res
AddResource en_USlexicon.res
EnableAlienCharacters
SetTextListSize 1
SetWordListSize 5
SetCharListSize 1


2-If using the assets builder API, the compiled resource shall indeed be stored using the "store" function. If any io error is raised, this means the resource cannot be saved in the given directory. You shall then use another directory, or look for the reason why it cannot be saved (there can have many reasons, most of these are iOS related, I will therefore not talk about these).


If the resoource is store in the "myResources" directory, you conf file shall be as follows (ensure you provide with the correct path for myResources):


Bundle-Version: 1.0
Bundle-Name: en_US
Configuration-Script:
AddResDir ../resources/
AddResDir myResources/

Name: text
Type: Text
Configuration-Script:
AddResource en_US/en_US-ak-cur.res
AddResource en_US/en_US-lk-text.res
EnableAlienCharacters
SetTextListSize 1
SetWordListSize 5
SetCharListSize 1

Name: textwithMyResource
Type: Text
Configuration-Script:
AddResource en_US/en_US-ak-cur.res
AddResource en_US/en_US-lk-text.res
AddResource lexicon.res
EnableAlienCharacters
SetTextListSize 1
SetWordListSize 5
SetCharListSize 1


You shall then call your resource as follows: try! editorViewController.editor.configuration.setString("textwithMyResource", forKey: "text.configuration.name");

Please check all the above points, as there should not have any other difficulty.

Best regards,

Olivier

Quick update. Things started working when I use lexicon from bundle within default "text" configuration inside .conf file.

On the other hand using separate configuration (textwithMyResource) still doesn't work. After adding config to editor makes no change on recognition

 

try! editorViewController.editor.configuration.setString("textwithMyResource", forKey: "text.configuration.name");

I'm looking forward if we can solve this on-device issue too.

Thanks,

Josip

 

Hi Oliver,

Previous implementation contained all words in same line. Today I created new lexicon.lex and compiled it using MyScript web tool but again same result. 

I wasn't able to attach file inside this response. You can find compiled file here and original .txt file here.

Beside using lexicon from app bundle, I tried to compile it on-device but I'm not sure how to reference it after storing it to disk.

Thing that I don't understand when using on-device compile are paths. I understand that when using lexicon from bundle then inside .conf file I can reference it like following:

AddResource MyResources/lexicon.res

Docs says following for on-device compilation and storing

// 3. Save it to the disc 

[assetsBuilder store:@"custom-grammar.res" error:nil];

Only way to have this stored without "io error" is to create file path like following:

 

let lexPath = NSTemporaryDirectory().appending("lexicon2.res")
            try assetsBuilder?.store(lexPath)

but then I'm having problem how to reference this inside .conf file?


Thanks,

Josip

 


Dear Josip,


Thank you for contacting us.


Can you confirm each work is added on a separate line? Indeed, in a lexicon, words have to be separated in a line.


If this is the case, can you please provide us with the source code of your lexicon?


Best regards,


Olivier