如何在 Python 中的较长字符串中找到字符串的所有精确出现或紧密匹配?

问题描述 投票:0回答:1

目标:

  • 我想在 Python 中的较长字符串中查找字符串的所有精确出现或紧密匹配。
  • 我还想知道这些出现在较长字符串中的位置。
  • 为了定义什么是接近匹配,我想设置一个阈值,例如如果使用编辑距离作为度量,则为编辑次数。
  • 我还希望代码给出匹配分数(可能用于确定候选子字符串是否超过我设置的匹配阈值)。

如何在 Python 中执行此操作?


示例:

long_string = """1. Bob likes classical music very much.
2. This is classic music!
3. This is a classic musical. It has a lot of classical musics.
"""

query_string = "classical music"

我希望Python代码能够根据我设置的字符串匹配阈值找到“古典音乐”以及可能的“经典音乐”、“经典音乐”和“古典音乐”。


研究:我发现在Python中检查较长字符串中存在的模糊/近似子字符串?但问题仅关注最佳匹配(即并非所有出现),答案要么也关注最佳匹配,要么不关注处理多单词查询字符串(因为问题只有一个单词查询字符串,或者返回一些不正确的分数(即使精确匹配也得不到完美分数)。

python string-matching fuzzy-search
1个回答
0
投票

这是迄今为止我的薄弱解决方案:

import regex
long_string = """1. Bob likes classical music very much.
2. This is classic music!
3. This is a classic musical. It has a lot of classical musics.
"""

query_string = "classical music"
threshold = 5

results = regex.finditer(r'(classical music){e<5}', long_string, flags=regex.IGNORECASE)

for result in results:
    print(result)

输出:

<regex.Match object; span=(9, 28), match='kes classical music', fuzzy_counts=(0, 4, 0)>
<regex.Match object; span=(49, 64), match='s classic music', fuzzy_counts=(0, 2, 2)>
<regex.Match object; span=(77, 92), match='a classic music', fuzzy_counts=(0, 2, 2)>
<regex.Match object; span=(108, 127), match=' of classical music', fuzzy_counts=(0, 4, 0)>

2个弱点:

  1. 不使用
    query_string
    threshold
    ,而是在正则表达式查询中对它们进行硬编码。
  2. 没有获得最佳匹配,而是获得有 4 个错误的匹配。
© www.soinside.com 2019 - 2024. All rights reserved.