Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- const DEFAULT_SUGGESTION_AMOUNT = 3;
- $suggestionAmount = 0;
- if (isset($_SERVER['REQUEST_METHOD'])) {
- if (!isset($_GET['input'])) {
- die("Please use the script with a GET parameter 'input' that consist of first letters of a search string. " .
- "Optionally 'suggestions' parameter can be set as a number of suggestions. " .
- "Usage: '/search-suggestions.php?input=Alex&suggestions=3'\n");
- }
- $input = filter_input(INPUT_GET, 'input', FILTER_SANITIZE_STRING);
- if (0 === strlen($input)) {
- die("Please use the script with a GET parameter 'input' that consist of first letters of a search string. " .
- "Optionally 'suggestions' parameter can be set as a number of suggestions. " .
- "Usage: '/search-suggestions.php?input=Alex&suggestions=3'\n");
- }
- $suggestionAmount = (int)filter_input(INPUT_GET, 'suggestions', FILTER_SANITIZE_NUMBER_INT);
- } elseif (!isset($argv[1])) {
- die("Please run the script with a first parameter that consist of first letters of a search string." .
- "Optionally the second parameter can be set as a number of suggestions. " .
- "Usage: 'php search-suggestions.php Alex 5'\n");
- } else {
- $input = filter_var($argv[1], FILTER_SANITIZE_STRING);
- if (0 === strlen($input)) {
- die("Please run the script with a first parameter that consist of first letters of a search string." .
- "Optionally the second parameter can be set as a number of suggestions. " .
- "Usage: 'php search-suggestions.php Alex 5'\n");
- }
- if (isset($argv[2])) {
- $suggestionAmount = (int)filter_var($argv[2], FILTER_SANITIZE_NUMBER_INT);
- }
- }
- $suggestionAmount = abs($suggestionAmount ?: DEFAULT_SUGGESTION_AMOUNT);
- $input = ucwords($input);
- $suggestions = [];
- $farSuggestions = [];
- $exactMatchesCount = 0;
- $dictionary = new SplFileObject('names.csv');
- $dictionary->setFlags(SplFileObject::READ_CSV);
- $dictionary->next();
- foreach ($dictionary as $row) {
- if (!isset($row[1])) {
- continue;
- }
- $firstLetterIsDifferent = !(substr($row[1], 0, 1) === substr($input, 0, 1));
- $nameAlreadyAdded = in_array($row[1], $suggestions) || in_array($row[1], $farSuggestions);
- if ($firstLetterIsDifferent || $nameAlreadyAdded) {
- continue;
- }
- $subString = substr($row[1], 0, strlen($input));
- if ($subString === $input) {
- array_unshift($suggestions, $row[1]);
- if (++$exactMatchesCount === $suggestionAmount) {
- break;
- }
- } elseif (count($suggestions) < $suggestionAmount && 1 === levenshtein($input, $subString)) {
- $suggestions[] = $row[1];
- } elseif (count($farSuggestions) < $suggestionAmount && 2 === levenshtein($input, $subString)) {
- $farSuggestions[] = $row[1];
- }
- }
- if (count($suggestions) < $suggestionAmount && count($farSuggestions) > 0) {
- $suggestions = array_merge($suggestions, array_slice($farSuggestions, 0, $suggestionAmount - count($suggestions)));
- } elseif (count($suggestions) > $suggestionAmount) {
- $suggestions = array_slice($suggestions, 0, $suggestionAmount);
- }
- echo json_encode($suggestions);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement