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

List available translation models

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

You can also list all available translations models

<?php

namespace App\Http\Controllers;

use FindBrok\WatsonTranslate\Contracts\TranslatorInterface as WatsonTranslator;

class IndexController extends Controller
{
    /**
     * Lists models
     *
     * @param WatsonTranslator $translator
     */
     public function listModels(WatsonTranslator $translator)
     {
         //Get List of translations models
         $models = $translator->listModels()->collectResults();
         /*
         Collection {#160 ▼
             #items: array:1 [▼
                 "models" => array:10 [▼
                      0 => array:11 [▼
                          "model_id" => "ar-en"
                          "source" => "ar"
                          "target" => "en"
                          "base_model_id" => ""
                          "domain" => "news"
                          "customizable" => true
                          "default_model" => true
                          "owner" => ""
                          "status" => "available"
                          "name" => ""
                          "train_log" => null
                       ]
                       1 => array:11 [▼
                          "model_id" => "arz-en"
                          "source" => "arz"
                          "target" => "en"
                          "base_model_id" => ""
                          "domain" => "news"
                          "customizable" => true
                          "default_model" => true
                          "owner" => ""
                          "status" => "available"
                          "name" => ""
                          "train_log" => null
                      ]
                     ....
         */
         ....
      }
}

You can include these model_id in your config/watson-translate.php file and use any of them

'models' => [
    'default' => env('WATSON_TRANSLATE_DEFAULT_MODEL', 'en-fr')
    'arabic_to_english' => 'ar-en'
],

//Then using model translation
$translation = $translator->usingModel('arabic_to_english')->textTranslate($text)->getTranslation();

Filters:

When retrieving Models you may specify some filters.

  1. Default models only
//Get default models only
$models = $translator->listModels(true)->collectResults();
  1. Non-default models only
//Get non-default models only
$models = $translator->listModels(false)->collectResults();
  1. Source filter
//Get models with en as source language
$models = $translator->listModels(null, 'en')->collectResults();
  1. Target filter
//Get models with fr as target language
$models = $translator->listModels(null, null, 'fr')->collectResults();
  1. Combination of filters
//Get default models with en as source language and fr as target language
$models = $translator->listModels(true, 'en', 'fr')->collectResults();