PHP code example of nitotm / efficient-language-detector

1. Go to this page and download the library: Download nitotm/efficient-language-detector 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/ */

    

nitotm / efficient-language-detector example snippets


$eld = new Nitotm\Eld\LanguageDetector();
// s?'));
// object( language => 'es', scores => ['es' => 0.5, 'et' => 0.2], isReliable() => true )
// object( language => null|string, scores => null|array, isReliable() => bool )

print $eld->detect('Hola, cómo te llamas?')->language;
// 'es'

// if cleanText(True), detect() removes Urls, .com domains, emails, alphanumerical & numbers
$eld->cleanText(true); // Default is false

$langSubset = ['en','es','fr','it','nl','de'];

/* Option #1
 With dynamicLangSubset() the detector executes normally, and then filters excluded languages
*/
$eld->dynamicLangSubset($langSubset);
// Returns an object with an ?array property named 'languages', with the validated languages

/* Option #2
 langSubset($langs, save: true) removes excluded languages from the Ngrams database
 For a single detection is slower than dynamicLangSubset(), but for several will be faster
 If $save option is true (default), the new Ngrams subset will be stored, and loaded next call
*/
var_dump($eld->langSubset($langSubset)); // returns the subset file name if saved
// Object ( success => bool, languages => null|[], error => null|string, file => null|string )

// To remove either dynamicLangSubset() or langSubset(), call the methods without any argument
$eld->langSubset(); 

/* Option #3
 Finally the optimal way to regularly use a language subset: we create the instance with a file
 The file in the argument can be a subset by langSubset() or another database like ngramsL60.php
*/
$eld_L = new Nitotm\Eld\LanguageDetector('ngramsL60.php');

var_dump($eld->info());

new Nitotm\Eld\Tests\TestsAutoload();
bash
$ php efficient-language-detector/tests/tests.php # Update path