正则表达式:搜索多个部分的多个单词

时间:2012-06-07 00:22:04

标签: python regex expression

我正在尝试实现搜索包含多个部分的多个单词的集合。 例如,我们有这些医学术语。

R Deep Transverse Metatarsal Ligament 4 GEODE
R Distal JointCapsule 1 GEODE
R Dorsal Calcaneocuboid Ligament GEODE
R Dorsal Carpometacarpal Ligament 2 GEODE
R Dorsal Cuboideavicular Ligament GEODE
R Dorsal Tarsometatarsal Ligament 5 GEODE
R Elbow Capsule GEODE
R F Distal JointCapsule 1 GEODE
R Fibular Collateral Bursa GEODE
R Fibular Collateral Ligament GEODE
R Fibular Ligament GEODE

用户可以输入以下搜索字词:

例如,“R De Me Li”然后应该找到“R Deep Transverse Metatarsal Ligament 4 GEODE”

例如,“Fi Colla”==> “R Fibular Collat​​eral Bursa GEODE”,“R Fibular Collat​​eral Ligament GEODE”

例如,“bow ODE”==> “R Elbow Capsule GEODE”

即使用户输入单词的某些部分,它也应该找到答案。 如果有多个答案,则应显示全部。 我提前感谢您的帮助。

补充)哦..我忘记了什么。

例如,“ral lar”==>它不应显示“R Fibular Collat​​eral Bursa GEODE”或“R Fibular Collat​​eral Ligament GEODE”,因为应该考虑查询词的顺序。

此外,查询词之间的空格表示每行(数据库)的不同单词。

查询字的顺序应与每行(数据库)的字相同, 但查询单词可能比数据库单词短。

例如,“R De Me 4”==> “R Deep Transverse Metatarsal Ligament 4 GEODE” 在那里我们可以看到'跖骨'和'韧带'包括'我',但与'跖骨'的第一场比赛很好,并且将搜索4。

此外,查询词的不同组合可以返回相同的结果。

e,g。,

'Car'==> 'R Dorsal Carpometacarpal Ligament 2 GEODE'

'做车'==> 'R Dorsal Carpometacarpal Ligament 2 GEODE'

'R Do Carp'==> 'R Dorsal Carpometacarpal Ligament 2 GEODE'

注意:不区分大小写。

4 个答案:

答案 0 :(得分:4)

您可以使用标准发行版中的difflib执行此操作:

import difflib

s="""R Deep Transverse Metatarsal Ligament 4 GEODE
R Distal JointCapsule 1 GEODE
R Dorsal Calcaneocuboid Ligament GEODE
R Dorsal Carpometacarpal Ligament 2 GEODE
R Dorsal Cuboideavicular Ligament GEODE
R Dorsal Tarsometatarsal Ligament 5 GEODE
R Elbow Capsule GEODE
R F Distal JointCapsule 1 GEODE
R Fibular Collateral Bursa GEODE
R Fibular Collateral Ligament GEODE
R Fibular Ligament GEODE""".split('\n')

qs="""R De Me Li
Fi Colla
bow ODE""".split('\n')

for q in qs:
    print "results for '{}':".format(q)
    matches=difflib.get_close_matches(q,s,3,0.3)
    for i,e in enumerate(matches,1):
        print "\t{}. {}".format(i,e)

打印:

results for 'R De Me Li':
    1. R Deep Transverse Metatarsal Ligament 4 GEODE
    2. R Dorsal Calcaneocuboid Ligament GEODE
    3. R Dorsal Cuboideavicular Ligament GEODE
results for 'Fi Colla':
    1. R Fibular Collateral Bursa GEODE
    2. R Fibular Collateral Ligament GEODE
results for 'bow ODE':
    1. R Elbow Capsule GEODE

结合使用cblab's answer将正则表达式与difflib相结合,你可以得到:

s="""R Deep Transverse Metatarsal Ligament 4 GEODE
R Distal JointCapsule 1 GEODE
R Dorsal Calcaneocuboid Ligament GEODE
R Dorsal Carpometacarpal Ligament 2 GEODE
R Dorsal Cuboideavicular Ligament GEODE
R Dorsal Tarsometatarsal Ligament 5 GEODE
R Elbow Capsule GEODE
R F Distal JointCapsule 1 GEODE
R Fibular Collateral Bursa GEODE
R Fibular Collateral Ligament GEODE
R Fibular Ligament GEODE""".split('\n')
s=set(s)
qs="""R De Me Li
Fi Colla
bow ODE
Car
Do Car
ral lar
R De Me 4
R Do Carp""".split('\n')

for q in sorted(qs):
    print "results for '{}':".format(q)
    pattern = r'.*' + re.sub(r'\W', '.*', q.strip()) + '.*'
    matches=[item for item in s if re.match(pattern, item, re.I)]
    for e in difflib.get_close_matches(q,s,3,0.33):
        if e not in matches: 
            matches.append(e)

    for i,e in enumerate(matches,1):
        print "\t{}. {}".format(i,e)
    else:
        if len(matches)==0:
            print "\tNo matches"    

打印:

results for 'Car':
    1. R Dorsal Carpometacarpal Ligament 2 GEODE
results for 'Do Car':
    1. R Dorsal Carpometacarpal Ligament 2 GEODE
results for 'Fi Colla':
    1. R Fibular Collateral Bursa GEODE
    2. R Fibular Collateral Ligament GEODE
results for 'R De Me 4':
    1. R Deep Transverse Metatarsal Ligament 4 GEODE
results for 'R De Me Li':
    1. R Deep Transverse Metatarsal Ligament 4 GEODE
    2. R Dorsal Calcaneocuboid Ligament GEODE
results for 'R Do Carp':
    1. R Dorsal Carpometacarpal Ligament 2 GEODE
    2. R Elbow Capsule GEODE
    3. R Distal JointCapsule 1 GEODE
results for 'bow ODE':
    1. R Elbow Capsule GEODE
results for 'ral lar':
    No matches

答案 1 :(得分:3)

一个简单的pythonic解决方案,可以完成工作并且不区分大小写

import re

def search(request, base):
    pattern = r'.*' + re.sub(r'\W', '.*', request.strip()) + '.*'
    return [item for item in base if re.match(pattern, item, re.I)]

基本上,我们创建一个简单的正则表达式,它匹配任何包含请求的所有子串的字符串(所有由非单词字符分隔的字符串),以及之前和之后的任何内容。 / p>

例如,请求'R De Me Li'变为模式r'.*R.*De.*Me.Li.*'

然后,我们返回所有匹配结果的列表。由于re.I中的标记re.match(),它不区分大小写。

然后,它按预期工作,您可以尝试使用基础:

>>> base = ['R Deep Transverse Metatarsal Ligament 4 GEODE',
'R Distal JointCapsule 1 GEODE',
'R Dorsal Calcaneocuboid Ligament GEODE',
'R Dorsal Carpometacarpal Ligament 2 GEODE',
'R Dorsal Cuboideavicular Ligament GEODE',
'R Dorsal Tarsometatarsal Ligament 5 GEODE',
'R Elbow Capsule GEODE',
'R F Distal JointCapsule 1 GEODE',
'R Fibular Collateral Bursa GEODE',
'R Fibular Collateral Ligament GEODE',
'R Fibular Ligament GEODE']

一些示例请求:

>>> search('R De Me Li', base)
['R Deep Transverse Metatarsal Ligament 4 GEODE']
>>> search('Fi Colla', base)
['R Fibular Collateral Bursa GEODE', 'R Fibular Collateral Ligament GEODE']
>>> search('bow ODE', base)
['R Elbow Capsule GEODE']
>>> search('Car', base)
['R Dorsal Carpometacarpal Ligament 2 GEODE']
>>> search('F', base)
['R F Distal JointCapsule 1 GEODE', 'R Fibular Collateral Bursa GEODE', 'R Fibular Collateral Ligament GEODE', 'R Fibular Ligament GEODE']
>>> search('F Ca', base)
['R F Distal JointCapsule 1 GEODE']
>>> search('F Co', base)
['R Fibular Collateral Bursa GEODE', 'R Fibular Collateral Ligament GEODE']

