| def correct_grammar(text): | |
| import re | |
| from string import punctuation | |
| contractions = { | |
| "cant": "can't", | |
| "wont": "won't", | |
| "dont": "don't", | |
| "doesnt": "doesn't", | |
| "didnt": "didn't", | |
| "isnt": "isn't", | |
| "arent": "aren't", | |
| "wasnt": "wasn't", | |
| "werent": "weren't", | |
| "havent": "haven't", | |
| "hasnt": "hasn't", | |
| "hadnt": "hadn't", | |
| "im": "I'm", | |
| "youre": "you're", | |
| "hes": "he's", | |
| "shes": "she's", | |
| "its": "it's", | |
| "theyre": "they're", | |
| "ive": "I've", | |
| "weve": "we've", | |
| "youve": "you've", | |
| "wouldve": "would've", | |
| "couldve": "could've", | |
| "shouldve": "should've", | |
| "thats": "that's", | |
| "theres": "there's", | |
| "heres": "here's", | |
| "whos": "who's", | |
| "whats": "what's", | |
| "wheres": "where's", | |
| "whens": "when's", | |
| "whys": "why's", | |
| "hows": "how's" | |
| } | |
| def fix_contractions(text): | |
| for wrong, correct in contractions.items(): | |
| text = re.sub(r'\b' + wrong + r'\b', correct, text, flags=re.IGNORECASE) | |
| return text | |
| def fix_capitalization(text): | |
| sentences = re.split(r'(?<=[.!?])\s+', text.strip()) | |
| sentences = [s[0].upper() + s[1:] if s else s for s in sentences] | |
| return ' '.join(sentences) | |
| def fix_punctuation_spacing(text): | |
| text = re.sub(r'\s+([,.!?;])', r'\1', text) | |
| text = re.sub(r'([,.!?;])\s*([,.!?;])', r'\1 \2', text) | |
| text = re.sub(r'\s*([\(])\s*', r' \1', text) | |
| text = re.sub(r'\s*([\)])', r'\1', text) | |
| return text | |
| def fix_possessives(text): | |
| text = re.sub(r'\b(\w+)s\b', r"\1's", text) | |
| text = re.sub(r"\b(\w+)'s\s+(own|house|car|book|dog|cat|child|children)\b", r"\1's \2", text) | |
| return text | |
| def fix_common_mistakes(text): | |
| common_errors = { | |
| "your": "you're", | |
| "there": "their", | |
| "then": "than", | |
| "loose": "lose", | |
| "effect": "affect", | |
| "to": "too", | |
| "alot": "a lot", | |
| "wierd": "weird", | |
| "definately": "definitely", | |
| "seperate": "separate" | |
| } | |
| for wrong, correct in common_errors.items(): | |
| text = re.sub(r'\b' + wrong + r'\b', correct, text, flags=re.IGNORECASE) | |
| return text | |
| def ensure_sentence_ends(text): | |
| if text and text[-1] not in '.!?': | |
| text += '.' | |
| return text | |
| def remove_extra_spaces(text): | |
| text = re.sub(r'\s+', ' ', text) | |
| return text.strip() | |
| def fix_quotation_marks(text): | |
| text = re.sub(r'"([^"]*)"', r'"\1"', text) | |
| text = re.sub(r'\'([^\']*)\'', r'"\1"', text) | |
| return text | |
| def fix_subject_verb_agreement(text): | |
| text = re.sub(r'\b(he|she|it)\s+(go|run|jump|sing|dance)\b', r'\1 \2es', text, flags=re.IGNORECASE) | |
| text = re.sub(r'\b(I|you|we|they)\s+(goes|runs|jumps|sings|dances)\b', r'\1 \2', text, flags=re.IGNORECASE) | |
| return text | |
| if not text or not isinstance(text, str): | |
| return "" | |
| text = fix_contractions(text) | |
| text = fix_capitalization(text) | |
| text = fix_punctuation_spacing(text) | |
| text = fix_possessives(text) | |
| text = fix_common_mistakes(text) | |
| text = fix_quotation_marks(text) | |
| text = fix_subject_verb_agreement(text) | |
| text = ensure_sentence_ends(text) | |
| text = remove_extra_spaces(text) | |
| return text |