tigitz/php-spellchecker

通过PHP直接提供一种简单的方法,使用多种拼写检查器对多个文本源进行拼写检查

0.7.0 2024-05-04 15:20 UTC

README

PHP-Spellchecker

Build Status Code coverage Code coverage PHP-Spellchecker chat room License

使用最流行的PHP拼写检查器,从任何文本源中检查拼写错误。

关于

PHP-Spellchecker是一个用于PHP的拼写检查器抽象库。通过提供许多不同拼写检查器的统一接口,您可以无需大量重写即可交换拼写检查器。

使用PHP-Spellchecker可以消除供应商锁定、减少技术债务并提高代码的可测试性。

功能

PHP-Spellchecker是一个欢迎新贡献者的项目。

想要做出您的第一个开源贡献吗?查看路线图,选择一个任务,打开一个问题,我们将帮助您完成它 🤓🚀

安装

通过Composer

$ composer require tigitz/php-spellchecker

使用方法

查看文档示例

直接使用拼写检查器

您可以直接从PhpSpellcheck\Spellchecker类检查拼写错误,并自行处理它们。

<?php
// if you made the default aspell installation on your local machine
$aspell = Aspell::create();

// or if you want to use binaries from Docker
$aspell = new Aspell(new CommandLine(['docker','run','--rm', '-i', 'starefossen/aspell']));

$misspellings = $aspell->check('mispell', ['en_US'], ['from_example']);
foreach ($misspellings as $misspelling) {
    $misspelling->getWord(); // 'mispell'
    $misspelling->getLineNumber(); // '1'
    $misspelling->getOffset(); // '0'
    $misspelling->getSuggestions(); // ['misspell', ...]
    $misspelling->getContext(); // ['from_example']
}

使用MisspellingFinder协调器

您还可以使用一个有偏见的MisspellingFinder类来协调您的拼写检查流程

PHP-Spellchecker-misspellingfinder-flow

遵循著名的 Unix 哲学

编写只做一件事且做得好的程序。编写可以协作工作的程序。编写可以处理文本流的程序,因为这是一种通用的接口。

<?php
// My custom text processor that replaces "_" by " "
$customTextProcessor = new class implements TextProcessorInterface
{
    public function process(TextInterface $text): TextInterface
    {
        $contentProcessed = str_replace('_', ' ', $text->getContent());

        return $text->replaceContent($contentProcessed);
    }
};

$misspellingFinder = new MisspellingFinder(
    Aspell::create(), // Creates aspell spellchecker pointing to "aspell" as it's binary path
    new EchoHandler(), // Handles all the misspellings found by echoing their information
    $customTextProcessor
);

// using a string
$misspellingFinder->find('It\'s_a_mispelling', ['en_US']);
// word: mispelling | line: 1 | offset: 7 | suggestions: mi spelling,mi-spelling,misspelling | context: []

// using a TextSource
$inMemoryTextProvider = new class implements SourceInterface
{
    public function toTexts(array $context): iterable
    {
        yield new Text('my_mispell', ['from_source_interface']);
        // t() is a shortcut for new Text()
        yield t('my_other_mispell', ['from_named_constructor']);
    }
};

$misspellingFinder->find($inMemoryTextProvider, ['en_US']);
//word: mispell | line: 1 | offset: 3 | suggestions: mi spell,mi-spell,misspell,... | context: ["from_source_interface"]
//word: mispell | line: 1 | offset: 9 | suggestions: mi spell,mi-spell,misspell,... | context: ["from_named_constructor"]

路线图

该项目仍处于初期阶段,需要更多实际使用来稳定其最终 1.0.0 API。

全局

  • 添加一个 CLI,可以执行类似 vendor/bin/php-spellchecker "misspell" Languagetools EchoHandler --lang=en_US 的操作。
  • 为拼写检查器添加异步机制。
  • 将一些计算的拼写错误属性设置为可选,以提高特定用例(例如,LanguageTools 中的行和偏移)的性能。
  • 添加一个语言映射器来管理拼写检查器之间的不同表示形式。
  • 为了性能,评估 strtok 而不是 explode 来解析文本行。
  • 评估 MutableMisspelling 以进行性能比较。
  • 封装 Webmozart/Assert 库异常,以抛出 PHP-Spellchecker 自定义异常。
  • 改进 Makefile

源代码

  • 创建一个 SourceInterface 类,使其能够影响所使用的拼写检查器配置。
  • League/Flysystem 源代码。
  • Symfony/Finder 源代码。

文本处理器

  • Markdown - 找到一个方法,在去除后保持单词的原始偏移和行。
  • 添加 PHPDoc 处理器。
  • 添加 HTML 处理器(灵感来源:HtmlFilter)。
  • 添加 XLIFF 处理器(灵感来源:XliffSource)。

拼写检查器

  • 缓存已拼写检查单词的建议(PSR-6/PSR-16?)。
  • Pspell - 找到计算单词偏移的方法。
  • LanguageTools - 评估 HTTPlug 库 来进行 API 请求。
  • Pspell - 找到列出可用字典的方法。
  • 添加 JamSpell 拼写检查器。
  • 添加 NuSpell 拼写检查器。
  • 添加 SymSpell 拼写检查器。
  • 添加 Yandex.Speller API 拼写检查器。
  • 添加 Bing Spell Check API 拼写检查器。

处理器

  • MonologHandler
  • ChainedHandler
  • HTMLReportHandler
  • XmlReportHandler
  • JSONReportHandler
  • ConsoleTableHandler

测试

  • 添加或改进具有不同文本编码的测试。
  • 重构 PHP 镜像之间的重复 Dockerfile 内容。

版本控制

我们遵循 SemVer v2.0.0

在考虑 v1.0.0 稳定发布之前,仍有许多设计决策应该在实际使用中得到检验。

  • TextInterfaceMisspellingInterface 真的有用吗?
  • 使用生成器是否是正确的方向?
  • 是否应该由包本身维护所有贡献的拼写检查器?
  • 如何设计一个直观的 CLI,同时考虑到使用所需的灵活性?
  • 通过所有层传递的 "上下文" 数组是处理数据共享的正确设计吗?

测试

拼写检查器以多种形式存在,从 HTTP API 到命令行工具。 PHP-Spellchecker 想要确保实际使用是可行的,因此它包含集成测试。要运行这些测试,在测试执行期间需要所有拼写检查器都可用。

最方便的方法是使用 Docker 并避免污染您的本地机器。

Docker

需要安装 dockerdocker-compose(已在 Linux 上测试)。

$ make build # build container images
$ make setup # start spellcheckers container
$ make tests-dox

您还可以指定 PHP 版本、依赖项版本目标以及是否需要覆盖率。

$ PHP_VERSION=8.2 DEPS=LOWEST WITH_COVERAGE="true" make tests-dox

运行 make help 以列出所有可用的任务。

环境变量

如果拼写检查器的执行路径与其默认值不同(例如,使用 docker exec -ti myispell 而不是 ispell),您可以通过在 PHPUnit 配置文件 中重新定义环境变量来覆盖测试中使用的路径。

贡献

请参阅 CONTRIBUTING

鸣谢

许可

MIT 许可证(MIT)。有关更多信息,请参阅 许可文件

标志:最终渲染所采用的元素由 rawpixel.com / Freepik 设计