Python – Spelling mistake – Pyenchant

Spelling mistake – Pyenchant… here is a solution to the problem.

Spelling mistake – Pyenchant

I tried using the python library for spell checking, correction, and replacement.

For some complex spelling corrections, I need to have a second opinion and see the replaced words underlined or strikethrough.

Even if the file output is in RTF format. 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 Results:

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

Solution

You just need to make the substitution, including the MisspelledWord section.

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

Related Problems and Solutions