如何检查一个字符串是否包含在任何英文单词中?

How to check to see if a string is contained in any english word?

结束这个 link:How to check if a word is an English word with Python?

有什么方法可以查看(在 python 中)英文单词中是否包含一串字母?例如,fun(wat) 会 return 为真,因为 "water" 是一个词(而且我确定还有多个其他词包含 wat)但是 fun(wayterlx) 会是假的,因为 wayterlx 不是包含在任何英语单词中。 (它本身不是一个词)

编辑:第二个例子:d.check("blackjack") returns true 但 d.check("lackjac") returns false ,但在我正在寻找的函数中它会 return true 因为它包含在一些英文单词中。

基于 solution 链接的答案。

我们可以使用Dict.suggest方法定义下一个效用函数

def is_part_of_existing_word(string, words_dictionary):
    suggestions = words_dictionary.suggest(string)
    return any(string in suggestion
               for suggestion in suggestions)

那么简单

>>> import enchant
>>> english_dictionary = enchant.Dict("en")
>>> is_part_of_existing_word('wat', words_dictionary=english_dictionary)
True
>>> is_part_of_existing_word('wate', words_dictionary=english_dictionary)
True
>>> is_part_of_existing_word('way', words_dictionary=english_dictionary)
True
>>> is_part_of_existing_word('wayt', words_dictionary=english_dictionary)
False
>>> is_part_of_existing_word('wayter', words_dictionary=english_dictionary)
False
>>> is_part_of_existing_word('wayterlx', words_dictionary=english_dictionary)
False
>>> is_part_of_existing_word('lackjack', words_dictionary=english_dictionary)
True
>>> is_part_of_existing_word('ucumber', words_dictionary=english_dictionary)
True