mekras / php-speller
PHP拼写检查库
2.3.0
2023-04-14 06:53 UTC
Requires
- php: >=7.4
- ext-dom: *
- ext-iconv: *
- ext-libxml: *
- phpspec/prophecy-phpunit: ^2.0
- symfony/process: ^5.4.22 || ^6.0
Requires (Dev)
- phpunit/phpunit: ^9.6.6
- squizlabs/php_codesniffer: ^3.7.2
- dev-master
- 2.3.0
- 2.2.0
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- v2.0
- 1.x-dev
- v1.7.2
- v1.7.1
- v1.7.0
- v1.6
- v1.5.1
- v1.5
- v1.4.1
- v1.4
- v1.03.1
- v1.03
- v1.02
- v1.01
- v1.00
- dev-dependabot/github_actions/shivammathur/setup-php-2.27.1
- dev-dependabot/composer/phpunit/phpunit-tw-9.6.13
- dev-dependabot/github_actions/actions/checkout-4
- dev-fix/checktext-twice
- dev-fix/symfony-process
This package is auto-updated.
Last update: 2024-09-04 15:38:46 UTC
README
PHP拼写检查库。
当前支持的后端
安装
使用 Composer
$ composer require mekras/php-speller
用法
- 使用
Mekras\Speller\Source\Source
的实现之一(见下文 来源)从字符串、文件或其他内容创建一个文本源对象。 - 创建一些拼写检查实例(Hunspell、Ispell 或其他
Mekras\Speller\Speller
的实现)。 - 执行
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 使用自定义词典。词典需要以下格式
personal_ws-1.1 [lang] [words]
其中 [lang]
应为所使用语言的简称(例如 en
)和 [words]
是词典中单词的数量。 请注意,单词末尾不应有空格。每个单词应单独一行。
$aspell = new Aspell(); $aspell->setPersonalDictionary(new Dictionary('/path/to/custom.pws'));
重要
- 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');
您可以为 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');
源过滤器
用于内部过滤来自来源的所有非文本内容。为了保存原始单词位置(行和列号),所有过滤器都将非文本内容替换为空格。
可用过滤器
- StripAllFilter - 删除所有输入文本;
- HtmlFilter — 移除HTML标签。