考虑下面的例子:
例1:
str1 = "wow...it looks amazing"
str2 = "looks amazi"
你看amazi
接近amazing
,str2
输入错误,我想写一个程序,告诉我amazi
接近amazing
,然后在str2
中,我将amazi
替换为amazing
例2:
str1 = "is looking good"
str2 = "looks goo"
在这种情况下,更新的str2
将是"looking good"
例3:
str1 = "you are really looking good"
str2 = "lok goo"
在这种情况下str2
将是"good"
,因为lok
不接近looking
(或者即使程序在这种情况下可以将lok
转换为looking
,那么我的问题的解决方案也没有问题)
例4:
str1 = "Stu is actually SEVERLY sunburnt....it hurts!!!"
str2 = "hurts!!"
更新的str2
将"hurts!!!"
例5:
str1 = "you guys were absolutely amazing tonight, a..."
str2 = "ly amazin"
更新的str2
将被"amazing"
,"ly"
将被删除或替换为绝对
这个的算法和代码是什么
也许我们可以通过按字典顺序查看字符并设置
阈值类似于0.8或80%,所以如果word2
从str1
中获得80%的word1
序列字符,那么我们将str2
中的word2
替换为str1
的单词?
有没有其他有效的python代码解决方案
有很多方法可以做到这一点。这个解决了你所有的例子。我添加了一个最小相似性过滤器,只返回高质量的匹配。这就是允许在最后一个示例中删除“ly”的原因,因为它并不完全关闭任何单词
Documentation
您可以使用
pip install python-Levenshtein
安装levenshtein你提议的每个样品
我用正则表达式完成了它
像这样:
输出:
使用OP给出的条件进行更新:
相关问题 更多 >
编程相关推荐