1
import enchant

def countWPP(prompt):
    #Prompt would be something like "er" or "ua"
    d = enchant.Dict("en_US")

    count = 0

    for word in d:
        if d.check(prompt) and prompt in word:
            count += 1

    return count

D is currently an object, and I'm unsure how to iterate through its dictionary. I've checked the docs, and haven't found anything helpful for this problem. The ideal output for this would be countWPP("er") = 13540 or countWPP("mt") = 1.

When trying to use the function, I got the error "'Dict' object is not iterable" indicating I need to somehow extract a list/dictionary out of the enchant dictionary, but I have no idea how.

FoxStamp
  • 11
  • 1
  • 1
    Are you sure you want to use `enchant` for this? All you need is a corpus; `enchant` seems like it's very specifically for spellchecking (and auto-suggesting) and as far as I can tell it doesn't even provide an API for exhaustively iterating over its entire language corpus. – Samwise Apr 07 '23 at 17:43
  • 1
    The documentation is [here](https://pyenchant.github.io/pyenchant/api/enchant.html#enchant.Dict) and there's no method for getting the contents. – Barmar Apr 07 '23 at 17:44
  • @Barmar I think they're looking for the number of words that *contain* the substring. Is that correct, FoxStamp? (If so, `check` doesn't do what you think it does; it only returns `True` if the argument is a complete, correctly-spelled word.) – CrazyChucky Apr 07 '23 at 17:46
  • 1
    Looks like maaaaybe you can do `d.provider.file` and then figure out how to read the file? But I maintain that what you probably want is something more like `twl` that just gives you access to a word list. Given a word list you can get what you want as just `sum(prompt in word for word in word_list)`. – Samwise Apr 07 '23 at 17:46
  • @CrazyChucky yeah, it doesn't make much sense. If the loop worked, it's basically doing "Is `prompt` a correctly spelled word, and if so, how many words is it part of?" But then the `d.check(prompt)` doesn't need to be in the loop, since it doesn't change depending on `word`. – Barmar Apr 07 '23 at 17:54
  • E.g. `father` is correctly spelled, and it's also part of `grandfather`, `father-in-law`, etc. – Barmar Apr 07 '23 at 17:54
  • @CrazyChucky, That's correct! What I've discovered is that enchant isn't the correct library for this project... – FoxStamp Apr 10 '23 at 15:36

0 Answers0