部分匹配
是否有内置函数或某人已经编写的函数可以在不精确的情况下匹配名称?
例如,我有:
Marry
John
Steve
Steven
Stewie
如果有人输入“stew”,该函数将返回 Stewie。
或者,如果有人输入“ry”,该函数将返回 Marry。
或者,如果有人拼错“Marry”,该函数仍会返回 Marry。 (因为它们是最相似的)
如果提供“Ste”,它可能返回 false,但这对我来说并不重要。
有谁知道如何编写此类函数或知道已经编写的函数?鉴于这可能是常见的事情,我想是这样。
谢谢。
Is there a built in function or a function that someone has already written that can match names without being exact?
For example, I have:
Marry
John
Steve
Steven
Stewie
If someone types "stew" the function would return Stewie.
Or if someone types "ry" the function would return Marry.
Or if someone misspells "Marries" the function would still return Marry. (due to being the most similar of them all)
If "Ste" is supplied it can return false but it doesn't really matter to me.
Does anyone know how to write this sort of function or know of one already written? Seeing as this is probably a common thing, I would assume so.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上有一些方法可以实现这一点:
内置方法
非内置方法
其中之一应该帮助您解决您的问题。
这些算法的问题在于它们都不准确。这样你就会有一个启发式的问题解决方案。
通常,距离算法和声音算法之间各有利弊。声音特定算法的准确度较低(准确度约为 33%)。但速度很快。 Levensthein 准确得多,但速度较慢。至少是php的实现。还有其他系统,Levensthein 的速度要快很多(参见例如 Levensthein Automata,但是这个自动机算法不是内置在 php 中的)。
可能作为一个基本提示:
Actually there are some methods to achieve this:
Built-In methods
Not Built in methods
One of those should help you to solve your problem.
The problem of every of those algorithms is that they are not accurate. So you will have a heuristical solution to the problem.
Usually there are pro and cons between distance and sound algorithms. Sound specific algorithms are less accurate(round about 33% accuracy). But fast. Levensthein is much more accurate but slow. At least the php implementation. There are other systems where Levensthein is faster by a large margin (see e.g. Levensthein Automata. But this automata algorithm is not built in in php).
Probably as a basic hint:
听起来像 soundex() 或 metaphone() 就是您要寻找的。使用这些,您可以计算一个代表单词发音的“键” - 对所有字符串执行此操作,如果两个单词发音相同(针对英语进行了优化),您可以比较。
另一种可能性是 levenshtein() 直接计算两个之间的差异字符串,这样您就可以比较所有字符串并显示 5 个最佳命中或类似内容。
sounds like soundex() or metaphone() is what you're looking for. using those, you can calculate a "key" that represents how a word sounds - doing this for all strings you can compare if two words sound the same (optimized for english language).
another possibility would be levenshtein() wich directly calculates the difference between two strings, so you can compare all strings and show the 5 best hits or something like that.