designbycode / fuzzy-search
The Fuzzy Search package provides a simple and efficient way to perform fuzzy searches on a collection of texts using the Levenshtein distance algorithm. This package is useful when you need to search for texts that may contain typos or slight variations.
Fund package maintenance!
Designbycode
Requires
- php: ^8.1|^8.2|^8.3
- designbycode/levenshtein-distance: ^1.0
Requires (Dev)
- laravel/pint: ^1.0
- pestphp/pest: ^2.20
- spatie/ray: ^1.28
This package is auto-updated.
Last update: 2024-10-28 10:21:59 UTC
README
Introduction
The Fuzzy Search package provides a simple and efficient way to perform fuzzy searches on a collection of texts using the Levenshtein distance algorithm. This package is useful when you need to search for texts that may contain typos or slight variations.
Installation
To install the Fuzzy Search package, simply require it in your PHP project using Composer:
composer require designbycode/fuzzy-search
Usage
Creating a Fuzzy Search Instance
To create a Fuzzy Search instance, you need to pass an array of texts to search and an optional flag for case-insensitive search:
use Designbycode\FuzzySearch\FuzzySearch; $texts = ['apple', 'banana', 'orange', 'grape']; $fuzzySearch = new FuzzySearch($texts, true); // Case-insensitive search
Performing a Fuzzy Search
To perform a fuzzy search, call the search
method and pass the search query and an optional maximum Levenshtein distance:
$query = 'aple'; $maxDistance = 2; $results = $fuzzySearch->search($query, $maxDistance); print_r($results); // Output: ['apple']
The search method returns an array of matching texts, sorted by their Levenshtein distance from the search query.
Getting the Best Match
To get the best match from the search results, call the getBestMatch method:
$bestMatch = $fuzzySearch->getBestMatch($results); echo $bestMatch; // Output: 'apple'
Levenshtein Distance Calculator
The Levenshtein Distance Calculator is a utility class that calculates the Levenshtein distance between two strings. This class is used internally by the Fuzzy Search package.
Calculating the Levenshtein Distance
To calculate the Levenshtein distance between two strings, call the calculate method:
use Designbycode\FuzzySearch\LevenshteinDistance; $str1 = 'kitten'; $str2 = 'sitting'; $distance = LevenshteinDistance::calculate($str1, $str2); echo $distance; // Output: 3
Examples
Example 1: Fuzzy Search with Case-Insensitive Search
$texts = ['Apple', 'Banana', 'Orange', 'Grape']; $fuzzySearch = new FuzzySearch($texts, true); $query = 'aple'; $maxDistance = 2; $results = $fuzzySearch->search($query, $maxDistance); print_r($results); // Output: ['Apple']
Example 2: Fuzzy Search with Case-Sensitive Search
$texts = ['apple', 'banana', 'orange', 'grape']; $fuzzySearch = new FuzzySearch($texts, false); $query = 'Aple'; $maxDistance = 2; $results = $fuzzySearch->search($query, $maxDistance); print_r($results); // Output: []
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.