answer-grading-app / HTR /spell_and_gramer_check.py
yamanavijayavardhan's picture
fix req 3
7c75354
import re
import string
import unicodedata
from utils import notification_queue, log_print
def spell_grammer(text):
try:
# Basic text cleaning
text = text.strip()
text = re.sub(r'\s+', ' ', text)
# Remove special characters except basic punctuation
text = ''.join(c for c in text if c.isalnum() or c.isspace() or c in string.punctuation)
# Normalize unicode characters
text = unicodedata.normalize('NFKC', text)
# Basic grammar fixes
text = re.sub(r'\s+([.,!?])', r'\1', text) # Remove spaces before punctuation
text = re.sub(r'([.,!?])\s*([A-Z])', r'\1 \2', text) # Add space after punctuation
# Capitalize first letter of sentences
sentences = re.split(r'([.!?]+)', text)
text = ''
for i in range(0, len(sentences)-1, 2):
text += sentences[i].strip().capitalize() + sentences[i+1] + ' '
if len(sentences) % 2:
text += sentences[-1].strip().capitalize()
return text.strip()
except Exception as e:
error_msg = str(e)
notification_queue.put({
"type": "error",
"message": f"Error in spell checking: {error_msg}"
})
return text