Search code examples
pythonrtfpyenchant

Spelling mistakes - Pyenchant


I tried using python library for spell-check,correcting and replacing.

For some complex spelling correction, I need to have a second opinion and to see replaced word underlined or strike through.

Even if the file output is in rtf format, it is ok. How to solve it?

Efforts so far.

import enchant
    from enchant.checker import SpellChecker
    chkr = SpellChecker("en_UK","en_US")
    spacedfile = "This is a setence. It has speeelinng mistake."
    chkr.set_text(spacedfile)
    for err in chkr:
        sug = err.suggest()[0]
        err.replace(sug)
    Spellchecked = chkr.get_text()
    print Spellchecked

Output:

This is a sentence. It has spelling mistake.

Expected outcome:

This is a **sntence** sentence. It has **speeelinng** spelling mistake."

Solution

  • You just need to do the replacement including the **misspelledword** part.

    import enchant
    from enchant.checker import SpellChecker
    chkr = SpellChecker("en_UK","en_US")
    spacedfile = "This is a setence. It has speeelinng mistake."
    chkr.set_text(spacedfile)
    for err in chkr:
        sug = err.suggest()[0]
        err.replace("**%s** %s" % (err.word, sug))  # Look here
    Spellchecked = chkr.get_text()
    print Spellchecked