1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpSpellcheck\Spellchecker; |
6
|
|
|
|
7
|
|
|
use PhpSpellcheck\Exception\ProcessHasErrorOutputException; |
8
|
|
|
use PhpSpellcheck\Utils\CommandLine; |
9
|
|
|
use PhpSpellcheck\Utils\IspellOutputParser; |
10
|
|
|
use PhpSpellcheck\Utils\ProcessRunner; |
11
|
|
|
use Symfony\Component\Process\Process; |
12
|
|
|
use Webmozart\Assert\Assert; |
13
|
|
|
|
14
|
|
|
class Aspell implements SpellcheckerInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var CommandLine |
18
|
|
|
*/ |
19
|
|
|
private $binaryPath; |
20
|
|
|
|
21
|
5 |
|
public function __construct(CommandLine $binaryPath) |
22
|
|
|
{ |
23
|
5 |
|
$this->binaryPath = $binaryPath; |
24
|
5 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritDoc} |
28
|
|
|
*/ |
29
|
3 |
|
public function check(string $text, array $languages = [], array $context = [], ?string $encoding = null): iterable |
30
|
|
|
{ |
31
|
3 |
|
Assert::greaterThan($languages, 1, 'Aspell spellchecker doesn\'t support multiple languages check'); |
32
|
|
|
|
33
|
3 |
|
$cmd = $this->binaryPath->addArg('-a'); |
34
|
|
|
|
35
|
3 |
|
if (!empty($languages)) { |
36
|
2 |
|
$cmd = $cmd->addArg('--lang=' . implode(',', $languages)); |
37
|
|
|
} |
38
|
|
|
|
39
|
3 |
|
if ($encoding !== null) { |
40
|
2 |
|
$cmd = $cmd->addArg('--encoding=' . $encoding); |
41
|
|
|
} |
42
|
|
|
|
43
|
3 |
|
$process = new Process($cmd->getArgs()); |
44
|
|
|
// Add prefix characters putting Ispell's type of spellcheckers in terse-mode, |
45
|
|
|
// ignoring correct words and thus speeding up the execution |
46
|
3 |
|
$process->setInput('!' . PHP_EOL . $text . PHP_EOL . '%'); |
47
|
|
|
|
48
|
3 |
|
$output = ProcessRunner::run($process)->getOutput(); |
49
|
|
|
|
50
|
3 |
|
if ($process->getErrorOutput() !== '') { |
51
|
1 |
|
throw new ProcessHasErrorOutputException($process->getErrorOutput(), $text, $process->getCommandLine()); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
return IspellOutputParser::parseMisspellings($output, $context); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getBinaryPath(): CommandLine |
58
|
|
|
{ |
59
|
|
|
return $this->binaryPath; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
*/ |
65
|
2 |
|
public function getSupportedLanguages(): iterable |
66
|
|
|
{ |
67
|
2 |
|
$languages = []; |
68
|
2 |
|
$cmd = $this->binaryPath->addArgs(['dump', 'dicts']); |
69
|
2 |
|
$process = new Process($cmd->getArgs()); |
70
|
|
|
$output = explode(PHP_EOL, ProcessRunner::run($process)->getOutput()); |
71
|
2 |
|
|
72
|
2 |
|
foreach ($output as $line) { |
73
|
2 |
|
$name = trim($line); |
74
|
|
|
if (strpos($name, '-variant') !== false || $name === '') { |
75
|
2 |
|
// Skip variants |
76
|
|
|
continue; |
77
|
2 |
|
} |
78
|
|
|
$languages[$name] = true; |
79
|
2 |
|
} |
80
|
2 |
|
$languages = array_keys($languages); |
81
|
|
|
\Safe\sort($languages); |
82
|
2 |
|
|
83
|
|
|
return $languages; |
84
|
|
|
} |
85
|
1 |
|
|
86
|
|
|
public static function create(?string $binaryPathAsString = null): self |
87
|
1 |
|
{ |
88
|
|
|
return new self(new CommandLine($binaryPathAsString ?? 'aspell')); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|