1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpSpellcheck\Spellchecker; |
6
|
|
|
|
7
|
|
|
use PhpSpellcheck\MisspellingInterface; |
8
|
|
|
use Webmozart\Assert\Assert; |
9
|
|
|
|
10
|
|
|
class MultiSpellchecker implements SpellcheckerInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var iterable<SpellcheckerInterface> |
14
|
|
|
*/ |
15
|
|
|
private $spellCheckers; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
private $mergeMisspellingsSuggestions; |
21
|
|
|
|
22
|
3 |
|
|
23
|
|
|
/** |
24
|
3 |
|
* @param SpellcheckerInterface[] $spellCheckers |
25
|
3 |
|
*/ |
26
|
3 |
|
public function __construct(iterable $spellCheckers, bool $mergeMisspellingsSuggestions = true) |
27
|
3 |
|
{ |
28
|
|
|
Assert::allIsInstanceOf($spellCheckers, SpellcheckerInterface::class); |
29
|
|
|
$this->spellCheckers = $spellCheckers; |
30
|
|
|
$this->mergeMisspellingsSuggestions = $mergeMisspellingsSuggestions; |
31
|
|
|
} |
32
|
2 |
|
|
33
|
|
|
/** |
34
|
2 |
|
* {@inheritdoc} |
35
|
1 |
|
*/ |
36
|
|
|
public function check(string $text, array $languages = [], array $context = [], ?string $encoding = null): iterable |
37
|
|
|
{ |
38
|
|
|
if (!$this->mergeMisspellingsSuggestions) { |
39
|
1 |
|
return $this->checkForAllSpellcheckers($text, $languages, $context, $encoding); |
40
|
1 |
|
} |
41
|
1 |
|
|
42
|
1 |
|
/** @var MisspellingInterface[] $misspellings */ |
43
|
1 |
|
$misspellings = []; |
44
|
|
|
/** @var SpellcheckerInterface $spellChecker */ |
45
|
|
|
foreach ($this->spellCheckers as $spellChecker) { |
46
|
1 |
|
foreach ($spellChecker->check($text, $languages, $context, $encoding) as $misspelling) { |
47
|
1 |
|
if (!empty($context)) { |
48
|
1 |
|
$misspelling = $misspelling->mergeContext($context); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
if (!$misspelling->canDeterminateUniqueIdentity()) { |
52
|
1 |
|
$misspellings[] = $misspelling; |
53
|
1 |
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
if (isset($misspellings[$misspelling->getUniqueIdentity()])) { |
57
|
|
|
$misspellings[$misspelling->getUniqueIdentity()]->mergeSuggestions($misspelling->getSuggestions()); |
58
|
|
|
continue; |
59
|
|
|
} |
60
|
1 |
|
|
61
|
|
|
$misspellings[$misspelling->getUniqueIdentity()] = $misspelling; |
62
|
|
|
} |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
return array_values($misspellings); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function checkForAllSpellcheckers( |
69
|
|
|
string $text, |
70
|
1 |
|
array $languages, |
71
|
1 |
|
array $context, |
72
|
1 |
|
?string $encoding |
73
|
1 |
|
): iterable { |
74
|
|
|
foreach ($this->spellCheckers as $spellChecker) { |
75
|
|
|
foreach ($spellChecker->check($text, $languages, $context, $encoding) as $misspelling) { |
76
|
1 |
|
if (!empty($context)) { |
77
|
|
|
$misspelling = $misspelling->mergeContext($context); |
78
|
|
|
} |
79
|
1 |
|
|
80
|
|
|
yield $misspelling; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
1 |
|
|
85
|
|
|
/** |
86
|
1 |
|
* {@inheritdoc} |
87
|
1 |
|
*/ |
88
|
1 |
|
public function getSupportedLanguages(): iterable |
89
|
1 |
|
{ |
90
|
|
|
$supportedLanguages = []; |
91
|
|
|
foreach ($this->spellCheckers as $spellChecker) { |
92
|
|
|
foreach ($spellChecker->getSupportedLanguages() as $language) { |
93
|
1 |
|
$supportedLanguages[] = $language; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return array_values(array_unique($supportedLanguages)); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|