定制 mekras/php-speller 二次开发

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

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

mekras/php-speller

Composer 安装命令:

composer require mekras/php-speller

包简介

PHP spell check library

README 文档

README

PHP spell check library.

Latest Stable Version License Build Pipeline

Currently supported backends:

Installation

With Composer:

$ composer require mekras/php-speller

Usage

  1. Create a text source object from string, file or something else using one of the Mekras\Speller\Source\Source implementations (see Sources below).
  2. Create some speller instance (Hunspell, Ispell or any other implementation of the Mekras\Speller\Speller).
  3. Execute Speller::checkText() method.
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

You can list languages supported by backend:

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

See examples for more info.

Source encoding

For aspell, hunspell and ispell source text encoding should be equal to dictionary encoding. You can use IconvSource to convert source.

Aspell

This backend uses aspell program, so it should be installed in the system.

use Mekras\Speller\Aspell\Aspell;

$speller = new Aspell();

Path to binary can be set in constructor:

use Mekras\Speller\Aspell\Aspell;

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

Custom Dictionary

You can use a custom dictionary for aspell. The dictionary needs to be in the following format:

personal_ws-1.1 [lang] [words]

Where [lang] shout be the shorthand for the language you are using (e.g. en) and [words] is the count of words inside the dictionary. Beware that there should no spaces at the end of words. Each word should be listed in a new line.

$aspell = new Aspell();
$aspell->setPersonalDictionary(new Dictionary('/path/to/custom.pws'));

Important

  • aspell allow to specify only one language at once, so only first item taken from $languages argument in Ispell::checkText().

Hunspell

This backend uses hunspell program, so it should be installed in the system.

use Mekras\Speller\Hunspell\Hunspell;

$speller = new Hunspell();

Path to binary can be set in constructor:

use Mekras\Speller\Hunspell\Hunspell;

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

You can set additional dictionary path:

use Mekras\Speller\Hunspell\Hunspell;

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

You can specify custom dictionaries to use:

use Mekras\Speller\Hunspell\Hunspell;

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

Ispell

This backend uses ispell program, so it should be installed in the system.

use Mekras\Speller\Ispell\Ispell;

$speller = new Ispell();

Path to binary can be set in constructor:

use Mekras\Speller\Ispell\Ispell;

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

Important

  • ispell allow to use only one dictionary at once, so only first item taken from $languages argument in Ispell::checkText().

Sources

Sources — is an abstraction layer allowing spellers receive text from different sources like strings or files.

FileSource

Reads text from file.

use Mekras\Speller\Source\FileSource;

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

You can specify file encoding:

use Mekras\Speller\Source\FileSource;

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

StringSource

Use string as text source.

use Mekras\Speller\Source\StringSource;

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

Meta sources

Additionally there is a set of meta sources, which wraps other sources to perform extra tasks.

HtmlSource

Return user visible text from HTML.

use Mekras\Speller\Source\HtmlSource;

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

Encoding detected via DOMDocument::$encoding.

IconvSource

This is a meta-source which converts encoding of other given source:

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

Loads text from XLIFF files.

use Mekras\Speller\Source\XliffSource;

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

Source filters

Filters used internally to filter out all non text contents received from source. In order to save original word location (line and column numbers) all filters replaces non text content with spaces.

Available filters:

mekras/php-speller 适用场景与选型建议

mekras/php-speller 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 413.71k 次下载、GitHub Stars 达 68, 最近一次更新时间为 2015 年 05 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 413.71k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 68
  • 点击次数: 30
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 68
  • Watchers: 4
  • Forks: 24
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-08