|
from spellchecker import SpellChecker |
|
import os |
|
|
|
|
|
spell = None |
|
|
|
def initialize_spell_checker(): |
|
global spell |
|
if spell is None: |
|
try: |
|
spell = SpellChecker() |
|
except Exception as e: |
|
print(f"Error initializing spell checker: {str(e)}") |
|
raise |
|
|
|
def spell_grammer(text): |
|
try: |
|
|
|
initialize_spell_checker() |
|
|
|
if not text or not isinstance(text, str): |
|
return "" |
|
|
|
|
|
words = text.split() |
|
|
|
|
|
misspelled = spell.unknown(words) |
|
|
|
|
|
corrected_words = [] |
|
for word in words: |
|
if word in misspelled: |
|
corrected_words.append(spell.correction(word)) |
|
else: |
|
corrected_words.append(word) |
|
|
|
|
|
corrected_text = ' '.join(corrected_words) |
|
return corrected_text.strip() |
|
except Exception as e: |
|
print(f"Error in spell_grammer: {str(e)}") |
|
return text |