您可以使用遞歸來解決此問題。首先,你要下載一本字典的txt文件,你可以在這裏:https://github.com/Ajax12345/My-Python-Projects/blob/master/the_file.txt
dictionary = [i.strip('\n') for i in open('the_file.txt')]
def get_options(scrambled, flag, totals, last):
if flag:
return totals
else:
new_list = [i for i in dictionary if scrambled.startswith(i)]
if new_list:
possible_word = new_list[-1]
new_totals = totals
new_totals.append(possible_word)
new_scrambled = scrambled[len(possible_word):]
return get_options(new_scrambled, False, new_totals, possible_word)
else:
return get_options("", True, totals, '')
s = "specificationsinaccordancewithqualityaccreditedstandards"
print(' '.join(get_options(s, False, [], '')))
輸出:
'specifications in accordance with quality accredited standards'
是否有其他PDF轉換器,你可以試試嗎?它不應該像所有這些詞彙一起幹擾。 – sniperd
你很可能會遇到模棱兩可的問題。例如:是字符串「特定」中的第一個單詞(後跟「at」和「ion」,是唯一的,有效的單詞)還是「規範」? – Zinki
你是否試圖通過搜索字典中的所有單詞強制你的方式?很確定你可以在英文字典中找到一個包含所有單詞的圖書館。 – semicolon