如何从文本文件中提取自定义实体列表?

How to extract a custom list of entities from a text file?

我有一个看起来像这样的实体列表:

["Bluechoice HMO/POS", "Pathway X HMO/PPO", "HMO", "Indemnity/Traditional Health Plan/Standard"]

这不是详尽的列表,还有其他类似的条目。

我想从文本文件(包含超过 30 页的信息)中提取这些实体(如果存在)。这里的关键是这个文本文件是使用 OCR 生成的,因此可能不包含确切的条目。也就是说,例如,它可能有:

"Out of all the entries the user made, BIueChoise HMOIPOS is the most prominent"

注意“BIueChoise HMOIPOS”中的拼写错误w.r.t。 “蓝色选择 HMO/POS”。

我想要那些出现在文本文件中的实体,即使相应的词不完全匹配。

欢迎任何帮助,无论是算法还是方法。非常感谢!

您可以使用可以近似匹配字符串并确定它们相似程度的算法来实现这一点,例如 Levenshtein distance, Hamming distance, Cosine similarity,等等。

textdistance 是一个包含大量此类算法的模块,您可以使用它们。检查一下 here.

我遇到了类似的问题,我使用 textdistance 通过从文本文件中选择长度等于我需要 search/extract 的字符串的子字符串来解决,然后使用其中一种算法来查看哪个算法解决了我的问题。 对我来说,当我过滤掉在 75%.

以上模糊匹配的字符串时,余弦相似度 给了我最好的结果

以你问题中的“Bluechoice HMO/POS”为例给你一个想法,我应用如下:

>>> import textdistance
>>>
>>> search_strg = "Bluechoice HMO/POS"
>>> text_file_strg = "Out of all the entries the user made, BIueChoise HMOIPOS is the most prominent"
>>>
>>> extracted_strgs = []
>>> for substr in [text_file_strg[i:i+len(search_strg)] for i in range(0,len(text_file_strg) - len(search_strg)+1)]:
...     if textdistance.cosine(substr, search_strg) > 0.75:
...             extracted_strgs.append(substr)
... 
>>> extracted_strgs
['BIueChoise HMOIPOS']