PHP code example of patrickschur / language-detection
1. Go to this page and download the library: Download patrickschur/language-detection 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/ */
patrickschur / language-detection example snippets
use LanguageDetection\Language;
$ld = new Language;
$ld->detect('Mag het een onsje meer zijn?')->close();
$ld = new Language(['de', 'en', 'nl']);
// Compares the sentence only with "de", "en" and "nl" language models.
$ld->detect('Das ist ein Test');
$ld->detect('Mag het een onsje meer zijn?')->whitelist('de', 'nn', 'nl', 'af')->close();
$ld->detect('Mag het een onsje meer zijn?')->blacklist('dk', 'nb', 'de')->close();
$ld->detect('Mag het een onsje meer zijn?')->bestResults()->close();
$ld->detect('Mag het een onsje meer zijn?')->limit(0, 3)->close();
$ld->detect('This is an example!')->close();
$ld->setTokenizer(new class implements TokenizerInterface
{
public function tokenize(string $str): array
{
return preg_split('/[^a-z0-9]/u', $str, -1, PREG_SPLIT_NO_EMPTY);
}
});
$ld->detect('Mag het een onsje meer zijn?')->blacklist('af', 'dk', 'sv')->limit(0, 4)->close();
$object = $ld->detect(Das ist ein Test');
echo $object['de'];
echo $object['en'];
echo $object['xy']; // does not exists
$t->learn('YOUR_PATH_HERE');
use LanguageDetection\Trainer;
$t = new Trainer();
$t->learn();
$t = new Trainer();
$t->setMaxNgrams(9000);
$t->learn();
$ld = new Language();
$ld->setMaxNgrams(9000);
// "grille pain" is french and means "toaster" in english
var_dump($ld->detect('grille pain')->bestResults());
text
0.6623339658444
0.56859582542694
NULL
text
class LanguageDetection\LanguageResult#5 (1) {
private $result =>
array(2) {
'fr' =>
double(0.91307037037037)
'en' =>
double(0.90623333333333)
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.