定制 tigitz/php-spellchecker 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tigitz/php-spellchecker

Composer 安装命令:

composer require tigitz/php-spellchecker

包简介

Provides an easy way to spellcheck multiple text source by many spellcheckers, directly from PHP

README 文档

README

PHP-Spellchecker

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

Check misspellings from any text source with the most popular PHP spellchecker.

About

PHP-Spellchecker is a spellchecker abstraction library for PHP. By providing a unified interface for many different spellcheckers, you’re able to swap out spellcheckers without extensive rewrites.

Using PHP-Spellchecker can eliminate vendor lock-in, reduce technical debt, and improve the testability of your code.

Features

PHP-Spellchecker is a welcoming project for new contributors.

Want to make your first open source contribution? Check the roadmap, pick one task, open an issue and we'll help you go through it 🤓🚀

Install

Via Composer

composer require tigitz/php-spellchecker

Usage

Check out the documentation and examples

Using the spellchecker directly

You can check misspellings directly from a PhpSpellcheck\Spellchecker class and process them on your own.

<?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']
}

Using the MisspellingFinder orchestrator

You can also use an opinionated MisspellingFinder class to orchestrate your spellchecking flow:

PHP-Spellchecker-misspellingfinder-flow

Following the well-known Unix philosophy:

Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.

<?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"]

Roadmap

The project is still in its initial phase, requiring more real-life usage to stabilize its final 1.0.0 API.

Global

  • Add a CLI that could do something like vendor/bin/php-spellchecker "misspell" Languagetools EchoHandler --lang=en_US
  • Add asynchronous mechanism to spellcheckers.
  • Make some computed misspelling properties optional to improve performance for certain use cases (e.g., lines and offset in LanguageTools).
  • Add a language mapper to manage different representations across spellcheckers.
  • Evaluate strtok instead of explode to parse lines of text, for performance.
  • Evaluate MutableMisspelling for performance comparison.
  • Wrap Webmozart/Assert library exceptions to throw PHP-Spellchecker custom exceptions instead.
  • Improve the Makefile.

Sources

  • Make a SourceInterface class that's able to have an effect on the used spellchecker configuration.
  • League/Flysystem source.
  • Symfony/Finder source.

Text processors

  • Markdown - Find a way to keep the original offset and line of words after stripping.
  • Add PHPDoc processor.
  • Add HTML Processor (inspiration).
  • Add XLIFF Processor (inspiration).

Spell checkers

Handlers

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

Tests

  • Add or improve tests with different text encoding.
  • Refactor duplicate Dockerfile content between PHP images.

Versioning

We follow SemVer v2.0.0.

There still are many design decisions that should be confronted with real-world usage before thinking about a v1.0.0 stable release:

  • Are TextInterface and MisspellingInterface really useful?
  • Is using generators the right way to go?
  • Should all the contributed spellcheckers be maintained by the package itself?
  • How to design an intuitive CLI given the needed flexibility of usage?
  • Is the "context" array passed through all the layers the right design to handle data sharing?

Testing

Spell checkers come in many different forms, from HTTP API to command line tools. PHP-Spellchecker wants to ensure real-world usage is OK, so it contains integration tests. To run these, spellcheckers need to all be available during tests execution.

The most convenient way to do it is by using Docker and avoid polluting your local machine.

Docker

Requires docker and docker-compose to be installed (tested on Linux).

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

You can also specify PHP version, dependency version target and if you want coverage.

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

Run make help to list all available tasks.

Environment variables

If spellcheckers execution paths are different than their default values (e.g., docker exec -ti myispell instead of ispell) you can override the path used in tests by redefining environment variables in the PHPUnit config file.

Contributing

Please see CONTRIBUTING.

Credits

License

The MIT License (MIT). Please see license file for more information.

Logo: Elements taken for the final rendering are Designed by rawpixel.com / Freepik.

tigitz/php-spellchecker 适用场景与选型建议

tigitz/php-spellchecker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 542.07k 次下载、GitHub Stars 达 310, 最近一次更新时间为 2019 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「spellcheck」 「spellchecker」 「spelling」 「spell-checker」 「spell-check」 「php-spellchecker」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tigitz/php-spellchecker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tigitz/php-spellchecker 我们能提供哪些服务?
定制开发 / 二次开发

基于 tigitz/php-spellchecker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 542.07k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 310
  • 点击次数: 25
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 310
  • Watchers: 8
  • Forks: 30
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-04-14