您可以按长度降序创建每个短语的“部分”,然后在文本块中找到这些部分。
例如
>>> text = "Marshmallows are delicious. I've been snacking on them while the wizards assaulted the fort. The unicorns sign wonderful melodies of those who forgot their spell books at home."
>>> phrase='Giant unicorns sign wonderful melodies of the imminent apocalypse'
>>> words = phrase.split()
>>> parts = list()
>>> for length in range(len(words),3,-1): #Assuming a part is atleast 3 words
for start in range(0,len(words)-length + 1):
parts.append(' '.join(words[start:start+length]))
>>> #A step of -1 ensures the list is sorted in a decreasing order of length.
>>> parts
['Giant unicorns sign wonderful melodies of the imminent apocalypse', 'Giant unicorns sign wonderful melodies of the imminent', 'unicorns sign wonderful melodies of the imminent apocalypse', 'Giant unicorns sign wonderful melodies of the', 'unicorns sign wonderful melodies of the imminent', 'sign wonderful melodies of the imminent apocalypse', 'Giant unicorns sign wonderful melodies of', 'unicorns sign wonderful melodies of the', 'sign wonderful melodies of the imminent', 'wonderful melodies of the imminent apocalypse', 'Giant unicorns sign wonderful melodies', 'unicorns sign wonderful melodies of', 'sign wonderful melodies of the', 'wonderful melodies of the imminent', 'melodies of the imminent apocalypse', 'Giant unicorns sign wonderful', 'unicorns sign wonderful melodies', 'sign wonderful melodies of', 'wonderful melodies of the', 'melodies of the imminent', 'of the imminent apocalypse']
>>> for part in parts:
if part.lower() in text.lower(): #for case insensitivity
found = part
break
>>> found
'unicorns sign wonderful melodies of'