<commit_before>/*
 * Copyright (c) 2019 toastkidjp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
 */
package jp.toastkid.yobidashi.libs.translation

import com.google.firebase.ml.common.modeldownload.FirebaseModelDownloadConditions
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslateLanguage
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslateModelManager
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslateRemoteModel
import timber.log.Timber

/**
 * @author toastkidjp
 */
class TranslationModelLoader {

    private val modelManager = FirebaseTranslateModelManager.getInstance()

    operator fun invoke() {
        modelManager
                .downloadRemoteModelIfNeeded(makeModel(FirebaseTranslateLanguage.EN))
                .addOnFailureListener { Timber.e(it) }
        modelManager
                .downloadRemoteModelIfNeeded(makeModel(FirebaseTranslateLanguage.JA))
                .addOnFailureListener { Timber.e(it) }
    }

    private fun makeModel(language: Int) = FirebaseTranslateRemoteModel.Builder(language)
            .setDownloadConditions(makeCommonCondition())
            .build()

    private fun makeCommonCondition() = FirebaseModelDownloadConditions.Builder()
            .requireWifi()
            .build()
}<commit_msg>refactor #412 Add comments to TranslationModelLoader.<commit_after>/*
 * Copyright (c) 2019 toastkidjp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
 */
package jp.toastkid.yobidashi.libs.translation

import com.google.firebase.ml.common.modeldownload.FirebaseModelDownloadConditions
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslateLanguage
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslateModelManager
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslateRemoteModel
import timber.log.Timber

/**
 * Translation model loader.
 *
 * @author toastkidjp
 */
class TranslationModelLoader {

    /**
     * Language model manager.
     */
    private val modelManager = FirebaseTranslateModelManager.getInstance()

    /**
     * Download English and Japanese language models.
     */
    operator fun invoke() {
        modelManager
                .downloadRemoteModelIfNeeded(makeModel(FirebaseTranslateLanguage.EN))
                .addOnFailureListener { Timber.e(it) }
        modelManager
                .downloadRemoteModelIfNeeded(makeModel(FirebaseTranslateLanguage.JA))
                .addOnFailureListener { Timber.e(it) }
    }

    /**
     * Make model.
     *
     * @param language
     */
    private fun makeModel(language: Int) = FirebaseTranslateRemoteModel.Builder(language)
            .setDownloadConditions(makeCommonCondition())
            .build()

    /**
     * Make common condition, it contains requireWifi.
     */
    private fun makeCommonCondition() = FirebaseModelDownloadConditions.Builder()
            .requireWifi()
            .build()
}
{'fraction_non_alphanumeric': 0.08467623791385878, 'fraction_numerical': 0.005566949897450923, 'mean_word_length': 3.399484536082474, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1569232', 'n_tokens_mistral': 943, 'n_tokens_neox': 889, 'n_words': 191}