判断两个字符串在Javascript中是否相似?
假设我有两个字符串,有什么方法可以检查它们是否至少 90% 相似?
var string1 = "theBoardmeetstoday,tomorrow51";
var string2 = "Board meets today, tomorrow";
谢谢,
泰根
Let's say I have two strings, is there any way to check if they are at least 90% similar?
var string1 = "theBoardmeetstoday,tomorrow51";
var string2 = "Board meets today, tomorrow";
Thanks,
Tegan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Levenshtein distance 的 wikipedia 条目包含一个示例实现。
The wikipedia entry for Levenshtein distance includes a sample implementation.
jsdifflib 是 Python 优秀 difflib 库。
它有一个函数
ratio()
,该函数“以 [0, 1] 范围内的浮点形式返回序列相似性的度量。”jsdifflib is a JavaScript port of Python's excellent difflib library.
It has a function
ratio()
which "return[s] a measure of the sequences’ similarity as a float in the range [0, 1]."另请考虑骰子系数,它被认为“基本上更好”比 string-similarity github 存储库及其相应的 npm 模块。
其文档中的用法:
Also consider Dice's Coefficient which is considered "mostly better" than the Levenshtein distance by the creator of the string-similarity github repo and its corresponding npm module.
Usage from its docs:
String.levenshtein(MooTools 插件)
查看一下: http://mootools.net/forge/p/string_levenshtein
GitHub:https://github.com/thinkphp/String.levenshtein
这个方法计算两个字符串之间的编辑距离。在信息论和计算机科学中,编辑距离是衡量两个序列之间差异量(称为编辑距离)的度量。两个字符串之间的编辑距离由将一个字符串转换为另一个给定字符串所需的最小操作数给出,其中可能的操作是插入、删除或替换单个字符。
Levenshtein 距离算法已用于:
String.levenshtein (a plugin MooTools)
check it out: http://mootools.net/forge/p/string_levenshtein
GitHub: https://github.com/thinkphp/String.levenshtein
This method calculates Levenshtein distance between two strings. In information theory and computer science, the Levenshtein distance is a metric for measuring the amount of difference between two sequences (called edit distance). The Levenshtein distance between two strings is given by minimum number of operations needed to transform one string into another given string, where possible operations are insertion, deletion, or substitution of a single character.
The Levenshtein distance algorithm has been used in:
借助其他人的答案,我编写了一个简单的 js 函数
stringsAreSimilar
来执行此操作:piggybacking on other people's answers, I wrote a simple js function
stringsAreSimilar
to do this:所以去年我就想这么做。我读到
Levenshtein distance
是 OP 在此询问的解决方案。我看到了一个线程中的代码,但没有得到太多的支持。也许是因为它的时间复杂度(在 for 循环中有一个 for 循环)。我尝试过,它似乎对我有用。该函数返回一个分数,其中 0 表示非常匹配,另一方面,较高的分数意味着偏离接近的匹配。我将分享,也许有人可以扩展它或更好地解释它:
祝你好运并分享你对此的想法 - 那就太棒了!干杯!
So I was trying to do this last year. I read that
Levenshtein distance
is the solution for this purpose the OP asked here.I saw a code from a thread and was not given much upvotes. Maybe because of its time complexity (having a forloop inside a forloop). I tried and it seem to work for me. The function returns a score wherein 0 is a great match on the other hand a higher score means deviating from a close match. Ill share and maybe someone can extend it or explain it better:
Good luck and share your thoughts on this - would be awesome! Cheers!