/** * Return spelled string * * @param string $baseQuery * @return string */ public function getSuggest($baseQuery) { $this->diffs = []; $this->keys = []; $final = []; $baseQuery = $this->text->cleanString($baseQuery); $queries = $this->text->splitWords($baseQuery); foreach ($queries as $query) { $len = $this->text->strlen($query); if ($len < $this->text->getGram() || is_numeric($query)) { $final[] = $query; continue; } $result = $this->getBestMatch($query); $keyword = $result['keyword']; $this->split($query, '', $query); $splitKeyword = ''; if (count($this->diffs)) { arsort($this->diffs); $keys = array_keys($this->diffs); $key = $keys[0]; $splitKeyword = $this->keys[$key]; } $basePer = $this->damerau->similarity($query, $keyword); $splitPer = $this->damerau->similarity($query, $splitKeyword); if ($basePer > $splitPer) { $final[] = $keyword; } else { $final[] = $splitKeyword; } } $result = implode(' ', $final); if ($this->damerau->similarity($result, $baseQuery) < 50) { $result = ''; } return $result; }
/** * Split string to words * * @param string $string * @param array &$results * @param int $increment * @return void */ protected function split($string, &$results, $increment = 1) { $string = $this->text->cleanString($string); $words = $this->text->splitWords($string); foreach ($words as $word) { if ($this->text->strlen($word) >= $this->text->getGram() && !is_numeric($word) && $this->text->strlen($word) <= 10) { $word = $this->text->strtolower($word); if (!isset($results[$word])) { $results[$word] = $increment; } else { $results[$word] += $increment; } } } }
/** * @param null|string $expected * @param string $data * @dataProvider prepareCleanStringProvider */ public function testCleanString($expected, $data) { $this->assertEquals($expected, $this->model->cleanString($data)); }