pythonregexspelling

Spelling correction in sentences


I use https://norvig.com/spell-correct.html to find spelling errors

How to find and replace misspelt words in a sentence.

Efforts so far:

sentences = "This sntence cntins errors. This sentence has to be corrcted."
list_string = sentences.split(' ') 
for word in list_string:
    print(correction(word))

Output:

this
sentence
contains
error
this
sentence
has
to
be
corrected.

Expected output:

This <<sntence>> sentence <<cntins>> contains errors. This sentence has to be <<corrcted>> corrected.

Able to achieve it using https://stackoverflow.com/questions/48123861/spelling-mistakes-pyenchant. How to find and replace misspelt words into original text while retaining misspelt words within << >>


Solution

  • Try str.join to a generator:

    print(' '.join('<<'+i+'>>'+' %s'%correction(i) if correction(i) != i else i for i in sentences.split()))