SHARE
TWEET

Untitled

a guest Oct 18th, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. const DEFAULT_SUGGESTION_AMOUNT = 3;
  4.  
  5. $suggestionAmount = 0;
  6. if (isset($_SERVER['REQUEST_METHOD'])) {
  7.     if (!isset($_GET['input'])) {
  8.         die("Please use the script with a GET parameter 'input' that consist of first letters of a search string. " .
  9.             "Optionally 'suggestions' parameter can be set as a number of suggestions. " .
  10.             "Usage: '/search-suggestions.php?input=Alex&suggestions=3'\n");
  11.     }
  12.  
  13.     $input = filter_input(INPUT_GET, 'input', FILTER_SANITIZE_STRING);
  14.     if (0 === strlen($input)) {
  15.         die("Please use the script with a GET parameter 'input' that consist of first letters of a search string. " .
  16.             "Optionally 'suggestions' parameter can be set as a number of suggestions. " .
  17.             "Usage: '/search-suggestions.php?input=Alex&suggestions=3'\n");
  18.     }
  19.  
  20.     $suggestionAmount = (int)filter_input(INPUT_GET, 'suggestions', FILTER_SANITIZE_NUMBER_INT);
  21. } elseif (!isset($argv[1])) {
  22.     die("Please run the script with a first parameter that consist of first letters of a search string." .
  23.         "Optionally the second parameter can be set as a number of suggestions. " .
  24.         "Usage: 'php search-suggestions.php Alex 5'\n");
  25. } else {
  26.     $input = filter_var($argv[1], FILTER_SANITIZE_STRING);
  27.     if (0 === strlen($input)) {
  28.         die("Please run the script with a first parameter that consist of first letters of a search string." .
  29.             "Optionally the second parameter can be set as a number of suggestions. " .
  30.             "Usage: 'php search-suggestions.php Alex 5'\n");
  31.     }
  32.  
  33.     if (isset($argv[2])) {
  34.         $suggestionAmount = (int)filter_var($argv[2], FILTER_SANITIZE_NUMBER_INT);
  35.     }
  36. }
  37.  
  38. $suggestionAmount = abs($suggestionAmount ?: DEFAULT_SUGGESTION_AMOUNT);
  39.  
  40. $input = ucwords($input);
  41.  
  42. $suggestions = [];
  43. $farSuggestions = [];
  44.  
  45. $exactMatchesCount = 0;
  46.  
  47. $dictionary = new SplFileObject('names.csv');
  48. $dictionary->setFlags(SplFileObject::READ_CSV);
  49. $dictionary->next();
  50.  
  51. foreach ($dictionary as $row) {
  52.     if (!isset($row[1])) {
  53.         continue;
  54.     }
  55.  
  56.     $firstLetterIsDifferent = !(substr($row[1], 0, 1) === substr($input, 0, 1));
  57.     $nameAlreadyAdded = in_array($row[1], $suggestions) || in_array($row[1], $farSuggestions);
  58.     if ($firstLetterIsDifferent || $nameAlreadyAdded) {
  59.         continue;
  60.     }
  61.  
  62.     $subString = substr($row[1], 0, strlen($input));
  63.     if ($subString === $input) {
  64.         array_unshift($suggestions, $row[1]);
  65.         if (++$exactMatchesCount === $suggestionAmount) {
  66.             break;
  67.         }
  68.     } elseif (count($suggestions) < $suggestionAmount && 1 === levenshtein($input, $subString)) {
  69.         $suggestions[] = $row[1];
  70.     } elseif (count($farSuggestions) < $suggestionAmount && 2 === levenshtein($input, $subString)) {
  71.         $farSuggestions[] = $row[1];
  72.     }
  73. }
  74.  
  75. if (count($suggestions) < $suggestionAmount && count($farSuggestions) > 0) {
  76.     $suggestions = array_merge($suggestions, array_slice($farSuggestions, 0, $suggestionAmount - count($suggestions)));
  77. } elseif (count($suggestions) > $suggestionAmount) {
  78.     $suggestions = array_slice($suggestions, 0, $suggestionAmount);
  79. }
  80.  
  81. echo json_encode($suggestions);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top