我試圖在給定的文本中打印短語。我希望能夠打印文本中的每個短語,從2個單詞到文本長度允許的最大單詞數。我寫了一個下面的程序,打印所有長度最多5個字的短語,但我無法找到一個更優雅的方式來打印所有可能的短語。在給定字符串中打印所有可能的短語(單詞的連續組合)
我的短語定義=字符串中的連續詞,無論意義如何。
def phrase_builder(i):
phrase_length = 4
phrase_list = []
for x in range(0, len(i)-phrase_length):
phrase_list.append(str(i[x]) + " " + str(i[x+1]))
phrase_list.append(str(i[x]) + " " + str(i[x+1]) + " " + str(i[x+2]))
phrase_list.append(str(i[x]) + " " + str(i[x+1]) + " " + str(i[x+2]) + " " + str(i[x+3]))
phrase_list.append(str(i[x]) + " " + str(i[x+1]) + " " + str(i[x+2]) + " " + str(i[x+3]) + " " + str(i[x+4]))
return phrase_list
text = "the big fat cat sits on the mat eating a rat"
print phrase_builder(text.split())
這個輸出是:
['the big', 'the big fat', 'the big fat cat', 'the big fat cat sits',
'big fat', 'big fat cat', 'big fat cat sits', 'big fat cat sits on',
'fat cat', 'fat cat sits', 'fat cat sits on', 'fat cat sits on the',
'cat sits', 'cat sits on', 'cat sits on the', 'cat sits on the mat',
'sits on', 'sits on the', 'sits on the mat', 'sits on the mat eating',
'on the', 'on the mat', 'on the mat eating', 'on the mat eating a',
'the mat', 'the mat eating', 'the mat eating a', 'the mat eating a rat']
我希望能夠打印短語,如"the big fat cat sits on the mat eating"
和"fat cat sits on the mat eating a rat"
等
任何人都可以提供一些建議嗎?
你不是也想短語,比如'吃rat'? – TheSoundDefense
@TheSoundDefense好點。是的,我願意。 – MLadbrook