查找两个字符串是否几乎相似 [英] Finding if two strings are almost similar

查看:56
本文介绍了查找两个字符串是否几乎相似的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道你们的字符串是否几乎相似.例如,像Mohan Mehta"这样的字符串应该匹配Mohan Mehte",反之亦然.另一个例子,像Umesh Gupta"这样的字符串应该匹配Umash Gupte".

I want to find out if you strings are almost similar. For example, string like 'Mohan Mehta' should match 'Mohan Mehte' and vice versa. Another example, string like 'Umesh Gupta' should match 'Umash Gupte'.

基本上一个字符串是正确的,另一个是拼写错误.我所有的字符串都是人名.

Basically one string is correct and other one is a mis-spelling of it. All my strings are names of people.

关于如何实现这一目标的任何建议.

Any suggestions on how to achieve this.

解决方案不一定是 100% 有效.

Solution does not have to be 100 percent effective.

推荐答案

您可以使用 difflib.sequencematcher 如果你想从标准库中得到一些东西:

You can use difflib.sequencematcher if you want something from the stdlib:

from difflib import SequenceMatcher
s_1 = 'Mohan Mehta'
s_2 = 'Mohan Mehte'
print(SequenceMatcher(a=s_1,b=s_2).ratio())
0.909090909091

fuzzywuzzy 是您可以安装的众多库之一,它使用带有 python-Levenshtein.您还应该查看 Approximate_string_matching

fuzzywuzzy is one of numerous libs that you can install, it uses the difflib module with python-Levenshtein. You should also check out the wikipage on Approximate_string_matching

这篇关于查找两个字符串是否几乎相似的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