Skip to content

Commit a4f4d8d

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-77777' into PANDA-FIXES-2.2
2 parents ba4b975 + 701af4e commit a4f4d8d

File tree

3 files changed

+139
-29
lines changed

3 files changed

+139
-29
lines changed

app/code/Magento/Search/Model/SynonymAnalyzer.php

Lines changed: 98 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
68
namespace Magento\Search\Model;
79

810
use Magento\Search\Api\SynonymAnalyzerInterface;
911

12+
/**
13+
* SynonymAnalyzer responsible for search of synonyms matching a word or a phrase.
14+
*/
1015
class SynonymAnalyzer implements SynonymAnalyzerInterface
1116
{
1217
/**
@@ -42,55 +47,119 @@ public function __construct(SynonymReader $synReader)
4247
*/
4348
public function getSynonymsForPhrase($phrase)
4449
{
45-
$synGroups = [];
50+
$result = [];
4651

47-
if (empty($phrase)) {
48-
return $synGroups;
52+
if (empty(trim($phrase))) {
53+
return $result;
4954
}
5055

51-
$rows = $this->synReaderModel->loadByPhrase($phrase)->getData();
52-
$synonyms = [];
53-
foreach ($rows as $row) {
54-
$synonyms [] = $row['synonyms'];
55-
}
56+
$synonymGroups = $this->getSynonymGroupsByPhrase($phrase);
57+
58+
// Replace multiple spaces in a row with the only one space
59+
$phrase = preg_replace("/ {2,}/", " ", $phrase);
5660

5761
// Go through every returned record looking for presence of the actual phrase. If there were no matching
5862
// records found in DB then create a new entry for it in the returned array
5963
$words = explode(' ', $phrase);
60-
foreach ($words as $w) {
61-
$position = $this->findInArray($w, $synonyms);
62-
if ($position !== false) {
63-
$synGroups[] = explode(',', $synonyms[$position]);
64-
} else {
65-
// No synonyms were found. Return the original word in this position
66-
$synGroups[] = [$w];
64+
65+
foreach ($words as $offset => $word) {
66+
$synonyms = [$word];
67+
68+
if ($synonymGroups) {
69+
$pattern = $this->getSearchPattern(array_slice($words, $offset));
70+
$position = $this->findInArray($pattern, $synonymGroups);
71+
if ($position !== null) {
72+
$synonyms = explode(',', $synonymGroups[$position]);
73+
}
6774
}
75+
76+
$result[] = $synonyms;
6877
}
69-
return $synGroups;
78+
79+
return $result;
7080
}
7181

7282
/**
73-
* Helper method to find the presence of $word in $wordsArray. If found, the particular array index is returned.
83+
* Helper method to find the matching of $pattern to $synonymGroupsToExamine.
84+
* If matches, the particular array index is returned.
7485
* Otherwise false will be returned.
7586
*
76-
* @param string $word
77-
* @param $array $wordsArray
78-
* @return boolean | int
87+
* @param string $pattern
88+
* @param array $synonymGroupsToExamine
89+
* @return int|null
7990
*/
80-
private function findInArray($word, $wordsArray)
91+
private function findInArray(string $pattern, array $synonymGroupsToExamine)
8192
{
82-
if (empty($wordsArray)) {
83-
return false;
84-
}
8593
$position = 0;
86-
foreach ($wordsArray as $wordsLine) {
87-
$pattern = '/^' . $word . ',|,' . $word . ',|,' . $word . '$/';
88-
$rv = preg_match($pattern, $wordsLine);
89-
if ($rv != 0) {
94+
foreach ($synonymGroupsToExamine as $synonymGroup) {
95+
$matchingResultCode = preg_match($pattern, $synonymGroup);
96+
if ($matchingResultCode === 1) {
9097
return $position;
9198
}
9299
$position++;
93100
}
94-
return false;
101+
return null;
102+
}
103+
104+
/**
105+
* Returns a regular expression to search for synonyms of the phrase represented as the list of words.
106+
*
107+
* Returned pattern contains expression to search for a part of the phrase from the beginning.
108+
*
109+
* For example, in the phrase "Elizabeth is the English queen" with subset from the very first word,
110+
* the method will build an expression which looking for synonyms for all these patterns:
111+
* - Elizabeth is the English queen
112+
* - Elizabeth is the English
113+
* - Elizabeth is the
114+
* - Elizabeth is
115+
* - Elizabeth
116+
*
117+
* For the same phrase on the second iteration with the first word "is" it will match for these synonyms:
118+
* - is the English queen
119+
* - is the English
120+
* - is the
121+
* - is
122+
*
123+
* The pattern looking for exact match and will not find these phrases as synonyms:
124+
* - Is there anybody in the room?
125+
* - Is the English is most popular language?
126+
* - Is the English queen Elizabeth?
127+
*
128+
* Take into account that returned pattern expects that data will be represented as comma-separated value.
129+
*
130+
* @param array $words
131+
* @return string
132+
*/
133+
private function getSearchPattern(array $words): string
134+
{
135+
$patterns = [];
136+
for ($lastItem = count($words); $lastItem > 0; $lastItem--) {
137+
$phrase = implode("\s+", array_slice($words, 0, $lastItem));
138+
$patterns[] = '^' . $phrase . ',';
139+
$patterns[] = ',' . $phrase . ',';
140+
$patterns[] = ',' . $phrase . '$';
141+
}
142+
143+
$pattern = '/' . implode('|', $patterns) . '/i';
144+
return $pattern;
145+
}
146+
147+
/**
148+
* Get all synonym groups for the phrase
149+
*
150+
* Returns an array of synonyms which are represented as comma-separated value for each item in the list
151+
*
152+
* @param string $phrase
153+
* @return string[]
154+
*/
155+
private function getSynonymGroupsByPhrase(string $phrase): array
156+
{
157+
$result = [];
158+
159+
$synonymGroups = $this->synReaderModel->loadByPhrase($phrase)->getData();
160+
foreach ($synonymGroups as $row) {
161+
$result[] = $row['synonyms'];
162+
}
163+
return $result;
95164
}
96165
}

dev/tests/integration/testsuite/Magento/Search/Model/SynonymAnalyzerTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,38 @@ public static function loadGetSynonymsForPhraseDataProvider()
4242
'phrase' => 'universe is enormous',
4343
'expectedResult' => [['universe', 'cosmos'], ['is'], ['big', 'huge', 'large', 'enormous']]
4444
],
45+
'WithCaseMismatch' => [
46+
'phrase' => 'GNU\'s Not Unix',
47+
'expectedResult' => [['GNU\'s'], ['Not'], ['unix', 'linux'],]
48+
],
49+
'WithMultiWordPhrase' => [
50+
'phrase' => 'Coastline of Great Britain stretches for 11,073 miles',
51+
'expectedResult' => [
52+
['Coastline'],
53+
['of'],
54+
['Great Britain', 'United Kingdom'],
55+
['Britain'],
56+
['stretches'],
57+
['for'],
58+
['11,073'],
59+
['miles']
60+
]
61+
],
62+
'PartialSynonymMatching' => [
63+
'phrase' => 'Magento Engineering',
64+
'expectedResult' => [
65+
['orange', 'magento'],
66+
['Engineering', 'Technical Staff']
67+
]
68+
],
4569
'noSynonyms' => [
4670
'phrase' => 'this sentence has no synonyms',
4771
'expectedResult' => [['this'], ['sentence'], ['has'], ['no'], ['synonyms']]
4872
],
73+
'multipleSpaces' => [
74+
'phrase' => 'GNU\'s Not Unix',
75+
'expectedResult' => [['GNU\'s'], ['Not'], ['unix', 'linux'],]
76+
],
4977
'oneMoreTest' => [
5078
'phrase' => 'schlicht',
5179
'expectedResult' => [['schlicht', 'natürlich']]

dev/tests/integration/testsuite/Magento/Search/_files/synonym_reader.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,22 @@
2424
$synonymsModel = $objectManager->create(\Magento\Search\Model\SynonymReader::class);
2525
$synonymsModel->setSynonyms('hill,mountain,peak')->setWebsiteId(1)->save();
2626

27+
$synonymsModel = $objectManager->create(\Magento\Search\Model\SynonymReader::class);
28+
$synonymsModel->setSynonyms('Community Engineering,Contributors,Magento Community Engineering')->setWebsiteId(1)
29+
->save();
30+
31+
$synonymsModel = $objectManager->create(\Magento\Search\Model\SynonymReader::class);
32+
$synonymsModel->setSynonyms('Engineering,Technical Staff')->setWebsiteId(1)->save();
33+
2734
// Synonym groups for "All Store Views"
2835
$synonymsModel = $objectManager->create(\Magento\Search\Model\SynonymReader::class);
2936
$synonymsModel->setSynonyms('universe,cosmos')->setWebsiteId(0)->save();
3037

38+
$synonymsModel = $objectManager->create(\Magento\Search\Model\SynonymReader::class);
39+
$synonymsModel->setSynonyms('unix,linux')->setWebsiteId(0)->save();
40+
41+
$synonymsModel = $objectManager->create(\Magento\Search\Model\SynonymReader::class);
42+
$synonymsModel->setSynonyms('Great Britain,United Kingdom')->setWebsiteId(0)->save();
43+
3144
$synonymsModel = $objectManager->create(\Magento\Search\Model\SynonymReader::class);
3245
$synonymsModel->setSynonyms('big,huge,large,enormous')->setWebsiteId(0)->save();

0 commit comments

Comments
 (0)