Esempio n. 1
0
 /**
  * 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;
 }
Esempio n. 2
0
 /**
  * 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;
             }
         }
     }
 }
Esempio n. 3
0
 /**
  * Fallback
  *
  * @param string $text
  * @return string
  */
 public function fallback($text)
 {
     $arQuery = $this->text->splitWords($text);
     for ($i = 1; $i < count($arQuery); $i++) {
         $combinations = $this->fallbackCombinations($arQuery, $i);
         foreach ($combinations as $combination) {
             $newQuery = $text;
             foreach ($combination as $word) {
                 $newQuery = str_replace($word, '', $newQuery);
                 $cntResults = $this->getNumResults($newQuery);
                 if ($cntResults > 0) {
                     $newQuery = preg_replace('/\\s{2,}/', ' ', $newQuery);
                     return trim($newQuery);
                 }
             }
         }
     }
     return false;
 }
Esempio n. 4
0
 /**
  * @param null|string $expected
  * @param string      $data
  * @dataProvider prepareSplitWordsProvider
  */
 public function testSplitWords($expected, $data)
 {
     $this->assertEquals($expected, $this->model->splitWords($data));
 }