0

Going off of this link: How to check if a word is an English word with Python?

Is there any way to see (in python) if a string of letters is contained in any word in the English language? For example, fun(wat) would return true since "water" is a word (and I'm sure there are multiple other words that contain wat) but fun(wayterlx) would be false since wayterlx is not contained in any English word. (and it is not a word itself)

Edit: A second example: d.check("blackjack") returns true but d.check("lackjac") returns false, but in the function I am looking for it would return true since it is contained in some english word.

  • what is the problem with solution from linked question? – Azat Ibrakov May 29 '17 at 01:35
  • It is saying that if I want to check to see if a string is an english word, where as I want to see if a string is a word OR is contained in any word. –  May 29 '17 at 01:37
  • The question you linked provides an answer to this post. What part of that other answer is unsuitable enough to warrant your posting an intentional duplicate of that same question? – Ken White May 29 '17 at 01:38
  • How is that an answer to my question? d.check("blackjack") returns true but d.check("lackjac") returns false, where as in the function I want it would return true since it is contained in some english word. –  May 29 '17 at 01:44

1 Answers1

2

Based on solution to the linked answer.

We can define next utility function using Dict.suggest method

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

then simply

>>> 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
Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50
  • This works in most cases, but there could be false negatives.. The string `"xylo"` is an example. This function would return `False` in the suggested answer as no word similar to `"xylo"` suggested by `.suggest(string)` contains that string, but the word xylophone contains that string. This suggested answer can only be trusted when it returns `True` but not when it returns `False` – TheIceBear May 23 '21 at 23:21