pythonpython-3.xpyenchantenchant

IndexError: list index out of range - enchant


I'm trying to use python to correct the spelling of a large corpus (about 100000 phrases):

Having bought a large carpet I was expecting a large package accordingly and surprised by its very small size I wanted to verify that the content was consistent with my order and the driver did not allow it, it is nevertheless recommend in the order?

Lower the sound of the ambient music ... we come for this quote cocooning resting when we come in this type of store deco, we do not want to find the supermarket atmosphere of the deco. Have the products in stock qu we can leave with.

Carrying caddies of the staff working in the shop in full passage everywhere in the stores without nobody takes care of it and which generate traffic and the general vision Take example on the shop of the forum

Package heavy and not easy to wear for a single woman: I parked at the door of the store and when I asked if anyone could help me was answered: no, we have no right to leave the store in case we have an acciden!!!.

Embed a search not refer to the website or note the name of the product on the label Car I needed to know the dimensions of the product but very long to find on the site because the reference did not allow me to find the object I bought some time ago a damaged product (bad for a painting) but since the time that I wanted it .... and I asked if it was the last copy because it was b annais to buy a product abime, I was sent to bolt a force!

...

Script:

import enchant
from enchant.checker import SpellChecker

language = SpellChecker('en_US') # ou simplement 'en'
language.set_text(text)
for error in language:
    correction = error.suggest()[0]
    error.replace("%s" %(correction))
correcteur = language.get_text()
print (correcteur)

The program works, but stops at a time with the error:

Traceback (most recent call last):

File "correction.py", line 4, in correction = error.suggest()[0] IndexError: list index out of range


Solution

  • Just check that suggest has at least one item: you can have an error without a proposed correction:

    for error in language:
        if len(error.suggest())>0: 
            correction = error.suggest()[0]
            error.replace("%s" %(correction))