1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require __DIR__.'/../vendor/autoload.php'; |
4
|
|
|
|
5
|
|
|
use PhpSpellcheck\Misspelling; |
6
|
|
|
use PhpSpellcheck\Spellchecker\SpellcheckerInterface; |
7
|
|
|
use PhpSpellcheck\Utils\LineAndOffset; |
8
|
|
|
|
9
|
|
|
$phpSpellcheckLibraryNameSpellchecker = new class implements SpellcheckerInterface { |
10
|
|
|
public function check( |
11
|
|
|
string $text, |
12
|
|
|
array $languages = [], |
13
|
|
|
array $context = [], |
14
|
|
|
?string $encoding = \PhpSpellcheck\Utils\TextEncoding::UTF8 |
15
|
|
|
): iterable { |
16
|
|
|
foreach (['php-spellchecker', 'php-spellcheckerer', 'php spellchecker'] as $misspelledCandidate) { |
17
|
|
|
$matches = []; |
18
|
|
|
|
19
|
|
|
if (preg_match('/\b'.$misspelledCandidate.'\b/i', $text, $matches, PREG_OFFSET_CAPTURE) !== false) { |
20
|
|
|
foreach ($matches as $match) { |
21
|
|
|
[$word, $offset] = $match; |
22
|
|
|
[$line, $offsetFromLine] = LineAndOffset::findFromFirstCharacterOffset($text, $offset, $encoding); |
|
|
|
|
23
|
|
|
|
24
|
|
|
yield new Misspelling( |
25
|
|
|
$word, |
26
|
|
|
$offsetFromLine, |
27
|
|
|
$line, |
28
|
|
|
['PHP Spellcheck'] |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getSupportedLanguages(): iterable |
36
|
|
|
{ |
37
|
|
|
yield 'en_US'; |
38
|
|
|
} |
39
|
|
|
}; |
40
|
|
|
|
41
|
|
|
/** @var Misspelling[]|\Generator $misspellings */ |
42
|
|
|
$misspellings = $phpSpellcheckLibraryNameSpellchecker->check('The PHP-Spellcheckerer library', ['en_US']); |
43
|
|
|
foreach ($misspellings as $misspelling) { |
44
|
|
|
print_r([ |
45
|
|
|
$misspelling->getWord(), // 'PHP-Spellcheckerer' |
46
|
|
|
$misspelling->getLineNumber(), // '...' |
47
|
|
|
$misspelling->getOffset(), // '...' |
48
|
|
|
$misspelling->getSuggestions(), // ['PHP Spellcheck'] |
49
|
|
|
$misspelling->getContext(), // [] |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
|