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
|
|
|
* @param SpellcheckerInterface[] $spellCheckers |
24
|
3 |
|
*/ |
25
|
3 |
|
public function __construct(iterable $spellCheckers, bool $mergeMisspellingsSuggestions = true) |
26
|
3 |
|
{ |
27
|
3 |
|
Assert::allIsInstanceOf($spellCheckers, SpellcheckerInterface::class); |
28
|
|
|
$this->spellCheckers = $spellCheckers; |
29
|
|
|
$this->mergeMisspellingsSuggestions = $mergeMisspellingsSuggestions; |
30
|
|
|
} |
31
|
|
|
|
32
|
2 |
|
public function check(string $text, array $languages, array $context): iterable |
33
|
|
|
{ |
34
|
2 |
|
if (!$this->mergeMisspellingsSuggestions) { |
35
|
1 |
|
yield from $this->checkForAllSpellcheckers($text, $languages, $context); |
36
|
|
|
|
37
|
|
|
return; |
38
|
|
|
} |
39
|
1 |
|
|
40
|
1 |
|
/** @var MisspellingInterface[] $misspellings */ |
41
|
1 |
|
$misspellings = []; |
42
|
1 |
|
/** @var SpellcheckerInterface $spellChecker */ |
43
|
1 |
|
foreach ($this->spellCheckers as $spellChecker) { |
44
|
|
|
$supportedLanguages = \is_array($spellChecker->getSupportedLanguages()) ? |
45
|
|
|
$spellChecker->getSupportedLanguages() : |
46
|
1 |
|
iterator_to_array($spellChecker->getSupportedLanguages()); |
47
|
1 |
|
|
48
|
1 |
|
$spellcheckerSupportedLanguages = array_intersect($supportedLanguages, $languages); |
49
|
|
|
|
50
|
|
|
if ($spellcheckerSupportedLanguages === []) { |
51
|
1 |
|
continue; |
52
|
1 |
|
} |
53
|
1 |
|
|
54
|
|
|
foreach ($spellChecker->check($text, $spellcheckerSupportedLanguages, $context) as $misspelling) { |
55
|
|
|
if (!empty($context)) { |
56
|
1 |
|
$misspelling = $misspelling->mergeContext($context); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (!$misspelling->canDeterminateUniqueIdentity()) { |
60
|
1 |
|
$misspellings[] = $misspelling; |
61
|
|
|
|
62
|
|
|
continue; |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
if (isset($misspellings[$misspelling->getUniqueIdentity()])) { |
66
|
|
|
$misspellings[$misspelling->getUniqueIdentity()]->mergeSuggestions($misspelling->getSuggestions()); |
67
|
|
|
|
68
|
|
|
continue; |
69
|
|
|
} |
70
|
1 |
|
|
71
|
1 |
|
$misspellings[$misspelling->getUniqueIdentity()] = $misspelling; |
72
|
1 |
|
} |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
yield from array_values($misspellings); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
public function getSupportedLanguages(): iterable |
79
|
1 |
|
{ |
80
|
|
|
$supportedLanguages = []; |
81
|
|
|
foreach ($this->spellCheckers as $spellChecker) { |
82
|
|
|
foreach ($spellChecker->getSupportedLanguages() as $language) { |
83
|
|
|
$supportedLanguages[] = $language; |
84
|
1 |
|
} |
85
|
|
|
} |
86
|
1 |
|
|
87
|
1 |
|
return array_values(array_unique($supportedLanguages)); |
88
|
1 |
|
} |
89
|
1 |
|
|
90
|
|
|
/** |
91
|
|
|
* @param array<mixed> $context |
92
|
|
|
* @param array<string> $languages |
93
|
1 |
|
* |
94
|
|
|
* @return iterable<MisspellingInterface> |
95
|
|
|
*/ |
96
|
|
|
private function checkForAllSpellcheckers( |
97
|
|
|
string $text, |
98
|
|
|
array $languages, |
99
|
|
|
array $context |
100
|
|
|
): iterable { |
101
|
|
|
foreach ($this->spellCheckers as $spellChecker) { |
102
|
|
|
$supportedLanguages = \is_array($spellChecker->getSupportedLanguages()) ? |
103
|
|
|
$spellChecker->getSupportedLanguages() : |
104
|
|
|
iterator_to_array($spellChecker->getSupportedLanguages()); |
105
|
|
|
|
106
|
|
|
$spellcheckerSupportedLanguages = array_intersect($supportedLanguages, $languages); |
107
|
|
|
|
108
|
|
|
if ($spellcheckerSupportedLanguages === []) { |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
foreach ($spellChecker->check($text, $spellcheckerSupportedLanguages, $context) as $misspelling) { |
113
|
|
|
if (!empty($context)) { |
114
|
|
|
$misspelling = $misspelling->mergeContext($context); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
yield $misspelling; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|