chibifr/language-detector

一个PHP包,可以从字符串中检测语言,并可以学习新语言。

1.0.0 2015-12-08 12:28 UTC

This package is not auto-updated.

Last update: 2024-09-18 17:55:58 UTC


README

一个PHP包,可以从字符串中检测语言,并可以学习新语言。

安装

要安装此包,请确保您已安装 composer。然后,要求它

composer require chibifr/language-detector

教授新语言

要向语言检测器教授新语言,只需将一个文件添加到 src/languages 目录中,命名为 "your-newly-added-language.php"。此文件应返回您要教授语言检测器的单词或字符数组。

此文件应如下所示

<?php
// Your file should simple return an array of words/characters.
return [
    'word1',
    'word2',
    'word3',
    'word4',
    'etc'
];

我应该添加单词还是字符?

这是个好问题,兄弟/姐妹!以下是答案:如果你的语言不使用拉丁字母,请添加字符,如果它使用,则添加单词。

添加的单词越多,语言检测器的效率就越高(但不要添加太多,这可能会减慢它的速度)。

用法

<?php
// Require the composer's vendor autoload file
require './vendor/autoload.php';

use ChibiFR\LanguageDetector\Detector;

// Create a new Converter object
$lg = new Detector();

// Trying to detect English
$result = $lg->detectLanguage('Hello, my name is FooBar and I live in New York. The weather here is pretty
nice! Anyways, have a good day, people.');

print_r($result) // Will print ['language' => 'english', 'reliable' => 1]

// Trying to detect an unkown language
$result = $lg->detectLanguage('A e i o u.');
print_r($result) // Will print ['language' => 'english', 'reliable' => 0]
// You can then check if the language found is reliable before doing more.