File size: 5,915 Bytes
f9bc0c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""Canonical writing-system profiles for Newari / Nepal Bhasa OCR.

The profiles distinguish source-image appearance from OCR transcription encoding.
That distinction matters for Bhujimol and Ranjana: neither may be treated as a
Prachalit visual model merely because its transcription can use an encoded script.
"""

from __future__ import annotations

import re
from dataclasses import dataclass


ScriptCodepointRange = tuple[int, int]

DEVANAGARI_RANGES: tuple[ScriptCodepointRange, ...] = (
    (0x0900, 0x097F),
    (0xA8E0, 0xA8FF),
)
NEWA_RANGES: tuple[ScriptCodepointRange, ...] = ((0x11400, 0x1147F),)


@dataclass(frozen=True, slots=True)
class NewariWritingSystemProfile:
    """Static encoding and routing semantics for one Newari writing system."""

    profile_id: str
    display_name: str
    aliases: tuple[str, ...]
    source_unicode_status: str
    source_unicode_script: str | None
    source_unicode_ranges: tuple[ScriptCodepointRange, ...]
    transcription_unicode_script: str
    transcription_unicode_ranges: tuple[ScriptCodepointRange, ...]
    route_script: str
    visual_profile_required: bool
    input_policy: str
    output_policy: str


NEWARI_WRITING_SYSTEM_PROFILES: tuple[NewariWritingSystemProfile, ...] = (
    NewariWritingSystemProfile(
        profile_id="newa_prachalit",
        display_name="Newar / Prachalit",
        aliases=(
            "newa",
            "newar",
            "newar script",
            "newari script",
            "nepal lipi",
            "nepaalalipi",
            "prachalit",
            "prachalit newa",
        ),
        source_unicode_status="encoded",
        source_unicode_script="Newa",
        source_unicode_ranges=NEWA_RANGES,
        transcription_unicode_script="Newa",
        transcription_unicode_ranges=NEWA_RANGES,
        route_script="newa",
        visual_profile_required=True,
        input_policy="unicode_text_or_source_image",
        output_policy="native_newa_unicode",
    ),
    NewariWritingSystemProfile(
        profile_id="bhujimol",
        display_name="Bhujimol / Bhujinmol",
        aliases=("bhujimol", "bhujinmol", "bhujimol script", "bhujinmol script"),
        source_unicode_status="no_separate_script_encoding",
        source_unicode_script="Newa",
        source_unicode_ranges=NEWA_RANGES,
        transcription_unicode_script="Newa",
        transcription_unicode_ranges=NEWA_RANGES,
        route_script="bhujimol",
        visual_profile_required=True,
        input_policy="source_image_or_newa_text_with_explicit_bhujimol_style",
        output_policy="newa_unicode_transcription_with_source_style_metadata",
    ),
    NewariWritingSystemProfile(
        profile_id="ranjana",
        display_name="Ranjana",
        aliases=("ranjana", "ranjana script", "ranjana lipi", "rañjana", "rañjanā"),
        source_unicode_status="unencoded",
        source_unicode_script=None,
        source_unicode_ranges=(),
        transcription_unicode_script="Newa",
        transcription_unicode_ranges=NEWA_RANGES,
        route_script="ranjana",
        visual_profile_required=True,
        input_policy="source_image_or_audited_legacy_font_mapping_only",
        output_policy="newa_unicode_transcription_with_source_image_and_ranjana_style_metadata",
    ),
    NewariWritingSystemProfile(
        profile_id="devanagari",
        display_name="Devanagari",
        aliases=("devanagari", "deva", "devanagari script", "देवनागरी"),
        source_unicode_status="encoded",
        source_unicode_script="Devanagari",
        source_unicode_ranges=DEVANAGARI_RANGES,
        transcription_unicode_script="Devanagari",
        transcription_unicode_ranges=DEVANAGARI_RANGES,
        route_script="devanagari",
        visual_profile_required=True,
        input_policy="unicode_text_or_source_image",
        output_policy="native_devanagari_unicode",
    ),
)


def _normalize_alias(value: str) -> str:
    return re.sub(r"[\s_/-]+", " ", value.strip().casefold())


def _build_alias_index() -> dict[str, NewariWritingSystemProfile]:
    aliases: dict[str, NewariWritingSystemProfile] = {}
    for profile in NEWARI_WRITING_SYSTEM_PROFILES:
        values = (profile.profile_id, profile.display_name, *profile.aliases)
        for value in values:
            normalized = _normalize_alias(value)
            previous = aliases.get(normalized)
            if previous is not None and previous.profile_id != profile.profile_id:
                raise RuntimeError(
                    f"ambiguous Newari writing-system alias {value!r}: "
                    f"{previous.profile_id!r} and {profile.profile_id!r}"
                )
            aliases[normalized] = profile
    return aliases


NEWARI_WRITING_SYSTEM_ALIASES = _build_alias_index()
NEWARI_WRITING_SYSTEM_BY_ID = {profile.profile_id: profile for profile in NEWARI_WRITING_SYSTEM_PROFILES}

# These keys score a recognizer's Unicode transcription, not the visual identity
# of the source glyphs. In particular, Ranjana remains unencoded.
NEWARI_TRANSCRIPTION_ROUTING_RANGES: dict[str, tuple[ScriptCodepointRange, ...]] = {
    "newar": NEWA_RANGES,
    "newar-script": NEWA_RANGES,
    "newa": NEWA_RANGES,
    "prachalit": NEWA_RANGES,
    "newa/prachalit": NEWA_RANGES,
    "newa-prachalit": NEWA_RANGES,
    "bhujimol": NEWA_RANGES,
    "bhujinmol": NEWA_RANGES,
    "ranjana": NEWA_RANGES,
    "ranjana-newa-transcription": NEWA_RANGES,
}


def resolve_newari_writing_system(value: str) -> NewariWritingSystemProfile:
    """Resolve a user/config alias to one of the four supported profiles."""

    profile = NEWARI_WRITING_SYSTEM_ALIASES.get(_normalize_alias(value))
    if profile is None:
        supported = ", ".join(profile.profile_id for profile in NEWARI_WRITING_SYSTEM_PROFILES)
        raise ValueError(f"unsupported Newari writing system {value!r}; supported profiles: {supported}")
    return profile