Skip to content
This repository has been archived by the owner on May 28, 2023. It is now read-only.

Translating using translation models

Percy Mamedy edited this page Feb 5, 2016 · 2 revisions

Examples: Using translation models for performing translation

Using a translation model means that you need not specify the source and the target language as they are already defined in the model. Translation model also contains features of machine learning, which means that Watson has been trained based on your model to suit your interpretation and translation corpus. Checkout translation model creation and machine training here

Configure your models first. In your config/watson-translate.php file add your model name (Any name you choose) and model Id to the config array. A sensible default model is set which will be used when no model name is specified.

    //config/watson-translate.php

    /*
    |--------------------------------------------------------------------------
    | Translations Models
    |--------------------------------------------------------------------------
    |
    | Here you may specify a name to give to each Watson Language translation
    | Service models to be used. You will use these model names instead of their
    | id when performing translations. Feel free to add as many models as you want
    | and give any name that you see fit.
    |
    */
    'models' => [
        'default' => env('WATSON_TRANSLATE_DEFAULT_MODEL', 'en-fr')
    ],

Translate using the model name

<?php

namespace App\Http\Controllers;

...
use FindBrok\WatsonTranslate\Contracts\TranslatorInterface as WatsonTranslator;

class IndexController extends Controller
{
    /**
     * Translate demo
     *
     * @param WatsonTranslator $translator
     */
     public function translate(WatsonTranslator $translator)
     {
       //Text to translate
       $text = 'Daniel\'s parents love the beach. Daniel and his sister and brother love the beach. The family\'s dog loves the beach very much.';
       //Translate
       $results = $translator->usingModel('default')->textTranslate($text)->getTranslation();
       ...
     }
}

When you do not specify the model name to use the default model is used. Keep in mind that using translation models will override source and target language