C++字符串比较
可能的重复:
与最相似的字符串进行比较
我想知道最好的方法是什么比较两个字符串的(一定百分比)相似度是。例如:字符串 1 是“我真的很喜欢吃馅饼”,字符串 2 是“我真的很喜欢吃奶酪”,函数返回“true”,因为超过 50% 的字符相似。
我想我可以看看一个字符串中的每个字符是否在另一个字符串中的某个位置,但可能有一种更精确的方法来解决问题。有什么建议吗?
Possible Duplicate:
string comparison with the most similar string
I was wondering what the best way to go about comparing two strings for (For a certain percentage of) similarity is. EX: String 1 is "I really like to eat pie," and String 2 is "I really like to eat cheese," with a function returning "true" because more than 50% of the characters are similar.
I was thinking that I could see if each character in one string is somewhere in the other, but there's probably a more precise way to go about things. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Levenshtein 距离 可能比较合适。它告诉我们必须进行多少次单字符插入、删除或替换才能将一个字符串转换为另一个字符串。您还可以为这三个操作指定不同的优先级。
Levenshtein distance might be suitable. It tells how many single-character insertions, deletions or replacements must be made in order to transform one string into the other. You can also give different priorities to the three operations.
对于像这样的模糊比较,您可以将每个字符串拆分为单词(使用 strtok()),并使用 stricmp() 比较两个不区分大小写的单词数组。还有 SOUNDEX 算法来比较单词,看看它们听起来是否相同。
For a fuzzy compare like this you could split each string up into words (using strtok()) and compare the two word arrays case-insensitive using stricmp(). There is also the SOUNDEX algorithm to compare words to see if they sound the same.