File size: 1,297 Bytes
7c75354
 
 
 
2795ce6
 
7c75354
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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