Search code examples
pythoncpu-wordpyenchant

Check if collection of words (pyenchant)


I want to check if a string in Python is a collection of words using PyEnchant.
For example, I want to somehow check a concatenated string is a word or not:

eng = enchant.Dict("en_US")
eng.check("Applebanana")

I know this will return false, but I want it to return true, since Apple + banana are legit words by PyEnchant.


Solution

  • If you limit yourself to words combined from two other words, you can check the combinations yourself:

    >>> s = "applebanana"
    >>> splits = [(s[:i], s[i:]) for i in range(1,len(s))]
    >>> splits
    [('a', 'pplebanana'), ('ap', 'plebanana'), ('app', 'lebanana'), 
     ('appl', 'ebanana'), ('apple', 'banana'), ('appleb', 'anana'), 
     ('appleba', 'nana'), ('appleban', 'ana'), ('applebana', 'na'), 
    ('applebanan', 'a')]
    >>> any((eng.check(item[0]) and eng.check(item[1])) for item in splits)
    True
    

    Of course you can expand that to more than two, but this should give you a general idea of where you're headed.