/**
  * 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;
 }
 /**
  * Returns an array of arrays consisting of the synonyms found for each word in the input 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)
 {
     $synGroups = [];
     if (empty($phrase)) {
         return $synGroups;
     }
     $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
     $words = explode(' ', $phrase);
     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;
 }
Exemplo n.º 3
0
 /**
  * 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;
 }
Exemplo n.º 4
0
 /**
  * @param string $phrase
  * @param array $expectedResult
  * @dataProvider loadByPhraseDataProvider
  */
 public function testLoadByPhrase($phrase, $expectedResult)
 {
     $data = $this->model->loadByPhrase($phrase)->getData();
     $i = 0;
     foreach ($expectedResult as $r) {
         $this->assertEquals($r['synonyms'], $data[$i]['synonyms']);
         $this->assertEquals($r['store_id'], $data[$i]['store_id']);
         ++$i;
     }
 }
 /**
  * Custom load model: Get data by user query phrase
  *
  * @param \Magento\Search\Model\SynonymReader $object
  * @param string $phrase
  * @return $this
  */
 public function loadByPhrase(\Magento\Search\Model\SynonymReader $object, $phrase)
 {
     $rows = $this->queryByPhrase(strtolower($phrase));
     $synsPerScope = $this->getSynRowsPerScope($rows);
     if (!empty($synsPerScope[\Magento\Store\Model\ScopeInterface::SCOPE_STORES])) {
         $object->setData($synsPerScope[\Magento\Store\Model\ScopeInterface::SCOPE_STORES]);
     } elseif (!empty($synsPerScope[\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITES])) {
         $object->setData($synsPerScope[\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITES]);
     } else {
         $object->setData($synsPerScope[\Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT]);
     }
     $this->_afterLoad($object);
     return $this;
 }