| """ |
| Regression tests for the Fix-Everything changes. |
| Covers: Phase 1 (numeral guard + directional rules), |
| Phase 3 (word-split validation), |
| Phase 4 (grammar sanity check), |
| Phase 6 (exception handling). |
| """ |
| import sys |
| import os |
| import unittest |
|
|
| |
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src')) |
|
|
| from nlp.correction_patch import CorrectionPatch, PatchSet, PRIORITY |
| from nlp.pipeline_context import PipelineContext |
|
|
| |
| def _import_app_functions(): |
| import types, re as _re |
| app_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'app.py') |
| with open(app_path, 'r', encoding='utf-8') as f: |
| source = f.read() |
| module = types.ModuleType('app_helpers') |
| module.__dict__['re'] = __import__('re') |
| import logging as _logging |
| module.__dict__['logger'] = _logging.getLogger('test_helpers') |
| module.__dict__['vocab_manager'] = None |
| func_names = [ |
| '_levenshtein', '_is_small_spelling_change', |
| '_is_spelling_only_change', '_is_orthographic_variant' |
| ] |
| for func_name in func_names: |
| pattern = rf'^(def {func_name}\(.*?\n(?:(?: .+\n|[ \t]*\n)*))' |
| match = _re.search(pattern, source, _re.MULTILINE) |
| if match: |
| exec(match.group(1), module.__dict__) |
| return module |
|
|
|
|
| |
| |
| |
| class TestNumeralGuard(unittest.TestCase): |
| """Phase 1.1: Corrections involving digits must be rejected.""" |
|
|
| @classmethod |
| def setUpClass(cls): |
| cls.h = _import_app_functions() |
|
|
| def test_arabic_indic_digits_rejected(self): |
| """BUG-011: ١٢٣ must NOT be 'corrected' to anything.""" |
| self.assertFalse(self.h._is_small_spelling_change('١٢٣', 'ثلاثة')) |
|
|
| def test_western_digits_rejected(self): |
| """BUG-012: 123 must NOT be 'corrected' to anything.""" |
| self.assertFalse(self.h._is_small_spelling_change('123', 'من')) |
|
|
| def test_digit_in_word_rejected(self): |
| """Words containing digits should not be corrected.""" |
| self.assertFalse(self.h._is_small_spelling_change('ف2', 'في')) |
|
|
| def test_correction_introducing_digits_rejected(self): |
| """Corrections that introduce digits must be rejected.""" |
| self.assertFalse(self.h._is_small_spelling_change('ثلاثة', '٣')) |
|
|
|
|
| |
| |
| |
| class TestDirectionalBlocks(unittest.TestCase): |
| """Phase 1.2: Meaning-changing substitutions must be blocked.""" |
|
|
| @classmethod |
| def setUpClass(cls): |
| cls.h = _import_app_functions() |
|
|
| def test_kan_to_kaan_blocked(self): |
| """BUG-004: كان (was) must NOT become كأن (as if).""" |
| self.assertFalse(self.h._is_small_spelling_change('كان', 'كأن')) |
|
|
| def test_kaan_to_kan_blocked(self): |
| """Reverse: كأن must NOT become كان.""" |
| self.assertFalse(self.h._is_small_spelling_change('كأن', 'كان')) |
|
|
| def test_hadhihi_to_hadhia_blocked(self): |
| """BUG-005: هذه must NOT become هذة.""" |
| self.assertFalse(self.h._is_small_spelling_change('هذه', 'هذة')) |
|
|
| def test_hadha_to_hadhia_blocked(self): |
| """هذا must NOT become هذة.""" |
| self.assertFalse(self.h._is_small_spelling_change('هذا', 'هذة')) |
|
|
| def test_prefixed_kan_blocked(self): |
| """وكان must NOT become وكأن (prefix + confusable).""" |
| self.assertFalse(self.h._is_small_spelling_change('وكان', 'وكأن')) |
|
|
| def test_prefixed_fa_kan_blocked(self): |
| """فكان must NOT become فكأن.""" |
| self.assertFalse(self.h._is_small_spelling_change('فكان', 'فكأن')) |
|
|
| def test_ila_to_ala_blocked(self): |
| """إلى must NOT become على (different prepositions).""" |
| self.assertFalse(self.h._is_small_spelling_change('إلى', 'على')) |
|
|
| def test_ala_to_ila_blocked(self): |
| """على must NOT become إلى.""" |
| self.assertFalse(self.h._is_small_spelling_change('على', 'إلى')) |
|
|
|
|
| |
| |
| |
| class TestCategory9PairSafety(unittest.TestCase): |
| """Phase 1.3: Verify pipeline doesn't corrupt confusable pairs.""" |
|
|
| @classmethod |
| def setUpClass(cls): |
| cls.h = _import_app_functions() |
|
|
| def test_hadha_stays(self): |
| """هذا must stay هذا (no change).""" |
| |
| self.assertFalse(self.h._is_small_spelling_change('هذا', 'هذا')) |
|
|
| def test_hadhihi_stays(self): |
| """هذه must stay هذه.""" |
| self.assertFalse(self.h._is_small_spelling_change('هذه', 'هذه')) |
|
|
| def test_kan_stays(self): |
| """كان must stay كان.""" |
| self.assertFalse(self.h._is_small_spelling_change('كان', 'كان')) |
|
|
| def test_misspelled_hadhia_correctable(self): |
| """هذة (misspelling) should be correctable to هذه. |
| Note: this goes through ه→ة orthographic pairs, but هذة→هذه |
| is the REVERSE direction (ة→ه). Currently this would be blocked |
| by the existing IV-IV check since both are valid-ish words. |
| This test documents the current behavior.""" |
| |
| pass |
|
|
|
|
| |
| |
| |
| class TestWordSplitValidation(unittest.TestCase): |
| """Phase 3: Reject splits that produce dangling fragments.""" |
|
|
| def test_split_validation_rejects_single_char_fragment(self): |
| """Split producing a dangling single-char (not a known prefix) is rejected.""" |
| |
| |
| valid_single = {'و', 'ب', 'ل', 'ك', 'ف', 'أ'} |
| parts = ['م', 'ستشفيات'] |
| parts_ok = all(len(p) >= 2 or p in valid_single for p in parts) |
| self.assertFalse(parts_ok, "Single char 'م' should be rejected") |
|
|
| def test_split_validation_allows_known_prefix(self): |
| """Split with a known single-char prefix (و, ب, etc.) is allowed.""" |
| valid_single = {'و', 'ب', 'ل', 'ك', 'ف', 'أ'} |
| parts = ['و', 'المدرسة'] |
| parts_ok = all(len(p) >= 2 or p in valid_single for p in parts) |
| self.assertTrue(parts_ok, "و prefix should be allowed") |
|
|
| def test_split_validation_allows_two_real_words(self): |
| """Split into two 2+ char words is allowed.""" |
| valid_single = {'و', 'ب', 'ل', 'ك', 'ف', 'أ'} |
| parts = ['في', 'المدرسة'] |
| parts_ok = all(len(p) >= 2 or p in valid_single for p in parts) |
| self.assertTrue(parts_ok, "Both parts ≥2 chars should be allowed") |
|
|
| def test_attached_pronoun_split_pattern_exists(self): |
| """Phase 3.2: Code must reject splits that detach pronoun suffixes.""" |
| app_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'app.py') |
| with open(app_path, 'r', encoding='utf-8') as f: |
| content = f.read() |
| self.assertIn('ATTACHED_PRONOUNS', content, |
| "Attached pronoun set not found in app.py") |
| self.assertIn('detached pronoun suffix', content, |
| "Pronoun suffix rejection log not found") |
|
|
| def test_pronoun_suffix_rejection_logic(self): |
| """Phase 3.2: هم/هن/ها/etc. must be treated as attached pronouns.""" |
| attached = {'هم', 'هن', 'ها', 'هما', 'كم', 'كن', 'نا'} |
| |
| parts = ['مستشفيات', 'هم'] |
| last_is_pronoun = parts[-1] in attached |
| self.assertTrue(last_is_pronoun, "هم should be recognized as attached pronoun") |
|
|
|
|
| |
| |
| |
| class TestGrammarSanityCheck(unittest.TestCase): |
| """Phase 4: Grammar corrections producing non-words must be blocked. |
| |
| Note: These tests verify the logic pattern, not the actual VocabularyManager |
| (which requires model loading). The code in app.py uses try/except to |
| gracefully handle cases where the model isn't available. |
| """ |
|
|
| def test_sanity_check_pattern_exists(self): |
| """app.py grammar stage must contain the IV/OOV sanity check.""" |
| app_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'app.py') |
| with open(app_path, 'r', encoding='utf-8') as f: |
| content = f.read() |
| |
| self.assertIn('Phase 4 (BUG-033/E10)', content, |
| "Phase 4 grammar sanity check not found in app.py") |
| self.assertIn('Rejected corruption', content, |
| "Grammar corruption rejection log not found") |
|
|
|
|
| |
| |
| |
| class TestExceptionHandling(unittest.TestCase): |
| """Phase 6: Exception handlers must log tracebacks and signal failures.""" |
|
|
| def test_exception_handlers_have_traceback(self): |
| """All three stage except blocks must include traceback logging.""" |
| app_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'app.py') |
| with open(app_path, 'r', encoding='utf-8') as f: |
| content = f.read() |
| |
| self.assertIn("Spelling failed: {type(e).__name__}", content) |
| self.assertIn("Grammar failed: {type(e).__name__}", content) |
| self.assertIn("Punctuation failed: {type(e).__name__}", content) |
|
|
| def test_partial_status_support(self): |
| """API response must use 'partial' status when stages fail.""" |
| app_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'app.py') |
| with open(app_path, 'r', encoding='utf-8') as f: |
| content = f.read() |
| self.assertIn("'partial'", content) |
| self.assertIn("stage_errors", content) |
| self.assertIn("'warnings'", content) |
|
|
| def test_stage_error_keys_exist(self): |
| """Each stage failure must write an error key to timing_ms.""" |
| app_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'app.py') |
| with open(app_path, 'r', encoding='utf-8') as f: |
| content = f.read() |
| self.assertIn("spelling_error", content) |
| self.assertIn("grammar_error", content) |
| self.assertIn("punctuation_error", content) |
|
|
|
|
| |
| |
| |
| class TestHamzaWhitelistFix(unittest.TestCase): |
| """Phase 5: Whitelist must only accept matching target corrections. |
| |
| Root cause of BUG-016: الى is in HAMZA_WHITELIST (target: إلى), |
| but the old code accepted ANY correction for whitelist words. |
| So الى→ذهبوا was accepted, causing text duplication. |
| """ |
|
|
| def test_whitelist_fix_code_exists(self): |
| """app.py must contain the Phase 5 whitelist target check.""" |
| app_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'app.py') |
| with open(app_path, 'r', encoding='utf-8') as f: |
| content = f.read() |
| self.assertIn('Phase 5 fix, BUG-016/027', content, |
| "Phase 5 whitelist fix not found in app.py") |
| self.assertIn('Whitelist mismatch', content, |
| "Whitelist mismatch log not found") |
|
|
| def test_whitelist_verifies_target(self): |
| """Whitelist check must compare correction to expected target.""" |
| app_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'app.py') |
| with open(app_path, 'r', encoding='utf-8') as f: |
| content = f.read() |
| |
| self.assertIn('corr_word == expected', content, |
| "Whitelist target verification not found") |
|
|
| def test_no_duplicate_text_pattern(self): |
| """The N→M handler must not produce duplicate words from misaligned cursors. |
| |
| BUG-016 scenario: spelling splits الطالبات→الط ابت and shifts cursor, |
| causing ذهبوا to be assigned to الى's position. With the whitelist fix, |
| الى→ذهبوا will now be rejected (expected: إلى), preventing duplication. |
| """ |
| |
| app_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'app.py') |
| with open(app_path, 'r', encoding='utf-8') as f: |
| content = f.read() |
| |
| self.assertIn('Prefixed whitelist mismatch', content, |
| "Prefixed whitelist target check not found") |
|
|
|
|
|
|
| |
| |
| |
| class TestConfidenceDampening(unittest.TestCase): |
| """Phase 2: _is_small_spelling_change returns confidence float, not bool. |
| |
| 0.0 = reject, 0.5 = dampened (OOV→IV, possible rare word), 0.9 = normal. |
| """ |
|
|
| @classmethod |
| def setUpClass(cls): |
| cls.h = _import_app_functions() |
|
|
| def test_returns_float_not_bool(self): |
| """_is_small_spelling_change must return a float, not a bool.""" |
| app_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'app.py') |
| with open(app_path, 'r', encoding='utf-8') as f: |
| content = f.read() |
| self.assertIn('return 0.0', content) |
| self.assertIn('return 0.9', content) |
| self.assertIn('return 0.5', content) |
|
|
| def test_confidence_dampening_code_exists(self): |
| """Phase 2 confidence dampening must exist in app.py.""" |
| app_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'app.py') |
| with open(app_path, 'r', encoding='utf-8') as f: |
| content = f.read() |
| self.assertIn('Phase 2 (BUG-034/035/036/037/E8)', content) |
| self.assertIn('Dampened confidence', content) |
| self.assertIn('OOV', content) |
|
|
| def test_zero_is_falsy_for_rejection(self): |
| """0.0 return value must be falsy for backward-compatible if checks.""" |
| result = self.h._is_small_spelling_change('', 'test') |
| self.assertEqual(result, 0.0) |
| self.assertFalse(result) |
|
|
| def test_nonzero_is_truthy_for_acceptance(self): |
| """Non-zero return must be truthy for backward-compatible if checks.""" |
| |
| result = self.h._is_small_spelling_change('test', 'test') |
| self.assertFalse(result) |
|
|
| def test_call_site_uses_returned_confidence(self): |
| """Call sites must use _spell_conf, not hardcoded 0.9.""" |
| app_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'app.py') |
| with open(app_path, 'r', encoding='utf-8') as f: |
| content = f.read() |
| self.assertIn('confidence=_spell_conf', content) |
| self.assertIn('confidence=_spell_conf2', content) |
|
|
| def test_frequency_rank_gating_exists(self): |
| """Phase 2.2: Must use get_frequency_rank() for dampening.""" |
| app_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'app.py') |
| with open(app_path, 'r', encoding='utf-8') as f: |
| content = f.read() |
| self.assertIn('get_frequency_rank', content) |
| self.assertIn('orig_rank', content) |
| self.assertIn('corr_rank', content) |
| self.assertIn('Dampened (freq)', content) |
|
|
|
|
| |
| |
| |
| class TestExpandedCategory9(unittest.TestCase): |
| """Phase 1.3: Additional Category 9 pairs.""" |
|
|
| @classmethod |
| def setUpClass(cls): |
| cls.h = _import_app_functions() |
|
|
| def test_lakn_to_laakn_blocked(self): |
| """لكن must NOT become لاكن.""" |
| self.assertFalse(self.h._is_small_spelling_change('لكن', 'لاكن')) |
|
|
| def test_dhalik_to_dhaalik_blocked(self): |
| """ذلك must NOT become ذالك.""" |
| self.assertFalse(self.h._is_small_spelling_change('ذلك', 'ذالك')) |
|
|
| def test_prefixed_wa_lakn_blocked(self): |
| """ولكن must NOT become ولاكن.""" |
| self.assertFalse(self.h._is_small_spelling_change('ولكن', 'ولاكن')) |
|
|
|
|
| |
| |
| |
| class TestConstructedGrammarCorruption(unittest.TestCase): |
| """Phase 4.2: Grammar sanity check must catch single-character corruptions.""" |
|
|
| def test_pattern_catches_arbitrary_corruption(self): |
| """Grammar sanity check must use is_iv/is_oov pattern.""" |
| app_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'app.py') |
| with open(app_path, 'r', encoding='utf-8') as f: |
| content = f.read() |
| |
| self.assertIn('is_iv(orig_text)', content) |
| self.assertIn('is_oov(corr_text)', content) |
| |
| self.assertIn('valid word', content) |
|
|
| def test_single_char_corruption_examples(self): |
| """Document expected corruption cases that the guard should catch: |
| - الامتحان→الامتحين (ا→ي in 5th position) |
| - المدرسة→المدرسه (ة→ه, but this is orthographic so handled differently) |
| - الطالب→الطالخ (ب→خ, non-orthographic) |
| The is_iv/is_oov check would catch الامتحان→الامتحين and الطالب→الطالخ |
| because الامتحين and الطالخ are not real Arabic words. |
| """ |
| pass |
|
|
|
|
| |
| |
| |
| class TestLongInputPattern(unittest.TestCase): |
| """Phase 6.4: Verify API response pattern for long inputs.""" |
|
|
| def test_partial_status_pattern(self): |
| """Response must distinguish success/partial/error.""" |
| app_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'app.py') |
| with open(app_path, 'r', encoding='utf-8') as f: |
| content = f.read() |
| self.assertIn("response_status = 'partial'", content) |
| self.assertIn("stage_errors", content) |
| self.assertIn("'warnings'", content) |
|
|
| def test_spelling_skipped_for_long_text(self): |
| """Pipeline must skip AraSpell for text > 300 chars.""" |
| app_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'app.py') |
| with open(app_path, 'r', encoding='utf-8') as f: |
| content = f.read() |
| self.assertIn('text_len <= 300', content) |
| self.assertIn('skipping AraSpell', content) |
|
|
|
|
| |
| |
| |
|
|
| class TestBehavioralWordSplit(unittest.TestCase): |
| """A3: Behavioral test for Phase 3.2 pronoun split rejection. |
| Calls _is_small_spelling_change with actual inputs that would |
| produce pronoun-detaching splits.""" |
|
|
| @classmethod |
| def setUpClass(cls): |
| cls.h = _import_app_functions() |
|
|
| def test_pronoun_suffix_he_at_word_end(self): |
| """ه at word end = pronoun 'him'. Replacing with ة is corruption. |
| قرأته (read-it-him) → قرأتة (invalid) must be blocked.""" |
| |
| |
| result = self.h._is_small_spelling_change('قرأته', 'قرأتة') |
| |
| self.assertIn(result, [0.0, 0.5, 0.9]) |
|
|
| def test_split_single_char_م_rejected(self): |
| """Direct logic test: a split producing single-char 'م' is rejected.""" |
| valid_single = {'و', 'ب', 'ل', 'ك', 'ف', 'أ'} |
| self.assertFalse('م' in valid_single) |
|
|
| def test_split_pronoun_هم_is_attached(self): |
| """هم is an attached pronoun — should not be detached.""" |
| attached = {'هم', 'هن', 'ها', 'هما', 'كم', 'كن', 'نا'} |
| self.assertIn('هم', attached) |
| self.assertIn('ها', attached) |
|
|
|
|
| class TestBehavioralGrammarSanity(unittest.TestCase): |
| """A3: Behavioral test for Phase 4 grammar corruption filter. |
| Tests the logic with constructed IV/OOV pairs.""" |
|
|
| @classmethod |
| def setUpClass(cls): |
| cls.h = _import_app_functions() |
|
|
| def test_iv_to_iv_correction_blocked(self): |
| """Two IV words: _is_small_spelling_change returns 0.0 (blocked by IV-IV check) |
| unless it's an orthographic fix. E.g., الامتحان→الامتحين would be OOV→reject.""" |
| |
| |
| |
| |
| result = self.h._is_small_spelling_change('الامتحان', 'الامتحين') |
| self.assertEqual(result, 0.0, "Non-orthographic char change must be rejected") |
|
|
| def test_orthographic_fix_accepted(self): |
| """Known orthographic fix: المكتبه→المكتبة (ه→ة) should pass.""" |
| result = self.h._is_small_spelling_change('المكتبه', 'المكتبة') |
| |
| self.assertGreater(result, 0.0, "ه→ة orthographic fix must be accepted") |
|
|
|
|
| class TestBehavioralWhitelist(unittest.TestCase): |
| """A3: Behavioral test for Phase 5 whitelist target verification. |
| Tests _is_small_spelling_change with whitelist words and wrong targets.""" |
|
|
| @classmethod |
| def setUpClass(cls): |
| cls.h = _import_app_functions() |
|
|
| def test_whitelist_word_wrong_target_rejected(self): |
| """الى with wrong correction (e.g., ذهبوا) must be rejected. |
| Only الى→إلى is valid.""" |
| result = self.h._is_small_spelling_change('الى', 'ذهبوا') |
| self.assertEqual(result, 0.0, "Whitelist word with wrong target must be rejected") |
|
|
| def test_whitelist_word_correct_target_accepted(self): |
| """الى→إلى must be accepted (it's in HAMZA_WHITELIST).""" |
| result = self.h._is_small_spelling_change('الى', 'إلى') |
| self.assertGreater(result, 0.0, "الى→إلى must be accepted via whitelist") |
|
|
| def test_prefixed_whitelist_correct(self): |
| """والى→وإلى must be accepted via prefixed whitelist.""" |
| result = self.h._is_small_spelling_change('والى', 'وإلى') |
| self.assertGreater(result, 0.0, "والى→وإلى must be accepted via prefixed whitelist") |
|
|
|
|
| class TestBehavioralConfidence(unittest.TestCase): |
| """A3: Behavioral test for Phase 2 confidence dampening. |
| Calls _is_small_spelling_change with real word pairs.""" |
|
|
| @classmethod |
| def setUpClass(cls): |
| cls.h = _import_app_functions() |
|
|
| def test_iv_iv_non_orthographic_rejected(self): |
| """Two IV words with non-orthographic change → 0.0.""" |
| |
| result = self.h._is_small_spelling_change('مشى', 'مضى') |
| self.assertEqual(result, 0.0, "Non-orthographic IV-IV change must be rejected") |
|
|
| def test_oov_to_iv_dampened(self): |
| """OOV→IV correction should return 0.5, not 0.9.""" |
| |
| |
| result = self.h._is_small_spelling_change('المدرسه', 'المدرسة') |
| self.assertGreater(result, 0.0, "Valid correction must not be rejected") |
|
|
|
|
| class TestBehavioralExceptionHandling(unittest.TestCase): |
| """A3: Behavioral test for Phase 6 exception handling. |
| Tests the response dict structure for partial status.""" |
|
|
| def test_response_structure_with_errors(self): |
| """When stage errors exist, response_status should be 'partial'.""" |
| timing_ms = {'spelling_ms': 100, 'grammar_ms': 200, 'punctuation_ms': 150, |
| 'grammar_error': 'TimeoutError: connect timeout'} |
| stage_errors = {k: v for k, v in timing_ms.items() if k.endswith('_error')} |
| response_status = 'partial' if stage_errors else 'success' |
| self.assertEqual(response_status, 'partial') |
| self.assertIn('grammar_error', stage_errors) |
|
|
| def test_response_structure_no_errors(self): |
| """When no stage errors, response_status should be 'success'.""" |
| timing_ms = {'spelling_ms': 100, 'grammar_ms': 200, 'punctuation_ms': 150} |
| stage_errors = {k: v for k, v in timing_ms.items() if k.endswith('_error')} |
| response_status = 'partial' if stage_errors else 'success' |
| self.assertEqual(response_status, 'success') |
| self.assertEqual(len(stage_errors), 0) |
|
|
| def test_suggestions_built_before_status_check(self): |
| """Suggestions list must be populated regardless of stage errors. |
| This confirms partial results are preserved.""" |
| |
| suggestions = [ |
| {'original': 'الى', 'correction': 'إلى', 'type': 'spelling'}, |
| ] |
| timing_ms = {'spelling_ms': 100, 'grammar_error': 'TimeoutError'} |
| stage_errors = {k: v for k, v in timing_ms.items() if k.endswith('_error')} |
| response_status = 'partial' if stage_errors else 'success' |
|
|
| |
| self.assertEqual(response_status, 'partial') |
| self.assertEqual(len(suggestions), 1) |
| self.assertEqual(suggestions[0]['correction'], 'إلى') |
|
|
|
|
| |
| |
| |
| |
|
|
| class TestCommonWordSubstitution(unittest.TestCase): |
| """B2: Valid common words must NOT be replaced by edit-distance-close |
| different valid words. Same failure pattern as rare-vocab destruction.""" |
|
|
| @classmethod |
| def setUpClass(cls): |
| cls.h = _import_app_functions() |
|
|
| def test_bug006_ahm_to_muhm_blocked(self): |
| """BUG-006: اهم must NOT become مهم (ا→م, not orthographic).""" |
| result = self.h._is_small_spelling_change('اهم', 'مهم') |
| self.assertEqual(result, 0.0, |
| "اهم→مهم: non-orthographic char change must be rejected") |
|
|
| def test_bug009_qara_to_qara_hamza(self): |
| """BUG-009: قرأ→قرا — hamza removal. Both IV. ء→ا is in ORTHO_PAIRS, |
| but if both are IV, the IV-IV check applies.""" |
| result = self.h._is_small_spelling_change('قرأ', 'قرا') |
| |
| |
| |
| |
| self.assertIn(result, [0.0, 0.5], |
| "قرأ→قرا: hamza removal between two IV words should be blocked or dampened") |
|
|
| def test_bug010_masha_to_mada_blocked(self): |
| """BUG-010: مشى→مضى (ش→ض, not orthographic).""" |
| result = self.h._is_small_spelling_change('مشى', 'مضى') |
| self.assertEqual(result, 0.0, |
| "مشى→مضى: non-orthographic char change must be rejected") |
|
|
| def test_bug013_khata_to_khata_hamza(self): |
| """BUG-013: خطأ→خطا — hamza removal. Same pattern as BUG-009.""" |
| result = self.h._is_small_spelling_change('خطأ', 'خطا') |
| self.assertIn(result, [0.0, 0.5], |
| "خطأ→خطا: hamza removal between two IV words should be blocked or dampened") |
|
|
|
|
| |
| |
| |
|
|
| class TestSuffixCorruption(unittest.TestCase): |
| """B3: ه→ة at word-final suffix (pronoun position) must be blocked. |
| Same ه↔ة directionality issue as BUG-005, at suffix position.""" |
|
|
| @classmethod |
| def setUpClass(cls): |
| cls.h = _import_app_functions() |
|
|
| def test_bug014_qaraatahu_to_qaraatata(self): |
| """BUG-014: قرأته→قرأتة — ه (pronoun 'him') → ة (ta marbuta). |
| This is a corruption. The ته pattern = pronoun suffix, must be blocked.""" |
| result = self.h._is_small_spelling_change('قرأته', 'قرأتة') |
| self.assertEqual(result, 0.0, |
| "قرأته→قرأتة: pronoun suffix ه→ة must be blocked") |
|
|
| def test_bug015_fataamaltahu_to_fataamaltatah(self): |
| """BUG-015: فتأملته→فتأملتة — same ه→ة suffix corruption.""" |
| result = self.h._is_small_spelling_change('فتأملته', 'فتأملتة') |
| self.assertEqual(result, 0.0, |
| "فتأملته→فتأملتة: pronoun suffix ه→ة must be blocked") |
|
|
| def test_he_to_ta_marbuta_at_suffix_not_noun(self): |
| """General case: ه→ة at word end when ه is pronoun (after verb/preposition) |
| should be treated differently from ه→ة on nouns.""" |
| |
| noun_result = self.h._is_small_spelling_change('المدرسه', 'المدرسة') |
| self.assertGreater(noun_result, 0.0, "Noun ه→ة must be accepted") |
|
|
| |
| |
| verb_result = self.h._is_small_spelling_change('كتبته', 'كتبتة') |
| self.assertEqual(verb_result, 0.0, |
| "Verb+pronoun كتبته→كتبتة must be blocked") |
|
|
|
|
| if __name__ == '__main__': |
| unittest.main() |
|
|