|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use PhpSpellCheck\MisspellingFinder; |
|
4
|
|
|
use PhpSpellCheck\MisspellingHandler\EchoHandler; |
|
5
|
|
|
use PhpSpellCheck\SpellChecker\Aspell; |
|
6
|
|
|
use PhpSpellCheck\TextInterface; |
|
7
|
|
|
use PhpSpellCheck\TextProcessor\TextProcessorInterface; |
|
8
|
|
|
|
|
9
|
|
|
require_once __DIR__ . '/../vendor/autoload.php'; |
|
10
|
|
|
|
|
11
|
|
|
// *** Using spellcheckers directly *** |
|
12
|
|
|
$aspell = Aspell::create(); // Creates aspell spellchecker pointing to "aspell" as it's binary path |
|
13
|
|
|
/** @var \PhpSpellCheck\Misspelling[] $misspellings */ |
|
14
|
|
|
$misspellings = $aspell->check('mispell', ['en_US']); // $misspellings is a \Generator here |
|
15
|
|
|
foreach ($misspellings as $misspelling) { |
|
16
|
|
|
$misspelling->getWord(); |
|
17
|
|
|
$misspelling->getLineNumber(); |
|
18
|
|
|
$misspelling->getOffset(); |
|
19
|
|
|
$misspelling->getSuggestions(); |
|
20
|
|
|
$misspelling->getContext(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
// *** Using MisspellingFinder class to orchestrate spellchecking flow *** |
|
24
|
|
|
// My custom text processor that replaces "_" by " " |
|
25
|
|
|
$customTextProcessor = new class implements TextProcessorInterface |
|
26
|
|
|
{ |
|
27
|
|
|
public function process(TextInterface $text): TextInterface |
|
28
|
|
|
{ |
|
29
|
|
|
$contentProcessed = str_replace('_', ' ', $text->getContent()); |
|
30
|
|
|
|
|
31
|
|
|
return $text->replaceContent($contentProcessed); |
|
32
|
|
|
} |
|
33
|
|
|
}; |
|
34
|
|
|
|
|
35
|
|
|
$misspellingFinder = new MisspellingFinder( |
|
36
|
|
|
Aspell::create(), // Creates aspell spellchecker pointing to "aspell" as it's binary path |
|
37
|
|
|
new EchoHandler(), // Handles all the misspellings found by echoing their information |
|
38
|
|
|
$customTextProcessor |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
|
|
$misspellingFinder->find('It\'s_a_mispelling', ['en_US']); // Misspellings are echoed |
|
42
|
|
|
|