确定两个字符串在 Javascript 中是否相似?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3935385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine if two strings are similar in Javascript?
提问by Tegan Snyder
Let's say I have two strings, is there any way to check if they are at least 90% similar?
假设我有两个字符串,有没有办法检查它们是否至少 90% 相似?
var string1 = "theBoardmeetstoday,tomorrow51";
var string2 = "Board meets today, tomorrow";
Thanks,
谢谢,
Tegan
泰根
采纳答案by rtalbot
The wikipedia entry for Levenshtein distanceincludes a sample implementation.
Levenshtein distance的维基百科条目包括一个示例实现。
回答by RichieHindle
回答by Adrian Statescu
String.levenshtein (a plugin MooTools)
String.levenshtein(MooTools 插件)
check it out: http://mootools.net/forge/p/string_levenshtein
检查一下:http: //mootools.net/forge/p/string_levenshtein
GitHub: https://github.com/thinkphp/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.
此方法计算两个字符串之间的 Levenshtein 距离。在信息论和计算机科学中,Levenshtein 距离是衡量两个序列之间差异量(称为编辑距离)的度量。两个字符串之间的 Levenshtein 距离由将一个字符串转换为另一个给定字符串所需的最少操作数给出,其中可能的操作是插入、删除或替换单个字符。
The Levenshtein distance algorithm has been used in:
Levenshtein 距离算法已用于:
- Spell checking
- Speech recognition
- DNA analysis
- plagiarism detection
- 拼写检查
- 语音识别
- DNA分析
- 抄袭检测
回答by Karoh
Also consider Dice's Coefficientwhich is considered "mostly better" than the Levenshtein distance by the creator of the string-similaritygithub repo and its corresponding npm module.
还要考虑Dice 的系数,它被字符串相似性github 存储库及其相应的npm 模块的创建者认为比 Levenshtein 距离“更好” 。
Usage from its docs:
其文档中的用法:
var stringSimilarity = require('string-similarity');
var similarity = stringSimilarity.compareTwoStrings('healed', 'sealed');
var matches = stringSimilarity.findBestMatch('healed', ['edward', 'sealed', 'theatre']);

