提取段落中与列表中单词相似的单词

如何解决提取段落中与列表中单词相似的单词

我有以下字符串:

"The boy went to twn and bought sausage and chicken. He then picked a tddy for his sister"

提取的单词列表:

["town","teddy","chicken","boy went"]

注意:town 和 teddy 在给定的句子中拼写错误

我尝试了以下方法,但我得到了不属于答案的其他词:

import difflib

sent = "The boy went to twn and bought sausage and chicken. He then picked a tddy for his sister"

list1 = ["town","boy went"]

[difflib.get_close_matches(x.lower().strip(),sent.split()) for x in list1 ]

我得到以下结果:

[['twn','to'],['tddy'],['chicken.','picked'],['went']]

代替:

'twn','tddy','chicken','boy went'

解决方法

文档中关于 difflib.get_closest_matches() 的通知:

difflib.get_close_matches(word,possibilities,n=3,cutoff=0.6)

返回最佳“足够好”匹配的列表。 word 是需要接近匹配的序列(通常是字符串),并且 possibilities 是与 word 匹配的序列列表 (通常是字符串列表)。

可选参数n(默认3)是要返回的最大匹配数; n 必须大于 0

可选参数 cutoff(默认 0.6)是 [0,1] 范围内的浮点数。得分至少不与 word 相似的可能性是 忽略。


目前,您使用的是默认的 ncutoff 参数。

您可以指定其中一个(或两者),以缩小返回的匹配范围。

例如,您可以使用 0.75 的 cutoff 分数:

result = [difflib.get_close_matches(x.lower().strip(),sent.split(),cutoff=0.75) for x in list1]

或者,您可以指定最多只返回 1 个匹配项:

result = [difflib.get_close_matches(x.lower().strip(),n=1) for x in list1]

在任何一种情况下,您都可以使用列表理解来展平列表列表(因为 difflib.get_close_matches() 总是返回一个列表):

matches = [r[0] for r in result]

由于您还想检查二元组的接近匹配,您可以通过提取相邻“单词”的配对并将它们作为 difflib.get_close_matches() 参数的一部分传递给 possibilities 来实现。

>

这是一个完整的工作示例:

import difflib
import re

sent = "The boy went to twn and bought sausage and chicken. He then picked a tddy for his sister"

list1 = ["town","teddy","chicken","boy went"]

# this extracts overlapping pairings of "words"
# i.e. ['The boy','boy went','went to','to twn',...
pairs = re.findall(r'(?=(\b[^ ]+ [^ ]+\b))',sent)

# we pass the sent.split() list as before
# and concatenate the new pairs list to the end of it also
result = [difflib.get_close_matches(x.lower().strip(),sent.split() + pairs,n=1) for x in list1]

matches = [r[0] for r in result]

print(matches)
# ['twn','tddy','chicken.','boy went']
,

如果你阅读了 difflib.get_close_matches() 的 Python 文档 https://docs.python.org/3/library/difflib.html 它返回所有可能的最佳匹配。 方法签名: difflib.get_close_matches(word,可能性,cutoff=0.6)

这里 n 是要返回的最大匹配数。所以我认为你可以将其作为 1 传递。

>>> [difflib.get_close_matches(x.lower().strip(),1)[0] for x in list1]
['twn','went']

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?