Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
SynonymAnalyzer Class Reference
Inheritance diagram for SynonymAnalyzer:
SynonymAnalyzerInterface

Public Member Functions

 __construct (SynonymReader $synReader)
 
 getSynonymsForPhrase ($phrase)
 

Protected Attributes

 $synReaderModel
 

Detailed Description

SynonymAnalyzer responsible for search of synonyms matching a word or a phrase.

Definition at line 15 of file SynonymAnalyzer.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( SynonymReader  $synReader)

Constructor

Parameters
SynonymReader$synReader

Definition at line 27 of file SynonymAnalyzer.php.

28  {
29  $this->synReaderModel = $synReader;
30  }

Member Function Documentation

◆ getSynonymsForPhrase()

getSynonymsForPhrase (   $phrase)

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" ] ]

Parameters
string$phrase
Returns
array
Exceptions

Implements SynonymAnalyzerInterface.

Definition at line 49 of file SynonymAnalyzer.php.

50  {
51  $result = [];
52 
53  if (empty(trim($phrase))) {
54  return $result;
55  }
56 
57  $synonymGroups = $this->getSynonymGroupsByPhrase($phrase);
58 
59  // Replace multiple spaces in a row with the only one space
60  $phrase = preg_replace("/ {2,}/", " ", $phrase);
61 
62  // Go through every returned record looking for presence of the actual phrase. If there were no matching
63  // records found in DB then create a new entry for it in the returned array
64  $words = explode(' ', $phrase);
65 
66  foreach ($words as $offset => $word) {
67  $synonyms = [$word];
68 
69  if ($synonymGroups) {
70  $pattern = $this->getSearchPattern(\array_slice($words, $offset));
71  $position = $this->findInArray($pattern, $synonymGroups);
72  if ($position !== null) {
73  $synonyms = explode(',', $synonymGroups[$position]);
74  }
75  }
76 
77  $result[] = $synonyms;
78  }
79 
80  return $result;
81  }
$pattern
Definition: website.php:22

Field Documentation

◆ $synReaderModel

SynonymReader $synReaderModel
protected

Definition at line 20 of file SynonymAnalyzer.php.


The documentation for this class was generated from the following file: