Fuzzy array search in php
after i searched i found how to do a fuzzy search in a string
but i have an array of strings
$search = {"a" => "laptop","b" => "screen" ....}
which i got from mysql db
Is there any php class or function that does a fuzzy search on an array of words
or at least a link to possibly useful information
I saw a comment that recommends using PostgreSQL
and this is a fuzzy search function, but
the company already had MySQL DB
Are there any recommendations?
Check out the Levenshtein feature
This basically gives you the difference (in terms of cost) between the lines. That is, what is the cost of converting string A to string B.
Set yourself a levenshein distance threshold and anything below that for the two words means they are similar.
Also, the Bitap algorithm is faster, since it can be implemented through bitwise operators, but I believe that you will have to implement it yourself, if only this is a PHP library for it somewhere.
EDIT Use the Levenshtein method:
The search string is โmaptopโ and you set your โcost thresholdโ to say 2. This means that you want any words that are two string conversion operations to be removed from your search string.
so you scroll the array "A" of the rows to
levenshtein ( A[i] , searchString ) <= 2
This will be your match. However, you can get more than one word that matches, so you decide how you want to handle the additional results.
You can do this in MySQL since you already have a MySQL database - How do I fuzzy match the company names in MYSQL with PHP for automatic completion? which mentions the MySQL Double Metaphone implementation and has an implementation in SQL for MySQL 5.0+
Edit: Sorry, replying here, as there is more than can fit in the & hellip;
Since you already accepted the answer using the PHP Levenshtein function , I suggest you try this approach first. The software is iterative; Finding a PHP array may be exactly what you want, but you need to first test and implement it according to your requirements. As I said in your other question , searching by type of solution may be the simplest solution here, which simply narrows the product by type of user. Perhaps there is no need to implement any fuzzy search, since you are using the user to perform the fuzzy search itself :-)
For example, the user starts typing S , a , m , which allows you to narrow down the products to those that start with Sam . Thus, you always allow the user to select a product that you already know is valid.