repat/mekras-php-speller

PHP拼写检查库针对PHP7更新

1.7.3 2018-09-11 21:21 UTC

This package is auto-updated.

Last update: 2024-09-20 08:40:33 UTC


README

PHP拼写检查库。

Latest Stable Version License Build Status Coverage Status

当前支持的后端

安装

使用 Composer

$ composer require mekras/php-speller

使用方法

  1. 使用 Mekras\Speller\Source\Source 实现之一从字符串、文件或其他内容创建一个文本源对象(见下文 来源)。
  2. 创建一些拼写检查实例(Hunspell、Ispell 或其他 Mekras\Speller\Speller 的实现)。
  3. 执行 Speller::checkText() 方法。
use Mekras\Speller\Hunspell\Hunspell;
use Mekras\Speller\Source\StringSource;

$source = new StringSource('Tiger, tigr, burning bright');
$speller = new Hunspell();
$issues = $speller->checkText($source, ['en_GB', 'en']);

echo $issues[0]->word; // -> "tigr"
echo $issues[0]->line; // -> 1
echo $issues[0]->offset; // -> 7
echo implode(',', $issues[0]->suggestions); // -> tiger, trig, tier, tigris, tigress

您可以列出后端支持的语言

/** @var Mekras\Speller\Speller $speller */
print_r($speller->getSupportedLanguages());

更多信息请参阅 示例

源编码

对于 aspell、hunspell 和 ispell,源文本编码应与字典编码相同。您可以使用 IconvSource 转换源。

Aspell

此后端使用 aspell 程序,因此它应在系统中安装。

use Mekras\Speller\Aspell\Aspell;

$speller = new Aspell();

可以在构造函数中设置二进制文件的路径

use Mekras\Speller\Aspell\Aspell;

$speller = new Aspell('/usr/local/bin/aspell');

重要

  • aspell 允许一次指定一种语言,因此 Ispell::checkText() 中的 $languages 参数的第一个项被采用。

Hunspell

此后端使用 hunspell 程序,因此它应在系统中安装。

use Mekras\Speller\Hunspell\Hunspell;

$speller = new Hunspell();

可以在构造函数中设置二进制文件的路径

use Mekras\Speller\Hunspell\Hunspell;

$speller = new Hunspell('/usr/local/bin/hunspell');

您可以设置额外的字典路径

use Mekras\Speller\Hunspell\Hunspell;

$speller = new Hunspell();
$speller->setDictionaryPath('/var/spelling/custom');

您可以指定要使用的自定义字典

use Mekras\Speller\Hunspell\Hunspell;

$speller = new Hunspell();
$speller->setDictionaryPath('/my_app/spelling');
$speller->setCustomDictionaries(['tech', 'titles']);

Ispell

此后端使用 ispell 程序,因此它应在系统中安装。

use Mekras\Speller\Ispell\Ispell;

$speller = new Ispell();

可以在构造函数中设置二进制文件的路径

use Mekras\Speller\Ispell\Ispell;

$speller = new Ispell('/usr/local/bin/ispell');

重要

  • ispell 允许一次使用一个字典,因此 Ispell::checkText() 中的 $languages 参数的第一个项被采用。

来源

来源 - 是一个抽象层,允许拼写检查器从字符串或文件等不同来源接收文本。

FileSource

从文件中读取文本。

use Mekras\Speller\Source\FileSource;

$source = new FileSource('/path/to/file.txt');

您可以指定文件编码

use Mekras\Speller\Source\FileSource;

$source = new FileSource('/path/to/file.txt', 'windows-1251');

StringSource

使用字符串作为文本源。

use Mekras\Speller\Source\StringSource;

$source = new StringSource('foo', 'koi8-r');

元来源

此外,还有一系列元来源,它们将其他来源包装起来以执行额外任务。

HtmlSource

从 HTML 返回用户可见的文本。

use Mekras\Speller\Source\HtmlSource;

$source = new HtmlSource(
    new StringSource('<a href="#" title="Foo">Bar</a> Baz')
);
echo $source->getAsString(); // Foo Bar Baz

通过 DOMDocument::$encoding 检测编码。

IconvSource

这是一个元来源,它转换其他给定来源的编码

use Mekras\Speller\Source\IconvSource;
use Mekras\Speller\Source\StringSource;

// Convert file contents from windows-1251 to koi8-r.
$source = new IconvSource(
    new FileSource('/path/to/file.txt', 'windows-1251'),
    'koi8-r'
);

XliffSource

XLIFF 文件中加载文本。

use Mekras\Speller\Source\XliffSource;

$source = new XliffSource(__DIR__ . '/fixtures/test.xliff');

来源过滤器

内部使用的过滤器用于从来源中过滤掉所有非文本内容。为了保存原始单词位置(行和列号),所有过滤器都将非文本内容替换为空格。

可用过滤器