I am seeking for a more robust replace method in python because I am building a spellchecker to input words in ocr-context.
Let's say we have the following text in python:
text = """
this is a text, generated using optical character recognition.
this ls having a lot of errors because
the scanned pdf has too bad resolution.
Unfortunately, his text is very difficult to work with.
"""
It is easy to realize that instead of "his is a text" the right phrase would be "this is a text". And if I do text.replace('his','this') then I replace every single 'his' for this, so I would get errors like "tthis" is a text. When I do a replacement. I would like to replace the whole word 'this' and not his or this. Why not trying this?
word_to_replace='his'
corrected_word = 'this'
corrected_text = re.sub('\b'+word_to_replace+'\b',corrected_word,text)
corrected_text
Awesome, we did it, but the problem is... what if the word to correct contains an special character like '|'. For example, '|ights are on' instead of 'lights are one'. Trust me, it happened to me, the re.sub is a disaster in that case. The question is, have you encountered the same problem? Is there any method to solve this? The replacement is the most robust option. I tried text.replace(' '+word_to_replace+' ',' '+word_to_replace+' ') and this solve a lot of things but still have the problem of phrases like "his is a text " because the replacement doesnt work here since 'his' is at the begining of a sentence and not ' his ' for ' this '.
Is there any replacement method in python that takes the whole word like in regexs \b word_to_correct \b as input ?