注意:只有在请求和项目中的订单相同时才会匹配(即'ode bow',因为请求与['R Elbow Capsule GEODE']不匹配, 'bow ode'会)。

注意:我不认为模糊搜索在这里会有很多帮助,至少在第一时间,因为它基于距离,如Levenshtein(编辑距离),比如'Fi'和'Fibular'之间的距离会非常大(7个单词的距离为5 ... ... 35%我不喜欢匹配...你可以使用它你很确定这个请求只包含可能很少有错误输入的完整单词)

答案 2 :(得分:1)

不是真正的“正则表达”问题;你应该看看字符串的模糊比较,即Levenshtein距离或差异。

请参阅https://stackoverflow.com/questions/682367/good-python-modules-for-fuzzy-string-comparison

修改:一些示例代码:

import Levenshtein

base_strings = [
    "R Deep Transverse Metatarsal Ligament 4 GEODE",
    "R Distal JointCapsule 1 GEODE",
    "R Dorsal Calcaneocuboid Ligament GEODE",
    "R Dorsal Carpometacarpal Ligament 2 GEODE",
    "R Dorsal Cuboideavicular Ligament GEODE",
    "R Dorsal Tarsometatarsal Ligament 5 GEODE",
    "R Elbow Capsule GEODE",
    "R F Distal JointCapsule 1 GEODE",
    "R Fibular Collateral Bursa GEODE",
    "R Fibular Collateral Ligament GEODE",
    "R Fibular Ligament GEODE"
]

def main():
    print("Medical term matcher:")
    while True:
        t = raw_input('Match what? ').strip()
        if len(t):
            print("Best match: {}".format(sorted(base_strings, key = lambda x: Levenshtein.ratio(x, t), reverse=True)[0]))
        else:
            break

if __name__=="__main__":
    main()

实际输出:

Medical term matcher:
Match what? R De Me Li
Best match: R Deep Transverse Metatarsal Ligament 4 GEODE
Match what? Fi Colla
Best match: R Fibular Collateral Bursa GEODE
Match what? bow ODE
Best match: R Elbow Capsule GEODE
Match what? 

编辑2 “如果有多个答案,则应显示全部” - 基础字符串所有答案到不同程度。那么,问题是你想要使用什么样的相似值 - 值;也许类似“所有答案至少90%和最佳匹配一样好”?

答案 3 :(得分:1)

当您将所有粒子(输入中由空格分隔的字符串片段)存在于结果中时,以下代码会使您想要考虑“匹配”。我在示例中使用了循环,但当然您应该调整它以使用raw_input

虽然它使用正则表达式(允许多个空格),但使用的主要功能是if particle in line

import re

entry = """R Deep Transverse Metatarsal Ligament 4 GEODE
R Distal JointCapsule 1 GEODE
R Dorsal Calcaneocuboid Ligament GEODE
R Dorsal Carpometacarpal Ligament 2 GEODE
R Dorsal Cuboideavicular Ligament GEODE
R Dorsal Tarsometatarsal Ligament 5 GEODE
R Elbow Capsule GEODE
R F Distal JointCapsule 1 GEODE
R Fibular Collateral Bursa GEODE
R Fibular Collateral Ligament GEODE
R Fibular Ligament GEODE
"""

searches = """R De Me Li
Fi Colla
bow ODE"""

for search in searches.split('\n'):
    print search, ':'
    termlist = re.split('\s', search)
    for line in entry.split('\n'):
        match = True
        for term in termlist:
            if not term in line:
                match = False
        if match:
            print '\t', line
    print