在python中将拼写错误的单词与正确的单词匹配 [英] Matching incorrectly spelt words with correct ones in python

查看:42
本文介绍了在python中将拼写错误的单词与正确的单词匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,该应用程序可以接收收到的短信,然后根据关键字查看该关键字是否与其正在运行的任何广告系列相关联.我现在这样做的方法是加载关键字列表和可能的拼写组合,然后当短信进来时,我查看所有关键字和组合以查看是否匹配.

I'm building an app that gets incoming SMSs, then based on a keyword, it looks to see if that keyword is associated with any campaigns that it is running. The way I'm doing it now is to load a list of keywords and possible spelling combinations, then when the SMS comes in, I look through all keywords and combinations to see if there is a match.

如果不使用这种方法,而是通过实际查找可能与另一个词匹配的词,您将如何做到这一点.

How would you do this not using this method, but by actually looking for words that might match another word.

假设正确的拼写是 HAMSTER,通常我会给活动替代方案,如 HMSTER HIMSTER HAMSTAR HAMSTR HAMSTIR 等.

Let's say the correct spelling is HAMSTER, normally I would give the campaign alternatives like HMSTER HIMSTER HAMSTAR HAMSTR HAMSTIR etc.

有没有一种聪明的方法来做到这一点?

Is there a smart way of doing this?

仓鼠

"hamstir".compare_to("hamster") ?匹配

"hamstir".compare_to("hamster") ? match

两个字怎么样?假设我们知道短信中有两个词需要匹配:

How about 2 words? Say we know there are two words that need to match in the SMS:

第一个词正确=第一个词

correct for first word = THE FIRST WORD

更正第二个单词 = AND SECOND WORD

correct for second word = AND SECOND WORD

短信 = 第一个字第二个

SMS = FIRST WORD SECOND

理想情况下,人们应该通过短信将逗号分隔的单词发送出去,这样我就会知道在哪里拆分并查找单词.

Ideally people should SMS the words comma seperated, that whay I would know where to split and look for the words.

但如果他们不这样做,比如:

But what if they dont, like :

唯一关键字 第二个参数

我如何知道单词的分割位置?第一个单词可能有 3 个单词长,第二个单词可能有 3 个或 1 个或 2 个等等.

How would I tell where the words split? The first word might be 3 words long and the second 3 or 1 or 2 etc.

在这些例子中,你会如何使用下面的技巧来找到这两个词?

In these examples, how would you use the techniques below to find the two words ?

你会看两次吗?每个需要的参数或关键字对应一个?

Would you look twice ? one for each needed parameter or keyword?

推荐答案

最简单的解决方案是使用 difflib 包,它有一个用于近似字符串匹配的 get_close_matches 函数:

The simplest solution is to use the difflib package, which has a get_close_matches function for approximate string matching:

import difflib
difflib.get_close_matches(word, possibilities)

这篇关于在python中将拼写错误的单词与正确的单词匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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