簡體   English   中英

Python找到最接近的匹配句子

[英]Python find the closest matching sentence

我正在嘗試從專輯中獲取曲目列表(歌曲),對於給定的曲目,我希望獲得所有類似匹配的曲目。 我已經提到了下面的例子,關於如何在python中繼續這個的任何想法? 似乎difflib.get_close_matches僅適用於單個單詞而不是句子。

例如:(找到包含字符串'環游世界'的任何內容

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)

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)']
filter(lambda x: 'Around The World' in x, tracks)

這將為您提供名稱中包含'Around The World'的歌曲列表。 如果您使用的是Python 3,請將其強制轉換為列表( list(filter(...)) ),因為它返回了一個filter對象。

如果可能有拼寫錯誤,那么我無法幫助你。

為此,您可以為SequenceMatcher設置get_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']
>>> 

你可以這樣做

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

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

如果它與您正在閱讀的文件中的臨時值匹配,則會打印出來。

或者您可以使用正則表達式進行匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM