fix(critical): stop spelling from corrupting correct words + fix pronoun agreement + reject hallucinations
Browse files3 critical fixes:
1. IV PROTECTION: When both original AND correction are in-vocabulary (valid
Arabic words), ONLY accept the change if it's a known orthographic fix
(hamza whitelist or ه→ة). This blocks وكان→وكأن type corruption where
the model changes one correct word to a completely different correct word.
2. PRONOUN EXCLUSION: fix_subject_verb_agreement now excludes pronouns
(أنا, أنت, هو, etc.) from triggering plural verb agreement. Previously
it incorrectly changed أنا ذهبت → أنا ذهبتوا.
3. HALLUCINATION FILTER: Grammar diffs with Jaccard char similarity <0.3
are rejected (e.g. جالس→جاكسون). Prevents model hallucinations from
reaching the user.
Also adds [SPELLING] Accepted/Rejected debug logging for production tracing.
59/59 tests passing
Files changed (2)
- src/app.py+57-5
- src/nlp/grammar/grammar_rules.py+39-15
733 unmodified lines73473573673773873974074174274315 unmodified lines759760761762763764189 unmodified lines9549559569579589599601 unmodified line96296396496596696721 unmodified lines98999099199299399499562 unmodified lines10581059106010611062106310641065106610671068106910701071733 unmodified linesreturn dp[m][n]def _is_small_spelling_change(orig_word, corr_word):"""Heuristic: only accept small spelling edits and ignoreaggressive changes (to avoid over-editing)."""if not orig_word or not corr_word:return False15 unmodified linesif corr_word == orig_word[:-1] or len(corr_word) < len(orig_word):return Falsedist = _levenshtein(orig_word, corr_word)max_len = max(len(orig_word), len(corr_word))189 unmodified lines# 1-word → 1-word: accept only small edits (typos)o_word = o_segment[0]c_word = c_segment[0]if _is_small_spelling_change(o_word, c_word):new_words.append(c_word)ctx.add_patch('spelling', start_idx, end_idx,1 unmodified linealternatives=_get_spelling_alternatives(o_word, c_word, spell_checker),)else:new_words.append(current_text[start_idx:end_idx])elif len(o_segment) == 1 and len(c_segment) > 1:# 1-word → N words: accept word splits (e.g. فيالمدرسة → في المدرسة)21 unmodified linesif ci < len(c_segment):c_word = c_segment[ci]# Check if this is a 1→1 small editif _is_small_spelling_change(o_word, c_word):new_words.append(c_word)ctx.add_patch('spelling', o_start, o_end,62 unmodified linesf"'{d.get('original','')}' — locked by previous stage")continue# Re-label: if grammar's change is purely orthographic# (hamza, ه→ة, etc.), tag it as 'spelling' for correct UI iconstage_label = 'grammar'if _is_spelling_only_change(d.get('original', ''), d.get('correction', '')):stage_label = 'spelling'ctx.add_patch(stage_label, d['start'], d['end'],d['correction'], confidence=1.0)ctx.mutate_text(corrected_grammar, OffsetMapper)current_text = ctx.current_text733 unmodified lines73473573673773873974074174274374474574674715 unmodified lines763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798189 unmodified lines9889899909919929939949951 unmodified line997998999100010011002100321 unmodified lines102510261027102810291030103162 unmodified lines109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123733 unmodified linesreturn dp[m][n]def _is_small_spelling_change(orig_word, corr_word, vocab_manager=None):"""Heuristic: only accept small spelling edits and ignoreaggressive changes (to avoid over-editing).CRITICAL: If both words are in-vocabulary (both are valid Arabic words),only accept known orthographic fixes (ه→ة, hamza whitelist).This prevents the model from corrupting correct words (e.g. وكان→وكأن)."""if not orig_word or not corr_word:return False15 unmodified linesif corr_word == orig_word[:-1] or len(corr_word) < len(orig_word):return False# CRITICAL: If both words are valid Arabic words, only accept known fixes.# This prevents the spelling model from changing one correct word to another# (e.g. وكان→وكأن, which changes "and was" to "as if" — a meaning change).if vocab_manager:orig_iv = vocab_manager.is_iv(orig_word)corr_iv = vocab_manager.is_iv(corr_word)if orig_iv and corr_iv:# Both are valid words — only accept known orthographic fixes:# 1. ه→ة at word end (feminine marker fix)if (orig_word.endswith('ه') and corr_word.endswith('ة')and orig_word[:-1] == corr_word[:-1]):return True# 2. ة→ه at word end (less common but valid)if (orig_word.endswith('ة') and corr_word.endswith('ه')and orig_word[:-1] == corr_word[:-1]):return True# 3. Word is in the hamza whitelist (known common errors)from nlp.spelling.araspell_rules import AraSpellPostProcessorif orig_word in AraSpellPostProcessor.HAMZA_WHITELIST:return True# 4. Check prefixed hamza (و+whitelist word, etc.)for prefix in AraSpellPostProcessor.HAMZA_PREFIXES:if orig_word.startswith(prefix) and len(orig_word) > len(prefix) + 1:remainder = orig_word[len(prefix):]if remainder in AraSpellPostProcessor.HAMZA_WHITELIST:return True# Both are valid words and change is NOT a known fix — REJECT# This prevents وكان→وكأن, etc.return Falsedist = _levenshtein(orig_word, corr_word)max_len = max(len(orig_word), len(corr_word))189 unmodified lines# 1-word → 1-word: accept only small edits (typos)o_word = o_segment[0]c_word = c_segment[0]if _is_small_spelling_change(o_word, c_word, spell_checker.vocab_manager):logger.info(f"[SPELLING] Accepted: '{o_word}'→'{c_word}'")new_words.append(c_word)ctx.add_patch('spelling', start_idx, end_idx,1 unmodified linealternatives=_get_spelling_alternatives(o_word, c_word, spell_checker),)else:logger.info(f"[SPELLING] Rejected: '{o_word}'→'{c_word}' (filter blocked)")new_words.append(current_text[start_idx:end_idx])elif len(o_segment) == 1 and len(c_segment) > 1:# 1-word → N words: accept word splits (e.g. فيالمدرسة → في المدرسة)21 unmodified linesif ci < len(c_segment):c_word = c_segment[ci]# Check if this is a 1→1 small editif _is_small_spelling_change(o_word, c_word, spell_checker.vocab_manager):new_words.append(c_word)ctx.add_patch('spelling', o_start, o_end,62 unmodified linesf"'{d.get('original','')}' — locked by previous stage")continue# Reject grammar hallucinations (e.g. جالس→جاكسون)orig_text = d.get('original', '')corr_text = d.get('correction', '')if orig_text and corr_text:orig_chars = set(orig_text.replace(' ', ''))corr_chars = set(corr_text.replace(' ', ''))if orig_chars and corr_chars:jaccard = len(orig_chars & corr_chars) / len(orig_chars | corr_chars)if jaccard < 0.3:logger.info(f"[GRAMMAR] Rejected hallucination: '{orig_text}'→'{corr_text}' "f"(jaccard={jaccard:.2f})")continue# Re-label: if grammar's change is purely orthographic# (hamza, ه→ة, etc.), tag it as 'spelling' for correct UI iconstage_label = 'grammar'if _is_spelling_only_change(orig_text, corr_text):stage_label = 'spelling'ctx.add_patch(stage_label, d['start'], d['end'],corr_text, confidence=1.0)ctx.mutate_text(corrected_grammar, OffsetMapper)current_text = ctx.current_text
162 unmodified lines1631641651661671681691701711721731 unmodified line1751761771781791801811825 unmodified lines1881891901911921936 unmodified lines200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238162 unmodified linesdef fix_subject_verb_agreement(self, text):"""Fix G1: When a plural/dual noun PRECEDES a singular verb (SVO order),the verb must agree in number and gender.Arabic rule: In VSO order, verb can be singular even with plural subject.But in SVO order, subject-verb agreement is required."""tokens = simple_word_tokenize(text)if len(tokens) < 2:1 unmodified linedisambig_tokens = self.mle.disambiguate(tokens)corrected_tokens = list(tokens)# Common plural nouns (masculine sound plural) ending in ون/ين/ات# and their expected verb conjugation patternsfor i in range(len(disambig_tokens) - 1):noun_info = disambig_tokens[i].analyses[0] if disambig_tokens[i].analyses else Noneverb_info = disambig_tokens[i+1].analyses[0] if disambig_tokens[i+1].analyses else None5 unmodified linesnoun_word = corrected_tokens[i]verb_word = corrected_tokens[i+1]# Only process noun → verb patterns (SVO order)if noun_pos != 'noun' or verb_pos != 'verb':continue6 unmodified linesif verb_num != 's':continue# Detect plural nounsis_plural_masc = (noun_word.endswith('ون') or noun_word.endswith('ين')or noun_num == 'p')is_plural_fem = (noun_word.endswith('ات') or(noun_gen == 'f' and noun_num == 'p'))# Common broken plurals and collective nounsKNOWN_PLURALS_MASC = {'الطلاب', 'طلاب', 'الرجال', 'رجال', 'الأولاد', 'أولاد','الأطباء', 'أطباء', 'الاطباء', 'اطباء','العمال', 'عمال', 'الناس', 'الشباب', 'الأبناء',}KNOWN_PLURALS_FEM = {'الطالبات', 'طالبات', 'النساء', 'نساء', 'البنات', 'بنات','المعلمات', 'معلمات', 'الأمهات', 'أمهات',}if noun_word in KNOWN_PLURALS_MASC:is_plural_masc = Trueif noun_word in KNOWN_PLURALS_FEM:is_plural_fem = Trueif not is_plural_masc and not is_plural_fem:continue# Fix the verb to agree with the plural subject# Past tense singular → pluralif is_plural_fem:# Feminine plural: ذهب → ذهبنif not verb_word.endswith('ن') and not verb_word.endswith('نَ'):# Check if it's a past tense verb (typically 3-5 chars, no prefix)if len(verb_word) >= 3 and not verb_word.startswith('ي') and not verb_word.startswith('ت'):corrected_tokens[i+1] = verb_word + 'ن'elif is_plural_masc:# Masculine plural: ذهب → ذهبواif (not verb_word.endswith('وا') and not verb_word.endswith('ون')and not verb_word.endswith('ين')):if len(verb_word) >= 3 and not verb_word.startswith('ي') and not verb_word.startswith('ت'):162 unmodified lines1631641651661671681691701711721731741751761771781 unmodified line1801811821831841851861871881891901911921931941955 unmodified lines2012022032042052062072082092106 unmodified lines217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262162 unmodified linesdef fix_subject_verb_agreement(self, text):"""Fix G1: When a CONFIRMED plural noun PRECEDES a singular verb (SVO order),the verb must agree in number and gender.Arabic rule: In VSO order, verb can be singular even with plural subject.But in SVO order, subject-verb agreement is required.EXCLUSIONS:- Pronouns (أنا, أنت, هو, etc.) — these are NOT plural- Proper nouns — don't modify verbs after names- Words tagged as singular by the disambiguator"""tokens = simple_word_tokenize(text)if len(tokens) < 2:1 unmodified linedisambig_tokens = self.mle.disambiguate(tokens)corrected_tokens = list(tokens)# Words that should NEVER trigger plural verb agreementEXCLUDED_WORDS = {# Pronouns (all singular/dual)'أنا', 'انا', 'أنت', 'انت', 'أنتِ', 'هو', 'هي','نحن', 'أنتما', 'هما',# Common words that look like nouns but aren't plural'كان', 'وكان', 'كانت', 'وكانت', 'ليس', 'ليست','هذا', 'هذه', 'ذلك', 'تلك', 'هناك',}for i in range(len(disambig_tokens) - 1):noun_info = disambig_tokens[i].analyses[0] if disambig_tokens[i].analyses else Noneverb_info = disambig_tokens[i+1].analyses[0] if disambig_tokens[i+1].analyses else None5 unmodified linesnoun_word = corrected_tokens[i]verb_word = corrected_tokens[i+1]# Skip excluded wordsif noun_word in EXCLUDED_WORDS:continue# Only process noun → verb patterns (SVO order)if noun_pos != 'noun' or verb_pos != 'verb':continue6 unmodified linesif verb_num != 's':continue# Only trigger on CONFIRMED plurals:# 1. Known broken plural nouns (hardcoded list)# 2. Sound masculine plural ending in ون/ين# 3. Sound feminine plural ending in ات# Do NOT rely on POS tagger alone — it misclassifies too many wordsis_plural_masc = Falseis_plural_fem = FalseKNOWN_PLURALS_MASC = {'الطلاب', 'طلاب', 'الرجال', 'رجال', 'الأولاد', 'أولاد','الأطباء', 'أطباء', 'الاطباء', 'اطباء','العمال', 'عمال', 'الشباب', 'الأبناء','المهندسون', 'المعلمون', 'المهندسين', 'المعلمين',}KNOWN_PLURALS_FEM = {'الطالبات', 'طالبات', 'النساء', 'نساء', 'البنات', 'بنات','المعلمات', 'معلمات', 'الأمهات', 'أمهات',}if noun_word in KNOWN_PLURALS_MASC:is_plural_masc = Trueelif noun_word in KNOWN_PLURALS_FEM:is_plural_fem = Trueelif noun_word.endswith('ون') or noun_word.endswith('ين'):# Sound masculine plural — but only if 4+ chars (avoid short words)if len(noun_word) >= 5:is_plural_masc = Trueelif noun_word.endswith('ات') and len(noun_word) >= 5:is_plural_fem = Trueif not is_plural_masc and not is_plural_fem:continue# Fix the verb to agree with the plural subjectif is_plural_fem:if not verb_word.endswith('ن') and not verb_word.endswith('نَ'):if len(verb_word) >= 3 and not verb_word.startswith('ي') and not verb_word.startswith('ت'):corrected_tokens[i+1] = verb_word + 'ن'elif is_plural_masc:if (not verb_word.endswith('وا') and not verb_word.endswith('ون')and not verb_word.endswith('ين')):if len(verb_word) >= 3 and not verb_word.startswith('ي') and not verb_word.startswith('ت'):