7 declare(strict_types=1);
29 $this->synReaderModel = $synReader;
53 if (empty(trim($phrase))) {
57 $synonymGroups = $this->getSynonymGroupsByPhrase($phrase);
60 $phrase = preg_replace(
"/ {2,}/",
" ", $phrase);
64 $words = explode(
' ', $phrase);
66 foreach ($words as $offset => $word) {
70 $pattern = $this->getSearchPattern(\array_slice($words, $offset));
71 $position = $this->findInArray(
$pattern, $synonymGroups);
72 if ($position !==
null) {
73 $synonyms = explode(
',', $synonymGroups[$position]);
92 private function findInArray(
string $pattern, array $synonymGroupsToExamine)
95 foreach ($synonymGroupsToExamine as $synonymGroup) {
96 $matchingResultCode = preg_match(
$pattern, $synonymGroup);
97 if ($matchingResultCode === 1) {
135 private function getSearchPattern(array $words): string
138 for ($lastItem = count($words); $lastItem > 0; $lastItem--) {
139 $phrase = implode(
"\s+", \array_slice($words, 0, $lastItem));
140 $patterns[] =
'^' . $phrase .
',';
141 $patterns[] =
',' . $phrase .
',';
142 $patterns[] =
',' . $phrase .
'$';
145 $pattern =
'/' . implode(
'|', $patterns) .
'/i';
158 private function getSynonymGroupsByPhrase(
string $phrase): array
163 $synonymGroups = $this->synReaderModel->loadByPhrase($phrase)->getData();
164 foreach ($synonymGroups as
$row) {
getSynonymsForPhrase($phrase)
__construct(SynonymReader $synReader)