andywer/language-detector
Composer 安装命令:
composer require andywer/language-detector
包简介
PHP library to detect the language of any free text.
README 文档
README
PHP library to detect languages from any free text.
It follows the approach described in the paper, a given text is tokenized into N-Grams (we cleanup whitespaces before doing this step). Then we sort the tokens and we compare against a language model.
Fork of crodas/languagedetector, since the original package seems abandoned.
How it works
The first thing we need is a language model (which looks like this file) that is used to compare the texts against at classification time. This process must done before anything, and it can be generated with an script similar to this file.
// register the autoloader require 'lib/LanguageDetector/autoload.php'; // it could use a little bit of memory, but it's fine // because this process runs once. ini_set('memory_limit', '1G'); // we load the configuration (which will be serialized // later into our language model file $config = new LanguageDetector\Config; $c = new LanguageDetector\Learn($config); foreach (glob(__DIR__ . '/samples/*') as $file) { // feed with examples ('language', 'text'); $c->addSample(basename($file), file_get_contents($file)); } // some callback so we know where the process is $c->addStepCallback(function($lang, $status) { echo "Learning {$lang}: $status\n"; }); // save it in `datafile`. // we currently support the `php` serialization but it's trivial // to add other formats, just extend `\LanguageDetector\Format\AbstractFormat`. //You can check example at https://github.com/crodas/LanguageDetector/blob/master/lib/LanguageDetector/Format/PHP.php $c->save(AbstractFormat::initFormatByPath('language.php'));
Once we have our language model file (in this case language.php) we're ready to classify texts by their language.
// register the autoloader require 'lib/LanguageDetector/autoload.php'; // we load the language model, it would create // the $config object for us. $detect = LanguageDetector\Detect::initByPath('language.php'); $lang = $detect->detect("Agricultura (-ae, f.), sensu latissimo, est summa omnium artium et scientiarum et technologiarum quae de terris colendis et animalibus creandis curant, ut poma, frumenta, charas, carnes, textilia, et aliae res e terra bene producantur. Specialius, agronomia est ars et scientia quae terris colendis student, agricultio autem animalibus creandis.") var_dump($lang);
And that's it.
Algorithms
The project is designed to work with modules, which means you can provide your own algorithm for sorting and comparing the N-Grams. By default the library implements the PageRank as sorting algorithm, and out of place (described in the paper) as comparing.
In order to supply your own algorithms, you must change the $config at learning stage to load your own classes (which by the way should implement some interaces).
Language Detection Training Files
Have a look at example/samples directory. For more advanced traning data, visit the Leipzig Corpora Download Page.
Languages with non-latin characters
Remember to set the Config's mb property (already before creating the language model) if you train for languages based on non-latin characters. Use UTF-8 encoded texts.
andywer/language-detector 适用场景与选型建议
andywer/language-detector 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 09 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 andywer/language-detector 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 andywer/language-detector 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 26
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-4-Clause
- 更新时间: 2015-09-29