简体   繁体   English

在php中模糊搜索数组

[英]fuzzy searching an array in php

after i searched i found how to do a fuzzy searching on a string 在我搜索之后,我发现了如何对字符串进行模糊搜索

but i have an array of strings 但我有一个字符串数组

$search = {"a" => "laptop","b" => "screen" ....}

that i retrieved from the DB MySQL 我从DB MySQL中检索到的

IS there any php class or function that does fuzzy searching on an array of words 是否有任何php类或函数对单词数组进行模糊搜索

or at least a link with maybe some useful info's 或至少与一些有用信息的链接

i saw a comment that recommend using PostgreSQL 我看到推荐使用PostgreSQL的评论

and it's fuzzy searching capability but 它是模糊搜索能力但是

the company had already a MySQL DB 该公司已经有一个MySQL数据库

Is there any recommendation ?? 有什么建议吗?

Look at the Levenshtein function 看看Levenshtein的功能

Basically it gives you the difference (in terms of cost) between to strings. 基本上,它为您提供了字符串之间的差异(就成本而言)。 Ie what is the cost to transform string A into string B. 即将字符串A转换为字符串B的成本是多少。

Set yourself a threshold levenshein distance and anything under that for two words mean they're similar. 为自己设定一个阈值levenshein距离,两个词之下的任何东西都意味着它们是相似的。

Also the Bitap algorithm is faster since it can be implemented via bitwise operators, but I believe you will have to implement it yourself, unless there is a PHP lib for it somewhere. Bitap算法也更快,因为它可以通过按位运算符实现,但我相信你必须自己实现它,除非在某处有一个PHP库。

EDIT To use levenshtein method: 编辑使用levenshtein方法:

The search string is "maptop", and you set your "cost threshold" to say 2. That means you want any words that are two string transform operations away from your search string. 搜索字符串是“maptop”,您将“成本阈值”设置为2.这意味着您希望任何两个字符串转换操作的单词远离搜索字符串。

so you loop through your array "A" of strings until 所以你循环遍历你的数组“A”字符串,直到

levenshtein ( A[i] , searchString ) <= 2

That will be your match. 这将是你的比赛。 However you may get more than one word that matches, so it is up to you how you want to handle the extra results. 但是,您可能会获得多个匹配的单词,因此您需要如何处理额外的结果。

You could do this in MySQL since you already have a MySQL database - How do I do a fuzzy match of company names in MYSQL with PHP for auto-complete? 可以在MySQL中执行此操作,因为您已经拥有MySQL数据库 - 如何在MYSQL中使用PHP进行模糊匹配,以便自动完成? which mentions the MySQL Double Metaphone implementation and has an implementation in SQL for MySQL 5.0+ 其中提到了MySQL的双音位实现 ,并具有实现在SQL的MySQL 5.0 +

Edit: Sorry answering here as there is more than could fit in a comment… 编辑:抱歉在这里回答,因为有更多可能适合评论...

Since you've already accepted an answer using PHP Levenshtein function then I suggest you try that approach first. 既然您已经使用PHP Levenshtein函数接受了答案,那么我建议您先尝试这种方法。 Software is iterative; 软件是迭代的; the PHP array search may be exactly what you want but you have to test and implement it first against your requirements. PHP数组搜索可能正是您想要的,但您必须首先根据您的要求测试和实现它。 As I said in your other question a find as you type solution might be the simplest solution here, which simply narrows the product as the user types. 正如我在你的另一个问题中所说, 当你输入解决方案时, 查找可能是最简单的解决方案,它只是在用户输入时缩小产品范围。 There might not be a need to implement any fuzzy searching since you are using the User to do the fuzzy search themselves :-) 可能不需要实现任何模糊搜索,因为您使用用户自己进行模糊搜索:-)

For example a user starts typing S , a , m which allows you to narrow the products to those beginning with Sam . 例如,用户开始键入Sam ,这允许您将产品缩小到以Sam开头的产品 So you are always only letting the user select a product you already know is valid. 因此,您始终只让用户选择您已知的产品才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM