tigitz / php-spellchecker
通过PHP直接提供一种简单的方法,使用多种拼写检查器对多个文本源进行拼写检查
Requires
- php: ^8.1
- nyholm/psr7: ^1.3
- psr/http-client: ^1.0
- symfony/process: ^4.4.30 | ^5.0 |^6.0 | ^7.0
- thecodingmachine/safe: ^1.0 | ^2.0
- webmozart/assert: ^1.11
Requires (Dev)
- aptoma/twig-markdown: ^3.0
- cocur/slugify: ^3.2 || ^4.0
- erusev/parsedown: ^1.7
- erusev/parsedown-extra: ^0.8
- phpstan/phpstan: ^1.2.0
- phpstan/phpstan-phpunit: ^1.0.0
- phpstan/phpstan-strict-rules: ^1.1.0
- phpstan/phpstan-webmozart-assert: ^1.0.0
- phpunit/phpunit: ^9.5
- pixelrobin/php-feather: ^1.0
- symfony/filesystem: ^4.4 || ^5.0 || ^6.0
- symfony/finder: ^4.4 || ^5.0 || ^6.0
- symfony/http-client: ^5.0 || ^6.0
- thecodingmachine/phpstan-safe-rule: ^1.1
Suggests
- symfony/http-client: A PSR-18 Client implementation to use spellcheckers that relies on HTTP APIs
- dev-master / 1.0.x-dev
- 0.7.0
- 0.6.0
- 0.5.0
- 0.4.0
- 0.3.0
- 0.2.2
- 0.2.1
- v0.2
- 0.1.1
- 0.1
- dev-upgrade-to-php-8.2
- dev-aspell-pipemode-fix
- dev-renovate/thecodingmachine-phpstan-safe-rule-1.x
- dev-renovate/thecodingmachine-safe-2.x
- dev-renovate/major-symfony
- dev-fix-ci
- dev-dependabot/add-v2-config-file
- dev-symspell
- dev-benchmark
- dev-sf-string-experiment
This package is auto-updated.
Last update: 2024-09-04 16:19:09 UTC
README
使用最流行的PHP拼写检查器,从任何文本源中检查拼写错误。
关于
PHP-Spellchecker是一个用于PHP的拼写检查器抽象库。通过提供许多不同拼写检查器的统一接口,您可以无需大量重写即可交换拼写检查器。
使用PHP-Spellchecker可以消除供应商锁定、减少技术债务并提高代码的可测试性。
功能
- 🧐 支持许多流行的拼写检查器,包括:Aspell、Hunspell、Ispell、PHP Pspell、LanguageTools、JamSpell和MultiSpellchecker (添加您的!)
- 📄 支持不同的文本源:文件系统文件/目录、字符串和多源 (添加您的!)
- 🛠 支持文本处理器:MarkdownRemover (添加您的!)
- 🔁 支持拼写错误处理器:EchoHandler (添加您的!)
- ➰ 使用生成器以减少内存占用
- ⚖ 灵活直观的设计
- 💡 使实现自己的拼写检查器、文本处理器和拼写错误处理器变得轻松
- 💪 对真实拼写检查器进行测试,以确保完全兼容
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
类来协调您的拼写检查流程
遵循著名的 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 稳定发布之前,仍有许多设计决策应该在实际使用中得到检验。
TextInterface
和MisspellingInterface
真的有用吗?- 使用生成器是否是正确的方向?
- 是否应该由包本身维护所有贡献的拼写检查器?
- 如何设计一个直观的 CLI,同时考虑到使用所需的灵活性?
- 通过所有层传递的 "上下文" 数组是处理数据共享的正确设计吗?
测试
拼写检查器以多种形式存在,从 HTTP API 到命令行工具。 PHP-Spellchecker 想要确保实际使用是可行的,因此它包含集成测试。要运行这些测试,在测试执行期间需要所有拼写检查器都可用。
最方便的方法是使用 Docker 并避免污染您的本地机器。
Docker
需要安装 docker
和 docker-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 设计。