PHP code example of avvertix / language-detect-php

1. Go to this page and download the library: Download avvertix/language-detect-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

avvertix / language-detect-php example snippets


use Avvertix\WhatLang\WhatLang;

$info = WhatLang::detect('Ĉu vi ne volas eklerni Esperanton? Bonvolu! Estas unu de la plej bonaj aferoj!');

$info->lang;                // Lang::Epo
$info->lang->value;         // 'epo'  (ISO 639-3)
$info->lang->nativeName();  // 'Esperanto'
$info->lang->englishName(); // 'Esperanto'
$info->script;              // Script::Latin
$info->confidence;          // 1.0
$info->isReliable();        // true

WhatLang::detect('1234567890-,;!'); // null

WhatLang::detectLang('There is no reason not to learn Esperanto.'); // Lang::Eng
WhatLang::detectScript('Благодаря Эсперанто');                      // Script::Cyrillic

use Avvertix\WhatLang\Detector;
use Avvertix\WhatLang\Lang;

$detector = Detector::withAllowlist([Lang::Eng, Lang::Rus]);
$detector->detectLang('That is not Russian'); // Lang::Eng

$detector = Detector::withDenylist([Lang::Eng, Lang::Ita]);
$detector->detectLang('Jen la trinkejo fermitis, ni iras tra mallumo kaj pluvo.'); // Lang::Epo

$info = WhatLang::detect($text);

if (! $info?->isReliable()) {
    // too little to go on — fall back to a default, or ask
}

use Avvertix\WhatLang\Method;
use Avvertix\WhatLang\Options;

WhatLang::detect($text, new Options(method: Method::Trigram));

use Avvertix\WhatLang\Lang;
use Avvertix\WhatLang\Script;

Lang::all();               // 70 languages
Script::all();             // 25 scripts
Script::Cyrillic->langs(); // [Lang::Rus, Lang::Ukr, Lang::Srp, Lang::Bel, Lang::Bul, Lang::Mkd]
Lang::Jpn->scripts();      // [Script::Hiragana, Script::Katakana]
bash
composer 
bash
php tools/generate-fixture.php [path-to-whatlang-rs]
bash
php tools/generate-data.php [path-to-whatlang-rs]