Phase 13: Fix spelling filter blind spots + eager model loading
Browse filesSpelling filter improvements:
- Add adjacent character transposition detection (e.g., العصوبات→الصعوبات)
Transpositions have Levenshtein=2 but are a single adjacent swap.
Accepted with dampened confidence (0.6) when OOV→IV.
- Add single character insertion detection (extra letter typed)
Accepted with confidence 0.7 when OOV→IV.
- Add single character deletion detection (missed key)
Accepted with confidence 0.7 when OOV→IV.
- All three new edit types require OOV→IV validation to prevent
false positives (spelling precision remains 1.000).
Bidirectional fix patch creation:
- The bidirectional validation (Phase 12 A5) was silently correcting
text without creating patches, so users never saw the suggestion.
- Now creates spelling patches when rescuing OOV→IV words, making
corrections visible in the UI.
Eager model loading:
- All NLP models (spelling, grammar, punctuation, autocomplete, dialect)
now load at server startup instead of on first request.
- Eliminates cold-start latency on first user interaction.
- Each model load is independently wrapped in try/except so a single
failure does not prevent the server from starting.
989 unmodified lines9909919929939949952 unmodified lines99899910001001100210031004622 unmodified lines162716281629163016311632568 unmodified lines2201220222032204220522062207220822092210221122122213221422152216221722182219222022212222989 unmodified lines('د', 'ض'), ('ض', 'د'), # d/emphatic-d('غ', 'ق'), ('ق', 'غ'), # gh/q}# Check every character pair — reject if ANY non-orthographic changeif len(orig_word) != len(corr_word):# Length change = structural change, not just orthographic2 unmodified linesreturn 0.0# ── Phase 12 (A1): Keyboard-neighbor and phonetic acceptance ──# Check each differing character: ortho → full accept, keyboard/phonetic → dampenedfrom nlp.spelling.araspell_rules import RulesBasedCorrector_has_keyboard_or_phonetic = Falsefor a, b in zip(orig_word, corr_word):if a != b:622 unmodified linesf"[SPELLING] Bidirectional fix: "f"'{_safe_words[_bi]}'(OOV)→'{_raw_words[_bi]}'(IV)")_safe_words[_bi] = _raw_words[_bi]_bidi_changed = Trueif _bidi_changed:568 unmodified lines_models_loaded = Falsedef _ensure_models_loaded():global _models_loadedif _models_loaded:return_models_loaded = Truelogger.info("Loading models (production startup)...")if not load_models():logger.error("Failed to load any models. Server will start but functionality will be limited.")# Load models on import (gunicorn imports this module, __name__ != '__main__')_ensure_models_loaded()if __name__ == '__main__':# Load models on startup (development)_ensure_models_loaded()# Run the appport = int(os.environ.get('PORT', 5000))debug = os.environ.get('DEBUG', 'False').lower() == 'true'989 unmodified lines9909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910802 unmodified lines108310841085108610871088622 unmodified lines1711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741568 unmodified lines23102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392989 unmodified lines('د', 'ض'), ('ض', 'د'), # d/emphatic-d('غ', 'ق'), ('ق', 'غ'), # gh/q}from nlp.spelling.araspell_rules import RulesBasedCorrector# ── Phase 13: Adjacent character transposition detection ──# Transpositions (e.g., العصوبات→الصعوبات) have Levenshtein=2 but are a# single adjacent swap. Detect and accept when OOV→IV.if len(orig_word) == len(corr_word) and dist == 2:_transposition_found = Falsefor _ti in range(len(orig_word) - 1):if (orig_word[_ti] == corr_word[_ti + 1] andorig_word[_ti + 1] == corr_word[_ti] andorig_word[:_ti] == corr_word[:_ti] andorig_word[_ti + 2:] == corr_word[_ti + 2:]):_transposition_found = Truebreakif _transposition_found:if vocab_manager:_orig_oov = not vocab_manager.is_iv(orig_word)_corr_iv = vocab_manager.is_iv(corr_word)if _orig_oov and _corr_iv:logger.info(f"[SPELLING] Transposition accepted (OOV→IV): "f"'{orig_word}'→'{corr_word}'")return 0.6 # Dampened confidence for transpositionselif _orig_oov and not _corr_iv:# Both OOV — still accept transposition with lower confidencelogger.info(f"[SPELLING] Transposition accepted (OOV→OOV): "f"'{orig_word}'→'{corr_word}' (low confidence)")return 0.5else:return 0.6 # No vocab manager — accept with dampened confidence# ── Phase 13: Single character insertion detection ──# When the original has one extra character (user typed an extra letter),# e.g., الكتتاب→الكتاب (extra ت). Levenshtein=1, lengths differ by 1.if len(orig_word) == len(corr_word) + 1 and dist == 1:# Find where the extra character is in orig_word_insertion_valid = Falsefor _di in range(len(orig_word)):# Try removing character at position _di from orig_word_candidate = orig_word[:_di] + orig_word[_di + 1:]if _candidate == corr_word:_insertion_valid = Truebreakif _insertion_valid:if vocab_manager:_orig_oov = not vocab_manager.is_iv(orig_word)_corr_iv = vocab_manager.is_iv(corr_word)if _orig_oov and _corr_iv:logger.info(f"[SPELLING] Insertion fix accepted (OOV→IV): "f"'{orig_word}'→'{corr_word}' (extra char removed)")return 0.7else:return 0.6# ── Phase 13: Single character deletion detection ──# When the original is missing one character (user missed a key),# e.g., الكتب→الكتاب (missing ا). Levenshtein=1, lengths differ by 1.if len(corr_word) == len(orig_word) + 1 and dist == 1:# Find where the missing character should be in corr_word_deletion_valid = Falsefor _di in range(len(corr_word)):# Try removing character at position _di from corr_word_candidate = corr_word[:_di] + corr_word[_di + 1:]if _candidate == orig_word:_deletion_valid = Truebreakif _deletion_valid:if vocab_manager:_orig_oov = not vocab_manager.is_iv(orig_word)_corr_iv = vocab_manager.is_iv(corr_word)if _orig_oov and _corr_iv:logger.info(f"[SPELLING] Deletion fix accepted (OOV→IV): "f"'{orig_word}'→'{corr_word}' (missing char added)")return 0.7else:return 0.6# Check every character pair — reject if ANY non-orthographic changeif len(orig_word) != len(corr_word):# Length change = structural change, not just orthographic2 unmodified linesreturn 0.0# ── Phase 12 (A1): Keyboard-neighbor and phonetic acceptance ──# Check each differing character: ortho → full accept, keyboard/phonetic → dampened_has_keyboard_or_phonetic = Falsefor a, b in zip(orig_word, corr_word):if a != b:622 unmodified linesf"[SPELLING] Bidirectional fix: "f"'{_safe_words[_bi]}'(OOV)→'{_raw_words[_bi]}'(IV)")# ── Phase 13: Create patch for bidirectional fix ──# Find this word's position in the ORIGINAL text so the# user sees the correction as a suggestion in the UI.try:_orig_words_list = text.split()if _bi < len(_orig_words_list):_bidi_orig_word = _orig_words_list[_bi]# Only create patch if the original word matches# (bidirectional fix is correcting a filter-rejected word)if _bidi_orig_word == _safe_words[_bi]:_bidi_pos = 0for _bw_idx in range(_bi):_next_pos = text.find(_orig_words_list[_bw_idx], _bidi_pos)if _next_pos >= 0:_bidi_pos = _next_pos + len(_orig_words_list[_bw_idx])_bidi_start = text.find(_bidi_orig_word, max(0, _bidi_pos))if _bidi_start >= 0:_bidi_end = _bidi_start + len(_bidi_orig_word)ctx.add_patch('spelling', _bidi_start, _bidi_end,_raw_words[_bi], confidence=0.6,alternatives=[_raw_words[_bi], _bidi_orig_word],)except Exception:pass # Patch creation is best-effort_safe_words[_bi] = _raw_words[_bi]_bidi_changed = Trueif _bidi_changed:568 unmodified lines_models_loaded = Falsedef _ensure_models_loaded():"""Load ALL models at startup — no lazy loading.Each model is wrapped in its own try/except so a single failuredoesn't prevent the server from starting. Models that fail to loadwill gracefully degrade at request time."""global _models_loadedif _models_loaded:return_models_loaded = Truetotal_t0 = time.time()logger.info("=" * 60)logger.info("BAYAN — Loading ALL models at startup (eager mode)...")logger.info("=" * 60)# 1. Summarization (legacy load_models)if not load_models():logger.error("Failed to load summarization model.")# 2. Spelling modeltry:t0 = time.time()from nlp.spelling.araspell_service import get_spelling_modelget_spelling_model()logger.info(f"✓ Spelling model loaded in {time.time()-t0:.1f}s")except Exception as e:logger.error(f"✗ Spelling model failed to load: {e}")# 3. Grammar model (Gradio client + camel-tools rules)try:t0 = time.time()from nlp.grammar.grammar_service import get_grammar_modelget_grammar_model()logger.info(f"✓ Grammar model loaded in {time.time()-t0:.1f}s")except Exception as e:logger.error(f"✗ Grammar model failed to load: {e}")# 4. Punctuation modeltry:t0 = time.time()from nlp.punctuation.punctuation_service import get_punctuation_modelget_punctuation_model()logger.info(f"✓ Punctuation model loaded in {time.time()-t0:.1f}s")except Exception as e:logger.error(f"✗ Punctuation model failed to load: {e}")# 5. Autocomplete modeltry:t0 = time.time()from nlp.autocomplete.autocomplete_service import get_autocomplete_modelget_autocomplete_model()logger.info(f"✓ Autocomplete model loaded in {time.time()-t0:.1f}s")except Exception as e:logger.error(f"✗ Autocomplete model failed to load: {e}")# 6. Dialect modeltry:t0 = time.time()from nlp.dialect.dialect_service import get_dialect_modelget_dialect_model()logger.info(f"✓ Dialect model loaded in {time.time()-t0:.1f}s")except Exception as e:logger.error(f"✗ Dialect model failed to load: {e}")total_elapsed = time.time() - total_t0logger.info("=" * 60)logger.info(f"BAYAN — All models loaded in {total_elapsed:.1f}s")logger.info("=" * 60)# Load models on import (gunicorn imports this module, __name__ != '__main__')_ensure_models_loaded()if __name__ == '__main__':# Models already loaded above via _ensure_models_loaded()# Run the appport = int(os.environ.get('PORT', 5000))debug = os.environ.get('DEBUG', 'False').lower() == 'true'