|
| 1 | +"""Tests for the orthography2ipa interface conformance and differential audit. |
| 2 | +
|
| 3 | +Validates: |
| 4 | +- MirandesePhonemizer (and all backends) implement G2PPlugin |
| 5 | +- transcribe() == phonemize_sentence(), transcribe_word() == phonemize() |
| 6 | +- language_codes per dialect |
| 7 | +- differential audit: g2p.json grapheme candidates vs orthography2ipa mwl spec |
| 8 | + (informational gate — agree >= differ; known divergences: j ʒ-vs-ʝ, ç s̻-vs-t͡s) |
| 9 | +""" |
| 10 | +import json |
| 11 | +import os |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +import orthography2ipa |
| 16 | +from orthography2ipa.g2p_plugin import G2PPlugin, WordContext |
| 17 | + |
| 18 | +from mwl_phonemizer.base import Dialects, MirandesePhonemizer, spec_for, DIALECT_TO_SPEC_CODE |
| 19 | +from mwl_phonemizer.orthography_hand_rules import OrthographyRulesMWL |
| 20 | +from mwl_phonemizer.char_lookup_mwl import LookupTableMWL |
| 21 | +from mwl_phonemizer.ngram_mwl import NgramMWLPhonemizer |
| 22 | +from mwl_phonemizer.crf_mwl import CRFPhonemizer |
| 23 | +from mwl_phonemizer.crf_ortho_mwl import CRFOrthoCorrector |
| 24 | + |
| 25 | + |
| 26 | +# --------------------------------------------------------------------------- |
| 27 | +# Interface conformance |
| 28 | +# --------------------------------------------------------------------------- |
| 29 | + |
| 30 | +class TestInterface: |
| 31 | + """All concrete backends must implement the shared G2PPlugin base.""" |
| 32 | + |
| 33 | + @pytest.fixture(params=[ |
| 34 | + "rules", |
| 35 | + "lookup", |
| 36 | + "ngram", |
| 37 | + "crf", |
| 38 | + "crf_ortho", |
| 39 | + ]) |
| 40 | + def plugin(self, request): |
| 41 | + mapping = { |
| 42 | + "rules": OrthographyRulesMWL, |
| 43 | + "lookup": LookupTableMWL, |
| 44 | + "ngram": NgramMWLPhonemizer, |
| 45 | + "crf": CRFPhonemizer, |
| 46 | + "crf_ortho": CRFOrthoCorrector, |
| 47 | + } |
| 48 | + return mapping[request.param]() |
| 49 | + |
| 50 | + def test_implements_g2p_plugin(self, plugin): |
| 51 | + assert isinstance(plugin, G2PPlugin) |
| 52 | + |
| 53 | + def test_language_codes_contains_mwl(self, plugin): |
| 54 | + assert "mwl" in plugin.language_codes |
| 55 | + |
| 56 | + def test_transcribe_equals_phonemize_sentence(self, plugin): |
| 57 | + text = "mui bien" |
| 58 | + assert plugin.transcribe(text) == plugin.phonemize_sentence(text) |
| 59 | + |
| 60 | + def test_transcribe_word_equals_phonemize(self, plugin): |
| 61 | + word = "bien" |
| 62 | + assert plugin.transcribe_word(word) == plugin.phonemize(word) |
| 63 | + |
| 64 | + def test_transcribe_word_accepts_context(self, plugin): |
| 65 | + ctx = WordContext(prev_word="mui", next_word=None) |
| 66 | + result = plugin.transcribe_word("bien", context=ctx) |
| 67 | + assert isinstance(result, str) |
| 68 | + |
| 69 | + |
| 70 | +class TestLanguageCodes: |
| 71 | + def test_central_dialect_codes(self): |
| 72 | + p = OrthographyRulesMWL(dialect=Dialects.CENTRAL) |
| 73 | + assert p.language_codes == ["mwl"] |
| 74 | + |
| 75 | + def test_raiano_dialect_codes(self): |
| 76 | + p = OrthographyRulesMWL(dialect=Dialects.RAIANO) |
| 77 | + # RAIANO has no dedicated spec yet; maps to base mwl |
| 78 | + assert p.language_codes == ["mwl"] |
| 79 | + |
| 80 | + def test_sendinese_dialect_codes(self): |
| 81 | + p = OrthographyRulesMWL(dialect=Dialects.SENDINESE) |
| 82 | + assert "mwl" in p.language_codes |
| 83 | + assert "mwl-x-sendim" in p.language_codes |
| 84 | + |
| 85 | + |
| 86 | +# --------------------------------------------------------------------------- |
| 87 | +# Dialect ↔ spec helper |
| 88 | +# --------------------------------------------------------------------------- |
| 89 | + |
| 90 | +class TestSpecFor: |
| 91 | + def test_central_returns_mwl_spec(self): |
| 92 | + spec = spec_for(Dialects.CENTRAL) |
| 93 | + assert spec.code == "mwl" |
| 94 | + |
| 95 | + def test_sendinese_returns_sendim_spec(self): |
| 96 | + spec = spec_for(Dialects.SENDINESE) |
| 97 | + assert spec.code == "mwl-x-sendim" |
| 98 | + |
| 99 | + def test_raiano_returns_mwl_spec(self): |
| 100 | + spec = spec_for(Dialects.RAIANO) |
| 101 | + assert spec.code == "mwl" |
| 102 | + |
| 103 | + def test_dialect_to_spec_code_complete(self): |
| 104 | + for d in Dialects: |
| 105 | + assert d in DIALECT_TO_SPEC_CODE, f"Dialect {d} missing from DIALECT_TO_SPEC_CODE" |
| 106 | + |
| 107 | + |
| 108 | +# --------------------------------------------------------------------------- |
| 109 | +# Differential audit |
| 110 | +# --------------------------------------------------------------------------- |
| 111 | + |
| 112 | +class TestDifferentialGraphemes: |
| 113 | + """Audit g2p.json grapheme candidates against the orthography2ipa mwl spec. |
| 114 | +
|
| 115 | + Informational gate: the local g2p.json table stays authoritative; |
| 116 | + this test pins the agreement level so drift on either side is visible. |
| 117 | +
|
| 118 | + Known divergences being adjudicated upstream (Phase M0): |
| 119 | + - j: g2p.json has ʒ, spec has ʝ |
| 120 | + - ç: g2p.json has z̻, spec has t͡s (Mirandese affricate vs. fricative) |
| 121 | + These are expected and do NOT cause a test failure as long as agree >= differ. |
| 122 | + """ |
| 123 | + |
| 124 | + def test_graphemes_agree_with_spec(self, capsys): |
| 125 | + g2p_path = os.path.join(os.path.dirname(__file__), "..", "mwl_phonemizer", "g2p.json") |
| 126 | + with open(g2p_path, encoding="utf-8") as f: |
| 127 | + g2p = json.load(f) |
| 128 | + |
| 129 | + spec = orthography2ipa.get("mwl") |
| 130 | + agree, differ = [], [] |
| 131 | + |
| 132 | + for grapheme, candidates in g2p.items(): |
| 133 | + spec_candidates = spec.graphemes.get(grapheme) |
| 134 | + if spec_candidates is None: |
| 135 | + continue # multi-char or word-specific entry not in spec — skip |
| 136 | + # agree if at least one candidate appears in the spec set |
| 137 | + shared = [c for c in candidates if c in spec_candidates] |
| 138 | + if shared: |
| 139 | + agree.append((grapheme, candidates, spec_candidates)) |
| 140 | + else: |
| 141 | + differ.append((grapheme, candidates, spec_candidates)) |
| 142 | + |
| 143 | + with capsys.disabled(): |
| 144 | + print(f"\n[differential audit] agree={len(agree)} differ={len(differ)}") |
| 145 | + if differ: |
| 146 | + print("Divergences (g2p.json vs spec):") |
| 147 | + for g, local, remote in differ: |
| 148 | + print(f" {g!r:12s} local={local} spec={remote}") |
| 149 | + |
| 150 | + assert len(agree) >= len(differ), ( |
| 151 | + f"g2p.json and the mwl spec diverged too much: " |
| 152 | + f"agree={len(agree)}, differ={len(differ)}\n" |
| 153 | + f"Divergences: {[(g, l, r) for g, l, r in differ]}" |
| 154 | + ) |
0 commit comments