tigitz /
php-spellchecker
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | use PhpSpellcheck\MisspellingFinder; |
||
| 6 | use PhpSpellcheck\MisspellingHandler\EchoHandler; |
||
| 7 | use PhpSpellcheck\Source\SourceInterface; |
||
| 8 | use PhpSpellcheck\Spellchecker\Aspell; |
||
| 9 | use PhpSpellcheck\TextInterface; |
||
| 10 | use PhpSpellcheck\TextProcessor\TextProcessorInterface; |
||
| 11 | |||
| 12 | use function PhpSpellcheck\t; |
||
| 13 | |||
| 14 | require_once __DIR__ . '/../vendor/autoload.php'; |
||
| 15 | |||
| 16 | // My custom text processor that replaces "_" by " " |
||
| 17 | $customTextProcessor = new class () implements TextProcessorInterface { |
||
| 18 | public function process(TextInterface $text): TextInterface |
||
| 19 | { |
||
| 20 | $contentProcessed = str_replace('_', ' ', $text->getContent()); |
||
| 21 | |||
| 22 | return $text->replaceContent($contentProcessed); |
||
| 23 | } |
||
| 24 | }; |
||
| 25 | |||
| 26 | $misspellingFinder = new MisspellingFinder( |
||
| 27 | Aspell::create(), // Creates aspell spellchecker pointing to "aspell" as it's binary path |
||
| 28 | new EchoHandler(), // Handles all the misspellings found by echoing their information |
||
| 29 | $customTextProcessor |
||
| 30 | ); |
||
| 31 | |||
| 32 | // Using a custom SourceInterface that generates Text |
||
| 33 | $inMemoryTextProvider = new class () implements SourceInterface { |
||
| 34 | public function toTexts(array $context): iterable |
||
| 35 | { |
||
| 36 | yield t('my_mispell', ['from_source_interface']); |
||
| 37 | yield t('my_other_mispell', ['from_named_constructor']); |
||
| 38 | } |
||
| 39 | }; |
||
| 40 | |||
| 41 | $misspellingFinder->find($inMemoryTextProvider, ['en_US']); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 42 | //word: mispell | line: 1 | offset: 3 | suggestions: mi spell,mi-spell,misspell,... | context: ["from_source_interface"] |
||
| 43 | //word: mispell | line: 1 | offset: 9 | suggestions: mi spell,mi-spell,misspell,... | context: ["from_named_constructor"] |
||
| 44 |