ideea / language-detector

通过文本检测语言

此包的规范存储库似乎已丢失,因此包已被冻结。

v1.1 2014-07-08 10:17 UTC

This package is auto-updated.

Last update: 2020-12-27 09:14:01 UTC


README

从字符串检测语言

安装

在您的 composer.json 中添加 LanguageDetector

{
    "require": {
        "ideea/language-detector": "~1.0"
    }
}

现在运行以下命令让 composer 下载库

$ php composer.phar update "ideea/language-detector"

与语言检测器一起工作

use Ideea\LanguageDetector\LanguageDetector;
use Ideea\LanguageDetector\Languages;

$detector = LanguageDetector::createDefault();
$text = 'your text';

$languages = $detector->detect($text);

// Get all votes languages
$votedLanguages = $languages->getVoteLanguages();
// Get all high votes languages
$highVotesLanguages = $languages->getVoteLanguages(Languages::VOTED_HIGH);
// Get the primary language of text
$language = $languages->getPrimaryLanguage();

注意:文本越多,定义越好

可检测的语言

  • BG
  • CS
  • DA
  • DE
  • EL
  • EN
  • ES
  • ET
  • FI
  • FR
  • GA
  • HR
  • HU
  • IT
  • LT
  • LV
  • MT
  • NL
  • PL
  • PT
  • RO
  • RU
  • SK
  • SL
  • SV
  • UK

检测系统

检测系统与访问者模式一起工作。每个访问者都可以通过语言投票或贡献。可用的访问者

  • SectionVisitor
  • AlphabetVisitor
  • DictionaryVisitor

您可以创建自定义访问者并将其添加到检测器中。

use Ideea\LanguageDetector\Visitor\VisitorInterface;

class CustomVisitor implements VisitorInterface
{
    /**
     * {@inheritDoc}
     */
    public function visit($string, array &$codes, \Ideea\LanguageDetector\Languages $languages)
    {
        // Your code here
    }
}

$customVisitor = new CustomVisitor();

$detector->addVisitor($customVisitor);

每个访问者都可以添加、删除检测语言,投票和贡献语言。

词典

此组件为可用的语言提供了 *.dict 文件词典。您可以将这些词典写入 Redis 存储并使用检查工作在词典中检查语言。

您可以使用 RedisDictionary 并从词典中填充此字典。

use Ideea\LanguageDetector\Util\Util;

// Create new redis instance
$redis = new \Redis();
$redis->connect($host, $port, $timeout);

Util::populateRedisDictionary($redis, __DIR__ . '/data/dictionary', null, function ($file, $count){
    print "File: " . $file . "\n";
    print "Count: " . $count . "\n";
    print "Memory: " . round(memory_get_usage() / (1024 * 1024), 2) . " Mb\n\n";
});

注意:如果您想使用前缀来为 redis 字典设置,则使用 redis 系统前缀 Redis::setOption(Redis::OPT_PREFIX, 'your_prefix')

注意:所有词典均由 ASPELL 词典生成。

生成词典

您可以为自定义词典。此包使用来自 ASPELL 工具的词典 http://ftp.gnu.org/gnu/aspell/dict/

aspell 生成新词典的示例

步骤 #1: 如果词典在系统中找不到,则从源加载词典

$ wget http://source-to-aspell-dicts.com/path/to/dict.tar
# Unpack package
$ cd /path/to/unpacked/dict
$ ./configure
$ make
$ make install

步骤 #2: 从 aspell 词典获取单词列表

$ aspell -d {LANG} dump master | aspell -l {LANG} expand > /data/dictionary/{LANG}.dict

其中:{LANG} - ISO-639-2 语言代码

然后您可以仅为此词典填充 redis 字典。

Util::populateRedisDictionary($redis, '/data/dictionary/{LANG}.dict', '{LANG}');

缓存加载器(字母和部分)

在生产环境中,您必须使用缓存系统,因为解析 *.yml 文件是简单的功能(检测语言)的高负载操作。

字母和部分加载器具有 FileCacheLoader 以使用序列化/反序列化函数缓存数据。

使用示例

use Ideea\LanguageDetector\Section\Loader\YamlLoader as SectionLoader;
use Ideea\LanguageDetector\Section\Loader\FileCacheLoader as SectionCacheLoader;
use Ideea\LanguageDetector\Alphabet\Loader\YamlLoader as AlphabetLoader;
use Ideea\LanguageDetector\Alphabet\Loader\FileCacheLoader as AlphabetCacheLoader;

// Create a base loaders
$sectionLoader = new SectionLoader();
$alphabetLoader = new AlphabetLoader();

// Create wrapping for loaders
$sectionLoader = new SectionCacheLoader($sectionLoader, '/path/to/cache/dir');
$alphabetLoader = new AlphabetCacheLoader($alphabetLoader, '/path/to/cache/dir');

许可证

此库采用MIT许可证。请参阅库中的完整许可证。

LICENSE

报告问题或功能请求

问题和功能请求在GitHub问题跟踪器中跟踪。

感谢帮助

待办事项列表

  • 添加语言
  • 添加PHPUnit测试