LanguageDetector is a PHP library that detects the language from texts.
- More than 50 supported languages
- Very fast, no database needed
- Packaged with a 2MB dataset
- Small code, easy to modify
- N-grams algorithm
- Supports PHP 5.4, 5.5, 5.6, 7.0, 7.1 and HHVM
composer require landrok/language-detector:1.0
For a one-shot detection only. This is not recommended if you want to make several consecutive evaluations.
require_once 'vendor/autoload.php';
// One line usage: instanciate, evaluate and get language
$lang = ( new LanguageDetector\LanguageDetector() )
->evaluate('My sentence is in english')
->getLanguage();
If you have a list of texts to detect, a possible way is to instanciate once and then to translate your list.
require_once 'vendor/autoload.php';
$texts = [
'My sentence is in english',
'My sentence number two is in english too'
];
$results = [];
$detector = new LanguageDetector\LanguageDetector();
foreach ($texts as $text)
{
$results[] = $detector->evaluate($text)->getLanguage();
}
LanguageDetector has other information methods:
[...]
// Gets all supported languages
$languages = $detector->getSupportedLanguages();
// Gets all scores
$scores = $detector->getScores();
####evaluate()
Type \LanguageDetector\LanguageDetector
It performs an evaluation on a given text.
Example
$detector->evaluate('My tailor is rich and Alison is in the kitchen with Bob.');
// Then you have access to the detected language
$detector->getLanguage(); // Returns 'en'
You can make a one line call
$detector->evaluate('My tailor is rich and Alison is in the kitchen with Bob.')->getLanguage(); // Returns 'en'
####getLanguage()
Type string
The detected language
Example
$detector->getLanguage(); // Returns 'en'
####getScores()
Type array
A list of scores by language, for all evaluated languages.
Example
$detector->getScores();
// Returns something like
Array
(
[en] => 0.43950135722745
[nl] => 0.40898789832569
[...]
[ja] => 0
[fa] => 0
)
####getSupportedLanguages()
Type array
A list of supported languages that will be evaluated.
Example
$detector->getSupportedLanguages();
// Returns something like
Array
(
[0] => af
[1] => ar
[...]
[51] => zh-cn
[52] => zh-tw
)
####getText()
Type string
Returns the last string which has been evaluated
Example
$detector->getText();
// Returns 'My tailor is rich and Alison is in the kitchen with Bob.'