Python寻找最接近的匹配句子

4
我正在尝试从专辑中获取曲目列表,并为给定的曲目获取所有匹配的曲目。我已经在下面提供了示例,请问如何在Python中处理?似乎difflib.get_close_matches仅适用于单词而不是句子。
示例:(查找包含字符串“Around the world”的任何内容)
tracks = ['Around The World (La La La La La) (Radio Version)', 'Around The World (La La La La La) (Alternative Radio Version)', 'Around The World (La La La La La) (Acoustic Mix)', 'Around The World (La La La La La) (Rucegsegger#Wittwer Club Mix)', 'World In Motion','My Heart Beats Like A Drum (Dam Dam Dam)','Thinking Of You','Why Oh Why','Mistake No. 2','With You','Love Is Blind','Lonesome Suite','Let Me Come & Let Me Go']

输出:

 Around The World (La La La La La) (Radio Version)
 Around The World (La La La La La) (Alternative Radio Version)
 Around The World (La La La La La) (Acoustic Mix)
 Around The World (La La La La La) (Rüegsegger#Wittwer Club Mix)
4个回答

8

difflib.get_close_matches 可以处理字符串(而不仅仅是单词)。在这种情况下,您需要降低截断值(默认值为0.6),并增加 n,即最大匹配数:

In [19]: import difflib

In [20]: tracks = ['Around The World (La La La La La) (Radio Version)', 'Around The World (La La La La La) (Alternative Radio Version)', 'Around The World (La La La La La) (Acoustic Mix)', 'Around The World (La La La La La) (Rucegsegger#Wittwer Club Mix)', 'World In Motion','My Heart Beats Like A Drum (Dam Dam Dam)','Thinking Of You','Why Oh Why','Mistake No. 2','With You','Love Is Blind','Lonesome Suite','Let Me Come & Let Me Go']

In [21]: difflib.get_close_matches('Around the world', tracks, n = 4,cutoff = 0.3)
Out[21]: 
['Around The World (La La La La La) (Acoustic Mix)',
 'Around The World (La La La La La) (Radio Version)',
 'Around The World (La La La La La) (Alternative Radio Version)',
 'Around The World (La La La La La) (Rucegsegger#Wittwer Club Mix)']

2
filter(lambda x: 'Around The World' in x, tracks)

这将为您提供名称中包含“Around The World”的歌曲列表。如果您使用的是Python 3,请将其转换为列表(list(filter(...))),因为它返回一个filter对象。如果可能存在拼写错误,则无法帮助您解决问题。

非常感谢,正如您所注意到的,我是Python的新手,仍在逐渐掌握。 - Prem Minister

1
你可以利用SequenceMatcherget_matching_blocks来实现这个目的。
>>> from pprint import PrettyPrinter
>>> from difflib import SequenceMatcher
>>> pp = PrettyPrinter(indent = 4)
>>> pp.pprint(tracks)
[   'World In Motion',
    'With You',
    'Why Oh Why',
    'Thinking Of You',
    'My Heart Beats Like A Drum (Dam Dam Dam)',
    'Mistake No. 2',
    'Love Is Blind',
    'Lonesome Suite',
    'Let Me Come & Let Me Go',
    'Around The World (La La La La La) (Rucegsegger#Wittwer Club Mix)',
    'Around The World (La La La La La) (Radio Version)',
    'Around The World (La La La La La) (Alternative Radio Version)',
    'Around The World (La La La La La) (Acoustic Mix)']
>>> seq = ((e, SequenceMatcher(None, 'Around the world', e).get_matching_blocks()[0]) for e in tracks)
>>> seq = [k for k, _ in sorted(seq, key = lambda e:e[-1].size, reverse = True)]
>>> pp.pprint(seq)
[   'Around The World (La La La La La) (Rucegsegger#Wittwer Club Mix)',
    'Around The World (La La La La La) (Radio Version)',
    'Around The World (La La La La La) (Alternative Radio Version)',
    'Around The World (La La La La La) (Acoustic Mix)',
    'World In Motion',
    'With You',
    'Thinking Of You',
    'Why Oh Why',
    'My Heart Beats Like A Drum (Dam Dam Dam)',
    'Mistake No. 2',
    'Love Is Blind',
    'Lonesome Suite',
    'Let Me Come & Let Me Go']
>>> 

-2

你可以这样做。

temp = "Around The World (La La La La La)"

for string in fh.readlines():
    if temp in string:
       print temp

如果它与您正在读取的任何文件的临时文件匹配,那么它将打印出来。

或者您可以使用正则表达式进行匹配。


这段代码无法工作,因为您命名的变量“str”已经是Python序列类型。永远不要以序列类型命名变量。 - m13op22
另外,它没有起作用是因为语法错误。你忘记在forif语句后面加上:了。 - m13op22

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接