This document contains information about usage of Open Dictionary API in Android applications.

- Getting translations

- Getting base forms of words using morphology

Getting translations

1) First you need to create a OpenDictionaryAPI object, that will do all the job.

OpenDictionaryAPI api = new OpenDictionaryAPI(getApplicationContext());

2) Then you should get a Dictionary object of needed translation direction. This can be done in several ways.

//you can check if there is at least one dictionary installed that provides translations from English to Russian api.hasDictionary(new Direction(Language.English, Language.Russian);
//or get collection of all dictionaries that provide translations from English to Russian HashSet<Dictionary> dictionaries = api.getDictionaries(new Direction(Language.English, Language.Russian));
//or from English to any language HashSet<Dictionary> dictionaries = api.getDictionaries(new Direction(Language.English, Language.ANY));
//or even all dictionaries installed HashSet<Dictionary> dictionaries = api.getDictionaries();
//then you can iterate over dictionaries as usual and get information about each dictionary via its get methods. if (!dictionaries.isEmpty()) {
    Dictionary dict = dictionaries.iterator().next();
}

3) Getting translations.
Once you've got needed dictionary you can get translations from it. There are several ways to do it:

a) Launching dictionary activity that will show translation to the user. This case is most simple to implement, but you have not any control over how translation looks.

//you can ask for translation of one word dict.showTranslation("code"); //or of several words delimited by comma or space dict.showTranslation("code, friend people."); //you can ask dictionary activity to return result code to your activity via Activity.onActivityResult() method dict.showTranslation("code", activity, requestCode); //result code can be one following: Dictionary.TRANSLATION_RESULT_CODE_EXACT_MATCH
Dictionary.TRANSLATION_RESULT_CODE_DATABASE_IS_UNAVAILABLE
Dictionary.TRANSLATION_RESULT_CODE_PARTIALLY_MATCH
Dictionary.TRANSLATION_RESULT_CODE_SIMILAR_WORDS
Dictionary.TRANSLATION_RESULT_CODE_NO_MATCH

b) If you want to get translation in html format you can use following asynchronous method:

dict.getTranslationAsText("code", TranslateMode.FULL, TranslateFormat.HTML, new TranslateAsTextListener) {
    @Override
    public void onComplete(String result, TranslateMode mode) {

    }
    @Override
    public void onWordNotFound(ArrayList similarWords) {

    }
    @Override
    public void onError(Error error) {

    }
    @Override
    public void onIPCError(String details) {

    }
});

c) If you want to get translation as a picture you can use following asynchronous method:

dict.getTranslationAsImage("code", TranslateMode.FULL, 400, 220, new TranslateAsImageListener() {
    @Override
    public void onComplete(Bitmap image, TranslateMode mode) {

    }
    @Override
    public void onWordNotFound(ArrayList similarWords) {

    }
    @Override
    public void onError(Error error) {

    }
    @Override
    public void onIPCError(String details) {

    }
    @Override
    public void onOutOfMemoryError(OutOfMemoryError e) {

    }
});

Getting base forms of words using morphology

1) First you need to create a OpenDictionaryAPI object, that will do all the job.

OpenDictionaryAPI api = new OpenDictionaryAPI(getApplicationContext());

2) Then you should get a Morphology object of needed language. Here are some handy methods:

//check if morphology module for English is installed api.hasMorphology(Language.English);
//get Morphology for English Morphology morph = api.getMorphology(Language.English);
//get collection of all morphology modules installed HashSet<Morphology> morphologies = api.getMorphologies();

3) Then you can ask for base forms using asynchronous method:

morph.getBaseForms("coded", new BaseFormsListener() {
    @Override
    public void onComplete(BaseForm[] baseForms) {

    }
    @Override
    public void onWordNotFound() {

    }
    @Override
    public void onError(Error error) {

    }
    @Override
    public void onIPCError(String details) {

    }
});