-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into MAGETWO-46472
- Loading branch information
Showing
24 changed files
with
768 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
/** | ||
* Copyright © 2015 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Search\Api; | ||
|
||
/** | ||
* @api | ||
*/ | ||
interface SynonymAnalyzerInterface | ||
{ | ||
/** | ||
* Get synonyms for specified phrase | ||
* | ||
* For phrase: "Elizabeth is the English queen" correct output is an array of arrays containing synonyms for each | ||
* word in the phrase: | ||
* | ||
* [ | ||
* 0 => [ 0 => "elizabeth" ], | ||
* 1 => [ 0 => "is" ], | ||
* 2 => [ 0 => "the" ], | ||
* 3 => [ 0 => "british", 1 => "english" ], | ||
* 4 => [ 0 => "queen", 1 => "monarch" ] | ||
* ] | ||
* | ||
* @param string $phrase | ||
* @return array | ||
*/ | ||
public function getSynonymsForPhrase($phrase); | ||
} |
98 changes: 98 additions & 0 deletions
98
app/code/Magento/Search/Model/ResourceModel/SynonymReader.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
/** | ||
* Copyright © 2015 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Search\Model\ResourceModel; | ||
|
||
use Magento\Framework\Model\AbstractModel; | ||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb; | ||
use Magento\Search\Model\SynonymReader as SynReaderModel; | ||
use Magento\Framework\DB\Helper\Mysql\Fulltext; | ||
|
||
/** | ||
* Synonym Reader resource model | ||
*/ | ||
class SynonymReader extends AbstractDb | ||
{ | ||
/** | ||
* @var Fulltext $fullTextSelect | ||
*/ | ||
private $fullTextSelect; | ||
|
||
/** | ||
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context | ||
* @param Fulltext $fulltext | ||
* @param string $connectionName | ||
*/ | ||
public function __construct( | ||
\Magento\Framework\Model\ResourceModel\Db\Context $context, | ||
Fulltext $fulltext, | ||
$connectionName = null | ||
) { | ||
parent::__construct($context, $connectionName); | ||
$this->fullTextSelect = $fulltext; | ||
} | ||
|
||
/** | ||
* Custom load model: Get data by store view id | ||
* | ||
* @param AbstractModel $object | ||
* @param int $value | ||
* @return $this | ||
*/ | ||
public function loadByStoreViewId(AbstractModel $object, $value) | ||
{ | ||
$select = $this->getConnection()->select()->from( | ||
$this->getMainTable() | ||
)->where( | ||
'store_id = ?', | ||
$value | ||
); | ||
$data = $this->getConnection()->fetchAll($select); | ||
if ($data) { | ||
$object->setData($data); | ||
$this->_afterLoad($object); | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* Custom load model: Get data by user query phrase and store view id | ||
* | ||
* @param SynReaderModel $object | ||
* @param string $value | ||
* @return $this | ||
*/ | ||
public function loadByPhrase(SynReaderModel $object, $value) | ||
{ | ||
$phrase = strtolower($value); | ||
$matchQuery = $this->fullTextSelect->getMatchQuery( | ||
['synonyms' => 'synonyms'], | ||
$phrase, | ||
Fulltext::FULLTEXT_MODE_BOOLEAN | ||
); | ||
$query = $this->getConnection()->select()->from( | ||
$this->getMainTable() | ||
)->where( | ||
'store_id = ?', | ||
$object->getStoreViewId() | ||
)->where($matchQuery); | ||
|
||
$rows = $this->getConnection()->fetchAll($query); | ||
$object->setData($rows); | ||
$this->_afterLoad($object); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Init resource data | ||
* | ||
* @return void | ||
*/ | ||
protected function _construct() | ||
{ | ||
$this->_init('search_synonyms', 'group_id'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
/** | ||
* Copyright © 2015 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Search\Model; | ||
|
||
use Magento\Search\Api\SynonymAnalyzerInterface; | ||
use Magento\Search\Model\SynonymsReader; | ||
|
||
class SynonymAnalyzer implements SynonymAnalyzerInterface | ||
{ | ||
/** | ||
* @var SynonymReader $synReaderModel | ||
*/ | ||
protected $synReaderModel; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param SynonymReader $synReader | ||
*/ | ||
public function __construct(SynonymReader $synReader) | ||
{ | ||
$this->synReaderModel = $synReader; | ||
} | ||
|
||
/** | ||
* Returns an array of arrays consisting of the synonyms found for each word in the input phrase | ||
* | ||
* @param string $phrase | ||
* @return array | ||
*/ | ||
public function getSynonymsForPhrase($phrase) | ||
{ | ||
$synGroups = []; | ||
|
||
if (empty($phrase)) { | ||
return $synGroups; | ||
} | ||
|
||
// strip off all the white spaces, comma, semicolons, and other such | ||
// "non-word" characters. Then implode it into a single string using white space as delimiter | ||
//$words = preg_split('/\W+/', strtolower($phrase), -1, PREG_SPLIT_NO_EMPTY); | ||
$words = preg_split( | ||
'/[~`!@#$%^&*()_+={}\[\]:"\',\s\.<>?\/\;\\\]+/', | ||
strtolower($phrase), | ||
-1, | ||
PREG_SPLIT_NO_EMPTY | ||
); | ||
$phrase = implode(' ', $words); | ||
|
||
$rows = $this->synReaderModel->loadByPhrase($phrase)->getData(); | ||
$synonyms = []; | ||
foreach ($rows as $row) { | ||
$synonyms [] = $row['synonyms']; | ||
} | ||
|
||
// Go through every returned record looking for presence of the actual phrase. If there were no matching | ||
// records found in DB then create a new entry for it in the returned array | ||
foreach ($words as $w) { | ||
$position = $this->findInArray($w, $synonyms); | ||
if ($position !== false) { | ||
$synGroups[] = explode(',', $synonyms[$position]); | ||
} else { | ||
// No synonyms were found. Return the original word in this position | ||
$synGroups[] = [$w]; | ||
} | ||
} | ||
return $synGroups; | ||
} | ||
|
||
/** | ||
* Helper method to find the presence of $word in $wordsArray. If found, the particular array index is returned. | ||
* Otherwise false will be returned. | ||
* | ||
* @param string $word | ||
* @param $array $wordsArray | ||
* @return boolean | int | ||
*/ | ||
private function findInArray($word, $wordsArray) | ||
{ | ||
if (empty($wordsArray)) { | ||
return false; | ||
} | ||
$position = 0; | ||
foreach ($wordsArray as $wordsLine) { | ||
$pattern = '/^' . $word . ',|,' . $word . ',|,' . $word . '$/'; | ||
$rv = preg_match($pattern, $wordsLine); | ||
if ($rv != 0) { | ||
return $position; | ||
} | ||
$position++; | ||
} | ||
return false; | ||
} | ||
} |
Oops, something went wrong.