youssefreda9 commited on
Commit
dcb0546
·
1 Parent(s): 524d4e0

CRITICAL FIX: _DIRECTIONAL_BLOCKS was local scope → NameError in grammar diff filter

Browse files

Root cause found from production logs:
NameError: name '_DIRECTIONAL_BLOCKS' is not defined (line 1633)

_DIRECTIONAL_BLOCKS was defined inside _compute_spelling_confidence() but
referenced in analyze_text()'s grammar diff processing section. This caused
ALL grammar corrections to silently crash and be dropped.

Fix: Move _DIRECTIONAL_BLOCKS to module level so both sections can access it.

src/app.py CHANGED
@@ -97,6 +97,32 @@ autocomplete_model = None
97
  grammar_model = None
98
  punctuation_model = None
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
  def load_models():
102
  """Load models. In HF API mode, load summarization locally; other models gracefully degrade."""
@@ -812,29 +838,7 @@ def _is_small_spelling_change(orig_word, corr_word, vocab_manager=None):
812
  # AraSpell duplicates shadda-bearing words in ISOLATION: إنّ→إن إن, أنّ→أن أن.
813
  # In sentence context (e.g., "إنّ العلم نور"), the model handles shadda correctly.
814
  # This is an isolation-only AraSpell quirk — no pipeline filter needed.
815
- _DIRECTIONAL_BLOCKS = {
816
- # Demonstratives: هذه (correct feminine) → هذة (misspelling) = ALWAYS wrong
817
- 'هذه': {'هذة'},
818
- 'هذا': {'هذة', 'هذه'}, # masculine → don't flip to feminine forms
819
- # Verb/particle confusion: كان (was) ↔ كأن (as if) = ALWAYS wrong
820
- 'كان': {'كأن'},
821
- 'كأن': {'كان'},
822
- 'كانت': {'كأنت'}, # H016: كانت → كأنت = ALWAYS wrong
823
- 'كانوا': {'كأنوا'}, # also block plural form
824
- # Preposition confusion: different meanings, both valid
825
- 'إلى': {'على', 'علي'},
826
- 'على': {'إلى', 'علي'},
827
- 'علي': {'على'}, # proper name vs preposition
828
- # Conjunction: لكن (correct) ↔ لاكن (misspelling of لكن, never valid)
829
- 'لكن': {'لاكن'}, # correct → misspelling = ALWAYS wrong
830
- # Demonstrative: ذلك (correct) ↔ ذالك (common misspelling)
831
- 'ذلك': {'ذالك'}, # correct → misspelling = ALWAYS wrong
832
- # Pronoun suffix: ه→ة corruption (G037: عمله→عملة)
833
- 'عمله': {'عملة'}, # عمله (his work) → عملة (currency) = WRONG
834
- 'لسانه': {'لسانة'}, # his tongue
835
- 'بيته': {'بيتة'}, # his house
836
- 'كتابه': {'كتابة'}, # his book → writing
837
- }
838
  if corr_word in _DIRECTIONAL_BLOCKS.get(orig_word, set()):
839
  return 0.0
840
 
 
97
  grammar_model = None
98
  punctuation_model = None
99
 
100
+ # ── Directional Blocks: prevent meaning-changing substitutions ──
101
+ # Used by both spelling confidence filter and grammar diff filter.
102
+ _DIRECTIONAL_BLOCKS = {
103
+ # Demonstratives: هذه (correct feminine) → هذة (misspelling) = ALWAYS wrong
104
+ 'هذه': {'هذة'},
105
+ 'هذا': {'هذة', 'هذه'}, # masculine → don't flip to feminine forms
106
+ # Verb/particle confusion: كان (was) ↔ كأن (as if) = ALWAYS wrong
107
+ 'كان': {'كأن'},
108
+ 'كأن': {'كان'},
109
+ 'كانت': {'كأنت'}, # H016: كانت → كأنت = ALWAYS wrong
110
+ 'كانوا': {'كأنوا'}, # also block plural form
111
+ # Preposition confusion: different meanings, both valid
112
+ 'إلى': {'على', 'علي'},
113
+ 'على': {'إلى', 'علي'},
114
+ 'علي': {'على'}, # proper name vs preposition
115
+ # Conjunction: لكن (correct) ↔ لاكن (misspelling of لكن, never valid)
116
+ 'لكن': {'لاكن'}, # correct → misspelling = ALWAYS wrong
117
+ # Demonstrative: ذلك (correct) ↔ ذالك (common misspelling)
118
+ 'ذلك': {'ذالك'}, # correct → misspelling = ALWAYS wrong
119
+ # Pronoun suffix: ه→ة corruption (G037: عمله→عملة)
120
+ 'عمله': {'عملة'}, # عمله (his work) → عملة (currency) = WRONG
121
+ 'لسانه': {'لسانة'}, # his tongue
122
+ 'بيته': {'بيتة'}, # his house
123
+ 'كتابه': {'كتابة'}, # his book → writing
124
+ }
125
+
126
 
127
  def load_models():
128
  """Load models. In HF API mode, load summarization locally; other models gracefully degrade."""
 
838
  # AraSpell duplicates shadda-bearing words in ISOLATION: إنّ→إن إن, أنّ→أن أن.
839
  # In sentence context (e.g., "إنّ العلم نور"), the model handles shadda correctly.
840
  # This is an isolation-only AraSpell quirk — no pipeline filter needed.
841
+ # _DIRECTIONAL_BLOCKS is defined at module level (line ~100)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
842
  if corr_word in _DIRECTIONAL_BLOCKS.get(orig_word, set()):
843
  return 0.0
844
 
tests/phase10/reports/phase10_results.json CHANGED
The diff for this file is too large to render. See raw diff