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\IspellParser; |
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
|
|
|
public function check(string $text, array $languages = [], array $context = []): iterable |
27
|
|
|
{ |
28
|
|
|
Assert::maxCount($languages, 1, 'Aspell spellchecker doesn\'t support multiple languages check'); |
29
|
3 |
|
|
30
|
|
|
$cmd = $this->binaryPath->addArgs(['--encoding', 'utf-8']); |
31
|
3 |
|
$cmd = $cmd->addArg('-a'); |
32
|
|
|
|
33
|
3 |
|
if (!empty($languages)) { |
34
|
|
|
$cmd = $cmd->addArg('--lang=' . implode(',', $languages)); |
35
|
3 |
|
} |
36
|
2 |
|
|
37
|
|
|
$process = new Process($cmd->getArgs()); |
38
|
|
|
// Add prefix characters putting Ispell's type of spellcheckers in terse-mode, |
39
|
3 |
|
// ignoring correct words and thus speeding up the execution |
40
|
2 |
|
$process->setInput('!' . PHP_EOL . IspellParser::adaptInputForTerseModeProcessing($text) . PHP_EOL . '%'); |
41
|
|
|
|
42
|
|
|
$output = ProcessRunner::run($process)->getOutput(); |
43
|
3 |
|
|
44
|
|
|
if ($process->getErrorOutput() !== '') { |
45
|
|
|
throw new ProcessHasErrorOutputException($process->getErrorOutput(), $text, $process->getCommandLine()); |
46
|
3 |
|
} |
47
|
|
|
|
48
|
3 |
|
return IspellParser::parseMisspellingsFromOutput($output, $context); |
49
|
|
|
} |
50
|
3 |
|
|
51
|
1 |
|
public function getBinaryPath(): CommandLine |
52
|
|
|
{ |
53
|
|
|
return $this->binaryPath; |
54
|
2 |
|
} |
55
|
|
|
|
56
|
|
|
public function getSupportedLanguages(): iterable |
57
|
|
|
{ |
58
|
|
|
$languages = []; |
59
|
|
|
$cmd = $this->binaryPath->addArgs(['dump', 'dicts']); |
60
|
|
|
$process = new Process($cmd->getArgs()); |
61
|
|
|
$output = explode(PHP_EOL, ProcessRunner::run($process)->getOutput()); |
62
|
|
|
|
63
|
|
|
foreach ($output as $line) { |
64
|
|
|
$name = trim($line); |
65
|
2 |
|
if (strpos($name, '-variant') !== false || $name === '') { |
66
|
|
|
// Skip variants |
67
|
2 |
|
continue; |
68
|
2 |
|
} |
69
|
2 |
|
$languages[$name] = true; |
70
|
|
|
} |
71
|
2 |
|
$languages = array_keys($languages); |
72
|
2 |
|
\Safe\sort($languages); |
|
|
|
|
73
|
2 |
|
|
74
|
|
|
return $languages; |
75
|
2 |
|
} |
76
|
|
|
|
77
|
2 |
|
public static function create(?string $binaryPathAsString = null): self |
78
|
|
|
{ |
79
|
2 |
|
return new self(new CommandLine($binaryPathAsString ?? 'aspell')); |
80
|
2 |
|
} |
81
|
|
|
} |
82
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.