'Is there any way to obtain a random word from PyEnchant?

Is there a way to obtain a random word from PyEnchant's dictionaries? I've tried doing the following:

enchant.Dict("<language>").keys() #Type 'Dict' has no attribute 'keys'

list(enchant.Dict("<language>")) #Type 'Dict' is not iterable

I've also tried looking into the module to see where it gets its wordlist from but haven't had any success.

Using the separate "Random-Words" module is a workaround, but as it doesn't follow the same wordlist as PyEnchant, not all words will match. It is also quite a slow method. It is, however, the best alternative I've found so far.



Solution 1:[1]

Your question really got me curious so I thought of some way to make a random word using enchant.

import enchant
import random
import string

# here I am getting hold of all the letters

letters = string.ascii_lowercase

# crating a string with a random length with random letters

word = "".join([random.choice(letters) for _ in range(random.randint(3, 8))])

d = enchant.Dict("en_US")

# using the `enchant` to suggest a word based on the random string we provide

random_word = d.suggest(word)

Sometimes the suggest method will not return any suggestion so you will need to make a loop to check if random_word has any value.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 omercotkd