Browse Source

Add Fuzzy Search driver with unicode support

master
Tom Lingham 9 years ago
parent
commit
0732dc7d2c
  1. 4
      config/searchy.php
  2. 31
      src/Matchers/AcronymUnicodeMatcher.php
  3. 45
      src/Matchers/ConsecutiveCharactersUnicodeMatcher.php
  4. 36
      src/Matchers/StudlyCaseUnicodeMatcher.php
  5. 20
      src/SearchDrivers/FuzzySearchUnicodeDriver.php

4
config/searchy.php

@ -12,6 +12,10 @@ return [
'class' => 'TomLingham\Searchy\SearchDrivers\FuzzySearchDriver',
],
'ufuzzy' => [
'class' => 'TomLingham\Searchy\SearchDrivers\FuzzySearchUnicodeDriver',
],
'simple' => [
'class' => 'TomLingham\Searchy\SearchDrivers\SimpleSearchDriver',
],

31
src/Matchers/AcronymUnicodeMatcher.php

@ -0,0 +1,31 @@
<?php
namespace TomLingham\Searchy\Matchers;
/**
* Matches strings for Acronym 'like' matches but does NOT return Studly Case Matches.
*
* for example, a search for 'fb' would match; 'foo bar' or 'Fred Brown' but not 'FreeBeer'.
*
* Class AcronymMatcher
*/
class AcronymUnicodeMatcher extends BaseMatcher
{
/**
* @var string
*/
protected $operator = 'LIKE';
/**
* @param $searchString
*
* @return mixed|string
*/
public function formatSearchString($searchString)
{
$results = [];
preg_match_all('/./u', mb_strtoupper($searchString, 'UTF-8'), $results);
return implode('% ', $results[0]).'%';
}
}

45
src/Matchers/ConsecutiveCharactersUnicodeMatcher.php

@ -0,0 +1,45 @@
<?php
namespace TomLingham\Searchy\Matchers;
/**
* Matches strings that include all the characters in the search relatively position within the string.
* It also calculates the percentage of characters in the string that are matched and applies the multiplier accordingly.
*
* For Example, a search for 'fba' would match; 'Foo Bar' or 'Afraid of bats'
*
* Class ConsecutiveCharactersMatcher
*/
class ConsecutiveCharactersUnicodeMatcher extends BaseMatcher
{
/**
* @var string
*/
protected $operator = 'LIKE';
/**
* @param $searchString
*
* @return string
*/
public function formatSearchString($searchString)
{
$results = [];
preg_match_all('/./u', $searchString, $results);
return '%'.implode('%', $results[0]).'%';
}
/**
* @param $column
* @param $rawString
*
* @return mixed|string
*/
public function buildQueryString($column, $rawString)
{
$searchString = $this->formatSearchString($rawString);
return "IF( REPLACE($column, '\.', '') {$this->operator} '$searchString', ROUND({$this->multiplier} * ( CHAR_LENGTH( '$rawString' ) / CHAR_LENGTH( REPLACE($column, ' ', '') ))), 0)";
}
}

36
src/Matchers/StudlyCaseUnicodeMatcher.php

@ -0,0 +1,36 @@
<?php
namespace TomLingham\Searchy\Matchers;
/**
* Matches Studly Case strings using the first letters of the words only.
*
* For example a search for 'hp' would match; 'HtmlServiceProvider' or 'HashParser' but not 'hasProvider'
*
* Class StudlyCaseMatcher
*/
class StudlyCaseUnicodeMatcher extends BaseMatcher
{
/**
* @var string
*/
protected $operator = 'LIKE BINARY';
/**
* @param $searchString
*
* @return string
*/
public function formatSearchString($searchString)
{
$results = [];
preg_match_all('/./u', mb_strtoupper($searchString, 'UTF-8'), $results);
return implode('%', $results[0]).'%';
}
public function buildQueryString($column, $searchString)
{
return "IF( CHAR_LENGTH( TRIM( $column )) = CHAR_LENGTH( REPLACE( TRIM( $column ), ' ', '')) AND $column {$this->operator} '{$this->formatSearchString($searchString)}', {$this->multiplier}, 0)";
}
}

20
src/SearchDrivers/FuzzySearchUnicodeDriver.php

@ -0,0 +1,20 @@
<?php
namespace TomLingham\Searchy\SearchDrivers;
class FuzzySearchUnicodeDriver extends BaseSearchDriver
{
/**
* @var array
*/
protected $matchers = [
\TomLingham\Searchy\Matchers\ExactMatcher::class => 100,
\TomLingham\Searchy\Matchers\StartOfStringMatcher::class => 50,
\TomLingham\Searchy\Matchers\AcronymUnicodeMatcher::class => 42,
\TomLingham\Searchy\Matchers\ConsecutiveCharactersUnicodeMatcher::class => 40,
\TomLingham\Searchy\Matchers\StartOfWordsMatcher::class => 35,
\TomLingham\Searchy\Matchers\StudlyCaseUnicodeMatcher::class => 32,
\TomLingham\Searchy\Matchers\InStringMatcher::class => 30,
\TomLingham\Searchy\Matchers\TimesInStringMatcher::class => 8,
];
}
Loading…
Cancel
Save