1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpSpellcheck\Spellchecker; |
6
|
|
|
|
7
|
|
|
use PhpSpellcheck\Exception\LogicException; |
8
|
|
|
use PhpSpellcheck\Exception\RuntimeException; |
9
|
|
|
use PhpSpellcheck\Misspelling; |
10
|
|
|
use PhpSpellcheck\Utils\TextEncoding; |
11
|
|
|
use Webmozart\Assert\Assert; |
12
|
|
|
|
13
|
|
|
class PHPPspell implements SpellcheckerInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
private $mode; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $numberOfCharactersLowerLimit; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @link http://php.net/manual/en/function.pspell-config-mode.php |
27
|
|
|
* @link http://php.net/manual/en/function.pspell-config-ignore.php |
28
|
|
|
* |
29
|
|
|
* @param int $mode The mode parameter is the mode in which spellchecker will work. |
30
|
|
|
* @param int $numberOfCharactersLowerLimit Words less than n characters will be skipped |
31
|
|
|
*/ |
32
|
3 |
|
public function __construct(int $mode, int $numberOfCharactersLowerLimit = 0) |
33
|
|
|
{ |
34
|
3 |
|
if (!extension_loaded('pspell')) { |
35
|
|
|
throw new RuntimeException('Pspell extension must be loaded to use the PHPPspell spellchecker'); |
36
|
|
|
} |
37
|
|
|
|
38
|
3 |
|
Assert::greaterThanEq($numberOfCharactersLowerLimit, 0); |
39
|
|
|
|
40
|
3 |
|
$this->mode = $mode; |
41
|
3 |
|
$this->numberOfCharactersLowerLimit = $numberOfCharactersLowerLimit; |
42
|
3 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritDoc} |
46
|
|
|
*/ |
47
|
2 |
|
public function check( |
48
|
|
|
string $text, |
49
|
|
|
array $languages = [], |
50
|
|
|
array $context = [], |
51
|
|
|
?string $encoding = TextEncoding::UTF8 |
52
|
|
|
): iterable { |
53
|
2 |
|
Assert::count($languages, 1, 'PHPPspell spellchecker doesn\'t support multiple languages check'); |
54
|
2 |
|
Assert::notNull($encoding, 'PHPPspell requires the encoding to be defined'); |
55
|
|
|
|
56
|
1 |
|
$pspellConfig = \Safe\pspell_config_create(current($languages), '', '', $encoding); |
57
|
1 |
|
\Safe\pspell_config_mode($pspellConfig, $this->mode); |
58
|
1 |
|
\Safe\pspell_config_ignore($pspellConfig, $this->numberOfCharactersLowerLimit); |
59
|
1 |
|
$dictionary = \Safe\pspell_new_config($pspellConfig); |
60
|
|
|
|
61
|
1 |
|
$lines = explode(PHP_EOL, $text); |
62
|
1 |
|
|
63
|
1 |
|
/** @var string $line */ |
64
|
1 |
|
foreach ($lines as $lineNumber => $line) { |
65
|
1 |
|
$words = explode(' ', \Safe\preg_replace("/(?!['’-])(\p{P}|\+|--)/u", '', $line)); |
66
|
1 |
|
foreach ($words as $key => $word) { |
67
|
|
|
if (!pspell_check($dictionary, $word)) { |
68
|
|
|
$suggestions = pspell_suggest($dictionary, $word); |
69
|
|
|
yield new Misspelling($word, 0, $lineNumber + 1, $suggestions, $context); |
70
|
1 |
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
/** |
76
|
|
|
* {@inheritDoc} |
77
|
1 |
|
*/ |
78
|
|
|
public function getSupportedLanguages(): iterable |
79
|
|
|
{ |
80
|
|
|
throw new LogicException('Retrieving supported dictionaries for PHPPspell spellchecker is not supported yet'); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|