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