/** * 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; }
/** * @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; } }