我试过这个包http://pear.php.net/package/Text_LanguageDetect/
它在 PHP 5.4 上非常准确。但我在 PHP 7 上失败了。知道为什么吗?
我运行的脚本:
<?php
require_once 'Text/LanguageDetect.php';
$l = new Text_LanguageDetect;
$stdin = fopen('php://stdin', 'r');
echo "Supported languages:\n";
try {
$langs = $l->getLanguages();
} catch (Text_LanguageDetect_Exception $e) {
die($e->getMessage());
}
sort($langs);
echo join(', ', $langs);
//$l->setNameMode(2);
while ($line = fgets($stdin)) {
print_r($l->detect($line, 4));
//print_r($l->detectSimple($line));
}
输入:
This is English. What does this mean?
PHP 7.0.9 上的相同脚本
This is English. What does this mean?
Array
(
[hungarian] => 0.20114942528736
[albanian] => 0.19632183908046
[danish] => 0.1848275862069
[french] => 0.17183908045977
)
PHP 上的相同脚本。5.4.34
This is English. What does this mean?
Array
(
[english] => 0.34965517241379
[dutch] => 0.24896551724138
[latin] => 0.23931034482759
[hungarian] => 0.20114942528736
)