File size: 32,015 Bytes
c02bc8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
"""
Symphonym v7 β€” Standalone Inference
====================================
Loads the Student (UniversalEncoder) model and computes phonetic embeddings
for toponyms from any script.  No G2P or IPA transcription required at
inference time.

Usage
-----
    from inference import SymphonymModel

    model = SymphonymModel()                        # loads from this directory
    emb   = model.embed("London", lang="en")        # (128,) numpy array
    sim   = model.similarity("London", "en",
                             "Π›ΠΎΠ½Π΄ΠΎΠ½", "ru")        # cosine similarity
    pairs = model.batch_embed([
        ("London", "en"),
        ("Π›ΠΎΠ½Π΄ΠΎΠ½", "ru"),
        ("伦敦",   "zh"),
    ])
"""

from __future__ import annotations

import json
import math
import os
from pathlib import Path
from typing import List, Optional, Tuple, Union

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

# ---------------------------------------------------------------------------
# Minimal architecture (copy of UniversalEncoder from models/models.py)
# Keep in sync with the training code if re-training.
# ---------------------------------------------------------------------------

class SelfAttention(nn.Module):
    def __init__(self, hidden_dim: int, num_heads: int = 2, dropout: float = 0.1):
        super().__init__()
        assert hidden_dim % num_heads == 0
        self.num_heads = num_heads
        self.head_dim  = hidden_dim // num_heads
        self.scale     = math.sqrt(self.head_dim)
        self.q_proj  = nn.Linear(hidden_dim, hidden_dim)
        self.k_proj  = nn.Linear(hidden_dim, hidden_dim)
        self.v_proj  = nn.Linear(hidden_dim, hidden_dim)
        self.out_proj = nn.Linear(hidden_dim, hidden_dim)
        self.dropout  = nn.Dropout(dropout)

    def forward(self, x, mask=None):
        B, L, H = x.shape
        def reshape(t):
            return t.view(B, L, self.num_heads, self.head_dim).transpose(1, 2)
        Q, K, V = reshape(self.q_proj(x)), reshape(self.k_proj(x)), reshape(self.v_proj(x))
        scores = torch.matmul(Q, K.transpose(-2, -1)) / self.scale
        if mask is not None:
            scores = scores.masked_fill(~mask[:, None, None, :], float("-inf"))
        w = self.dropout(F.softmax(scores, dim=-1))
        out = torch.matmul(w, V).transpose(1, 2).contiguous().view(B, L, H)
        return self.out_proj(out), w


class AttentionPooling(nn.Module):
    def __init__(self, hidden_dim: int, dropout: float = 0.1):
        super().__init__()
        self.attention = nn.Sequential(
            nn.Linear(hidden_dim, hidden_dim // 2),
            nn.Tanh(),
            nn.Dropout(dropout),
            nn.Linear(hidden_dim // 2, 1),
        )

    def forward(self, x, mask=None):
        scores = self.attention(x).squeeze(-1)
        if mask is not None:
            scores = scores.masked_fill(~mask, float("-inf"))
        w = F.softmax(scores, dim=-1)
        return torch.bmm(w.unsqueeze(1), x).squeeze(1), w


class UniversalEncoder(nn.Module):
    """Symphonym Student: script-/language-conditioned character encoder."""

    def __init__(
        self,
        vocab_size: int        = 113280,
        num_scripts: int       = 20,
        num_langs: int         = 1944,
        char_embed_dim: int    = 64,
        script_embed_dim: int  = 16,
        lang_embed_dim: int    = 16,
        hidden_dim: int        = 128,
        embed_dim: int         = 128,
        num_layers: int        = 2,
        num_attention_heads: int = 2,
        dropout: float         = 0.2,
        lang_dropout: float    = 0.5,
        num_length_buckets: int = 16,
        length_embed_dim: int  = 8,
    ):
        super().__init__()
        self.embed_dim          = embed_dim
        self.lang_dropout_rate  = lang_dropout
        self.num_length_buckets = num_length_buckets

        self.char_embed   = nn.Embedding(vocab_size,   char_embed_dim,   padding_idx=0)
        self.script_embed = nn.Embedding(num_scripts,  script_embed_dim)
        self.lang_embed   = nn.Embedding(num_langs,    lang_embed_dim,   padding_idx=0)
        self.length_embed = nn.Embedding(num_length_buckets, length_embed_dim)

        input_dim = char_embed_dim + script_embed_dim + lang_embed_dim + length_embed_dim
        self.input_proj = nn.Linear(input_dim, hidden_dim)
        self.input_norm = nn.LayerNorm(hidden_dim)

        self.bilstm = nn.LSTM(
            hidden_dim, hidden_dim, num_layers=num_layers,
            batch_first=True, bidirectional=True,
            dropout=dropout if num_layers > 1 else 0,
        )
        self.self_attention = SelfAttention(hidden_dim * 2, num_attention_heads, dropout)
        self.pooling        = AttentionPooling(hidden_dim * 2, dropout)
        self.output_proj    = nn.Sequential(
            nn.Linear(hidden_dim * 2, hidden_dim),
            nn.ReLU(),
            nn.Dropout(dropout),
            nn.Linear(hidden_dim, embed_dim),
            nn.LayerNorm(embed_dim),
        )

    def _length_bucket(self, lengths: torch.Tensor) -> torch.Tensor:
        buckets = (lengths.to(torch.long) - 1) // 2
        return buckets.clamp(0, self.num_length_buckets - 1)

    def forward(self, char_ids, script_ids, lang_ids, lengths):
        B, L    = char_ids.shape
        device  = char_ids.device
        mask    = torch.arange(L, device=device).unsqueeze(0) < lengths.to(device).unsqueeze(1)

        c_emb = self.char_embed(char_ids)
        s_emb = self.script_embed(script_ids).unsqueeze(1).expand(-1, L, -1)
        l_emb = self.lang_embed(lang_ids).unsqueeze(1).expand(-1, L, -1)
        lb    = self._length_bucket(lengths)
        len_emb = self.length_embed(lb.to(device)).unsqueeze(1).expand(-1, L, -1)

        x = torch.cat([c_emb, s_emb, l_emb, len_emb], dim=-1)
        x = self.input_norm(self.input_proj(x))

        packed   = nn.utils.rnn.pack_padded_sequence(x, lengths.cpu(), batch_first=True, enforce_sorted=False)
        lstm_out, _ = self.bilstm(packed)
        lstm_out, _ = nn.utils.rnn.pad_packed_sequence(lstm_out, batch_first=True, total_length=L)

        attended, _ = self.self_attention(lstm_out, mask)
        attended    = attended + lstm_out
        pooled,   _ = self.pooling(attended, mask)
        emb         = self.output_proj(pooled)
        return F.normalize(emb, p=2, dim=-1)


# ---------------------------------------------------------------------------
# Tokeniser β€” THE CANONICAL IMPLEMENTATION, vendored from phonetics/tokenise.py
# ---------------------------------------------------------------------------
#
# This file ships to HuggingFace and must stay importable on its own, so it
# cannot import from the repo. The block between the two markers below is
# therefore a character-for-character copy of the block of the same name in
# `phonetics/tokenise.py`, and `tests/test_tokeniser_contract.py` asserts the
# two are byte-identical. Change the repo copy, then re-vendor β€” do not edit
# one side alone.
#
# What it fixes: until 5 September 2026 this file tokenised raw codepoints β€”
# no CJK/Kana romanisation, no Hangul→Jamo, no NFC, and `' '` resolving to
# vocab id 12588 instead of `<SPACE>` (2). The `toponyms` index was embedded
# through `CharacterVocabulary.encode`, which does all four, so the gateway was
# querying the index in a different tokenisation from the one it was written
# in: cos(indexed, queried) 0.9691 for "New York", 0.9429 for
# "Bury St Edmunds", and -0.3036 for "東京". Multi-word names retrieved their
# own document at rank 1 only 65.7% of the time.
# See `developer/plan-symphonym-v8.md` Β§2.

# --- BEGIN CANONICAL TOKENISER ---
# CANONICAL-BLOCK v2 sha256=db9cefd56b145b3ce73d32313e4f2e7175c9b6fa0e6960712e8eec2ebfc122f8
# CANONICAL-BLOCK Convention, stated because the same block has been hashed two different
# CANONICAL-BLOCK ways elsewhere and nothing ever compared them: sha256 over this block
# CANONICAL-BLOCK INCLUDING both marker lines and EXCLUDING every line beginning
# CANONICAL-BLOCK "# CANONICAL-BLOCK". Recompute with that rule or you will get a different
# CANONICAL-BLOCK answer and no error.
# CANONICAL-BLOCK What this DOES: identifies which version of the block a copy carries, for
# CANONICAL-BLOCK a consumer who does not have the indexing repo on disk (whg3 and
# CANONICAL-BLOCK London_Customs_Accounts both vendor this block).
# CANONICAL-BLOCK What it does NOT do: a vendored copy carries the ORIGINAL's stamp, so
# CANONICAL-BLOCK checking your own copy detects local modification, not that upstream has
# CANONICAL-BLOCK moved. For that you must compare this value against upstream's.
# CANONICAL-BLOCK THE CASE THAT DEFEATS THE TRIPWIRE, and it is the LIKELY one: edit the block
# CANONICAL-BLOCK and correctly re-stamp it. The test below is then satisfied, this stamp is
# CANONICAL-BLOCK honest, and a consumer checking only that upstream's stamp matches upstream's
# CANONICAL-BLOCK block learns NOTHING. A consumer needs a SECOND witness -- the stamp value it
# CANONICAL-BLOCK was ported from, recorded as a constant on its side -- and only that one
# CANONICAL-BLOCK catches a legitimate upstream change. whg3 does this (2fe733d0c) and proved
# CANONICAL-BLOCK all three modes fire, including the re-stamped one, which is the mode a
# CANONICAL-BLOCK single-witness check silently passes.

import unicodedata
from typing import Dict, List, Optional, Tuple

try:  # anyascii is required only for CJK/Kana romanisation
    from anyascii import anyascii as _anyascii
except ImportError:  # pragma: no cover - exercised only where anyascii is absent
    _anyascii = None

PAD_ID = 0
UNK_ID = 1
SPACE_ID = 2
LANG_UNK_ID = 0

# Unicode blocks per script, in the SAME ORDER as
# `phonetics.utils.script_detection.SCRIPT_RANGES`. The order is load-bearing:
# the ranges overlap (Armenian ligatures sit inside the Hebrew presentation
# block) and the later entry wins, so a reordering silently reclassifies
# characters. The contract test compares the resulting codepoint map with the
# one script_detection builds, entry for entry.
_SCRIPT_UNICODE_RANGES: List[Tuple[str, List[Tuple[int, int]]]] = [
    ("LATIN", [(0x0000, 0x007F), (0x0080, 0x00FF), (0x0100, 0x017F),
               (0x0180, 0x024F), (0x0250, 0x02AF), (0x1D00, 0x1D7F),
               (0x1E00, 0x1EFF), (0x2C60, 0x2C7F), (0xA720, 0xA7FF),
               (0xAB30, 0xAB6F)]),
    ("CYRILLIC", [(0x0400, 0x04FF), (0x0500, 0x052F), (0x2DE0, 0x2DFF),
                  (0xA640, 0xA69F)]),
    ("GREEK", [(0x0370, 0x03FF), (0x1F00, 0x1FFF)]),
    ("ARABIC", [(0x0600, 0x06FF), (0x0750, 0x077F), (0x08A0, 0x08FF),
                (0xFB50, 0xFDFF), (0xFE70, 0xFEFF)]),
    ("HEBREW", [(0x0590, 0x05FF), (0xFB00, 0xFB4F)]),
    ("DEVANAGARI", [(0x0900, 0x097F), (0xA8E0, 0xA8FF)]),
    ("BENGALI", [(0x0980, 0x09FF)]),
    ("TAMIL", [(0x0B80, 0x0BFF)]),
    ("TELUGU", [(0x0C00, 0x0C7F)]),
    ("MALAYALAM", [(0x0D00, 0x0D7F)]),
    ("KANNADA", [(0x0C80, 0x0CFF)]),
    ("GUJARATI", [(0x0A80, 0x0AFF)]),
    ("THAI", [(0x0E00, 0x0E7F)]),
    ("GEORGIAN", [(0x10A0, 0x10FF), (0x2D00, 0x2D2F)]),
    ("ARMENIAN", [(0x0530, 0x058F), (0xFB00, 0xFB17)]),
    ("HANGUL", [(0xAC00, 0xD7AF), (0x1100, 0x11FF), (0x3130, 0x318F),
                (0xA960, 0xA97F), (0xD7B0, 0xD7FF)]),
    ("CJK", [(0x4E00, 0x9FFF), (0x3400, 0x4DBF), (0x20000, 0x2A6DF),
             (0x2A700, 0x2B73F), (0x2B740, 0x2B81F), (0x2B820, 0x2CEAF),
             (0x2CEB0, 0x2EBEF), (0x30000, 0x3134F), (0x3000, 0x303F),
             (0xF900, 0xFAFF)]),
    ("HIRAGANA", [(0x3040, 0x309F), (0x1B000, 0x1B0FF)]),
    ("KATAKANA", [(0x30A0, 0x30FF), (0x31F0, 0x31FF), (0xFF65, 0xFF9F)]),
    # ⚠ Kept in step with `script_detection.SCRIPT_RANGES`, same order, same
    # blocks β€” `test_tokeniser_contract` compares the two codepoint maps entry
    # for entry, so a divergence here is a test failure rather than a silent
    # reclassification.
    #
    # πŸ›‘ A PORT OF THESE RANGES IS REQUIRED, NOT OPTIONAL. An earlier version of
    # this comment said the model was UNAFFECTED because `encode_script` falls
    # back to OTHER for a script the vocabulary does not carry. That was true of
    # the 20-entry v7 `script_vocab.json` it was written against and is FALSE of
    # the 37-entry v8 one: the fallback needs the script to be ABSENT, and in v8
    # these scripts are PRESENT. Measured 15 Sep 2026 against the shipped v8
    # vocab β€” Gurmukhi 21, Myanmar 20, Tibetan 22, Khmer 24, Ethiopic 27, Lao 29,
    # where a detector still carrying only the original 19 ranges sends OTHER=19.
    #
    # So an unported client conditions the model differently from the index, on
    # 3,168 codepoints across 17 scripts, and nothing raises. Two readers β€” one
    # of them the author β€” used the old sentence to justify deferring the port.
    # ⚠ A claim whose scope was a file that has since changed becomes an
    # unconditional claim by attrition.
    ("MYANMAR", [(0x1000, 0x109F), (0xA9E0, 0xA9FF), (0xAA60, 0xAA7F)]),
    ("GURMUKHI", [(0x0A00, 0x0A7F)]),
    ("TIBETAN", [(0x0F00, 0x0FFF)]),
    ("SINHALA", [(0x0D80, 0x0DFF)]),
    ("KHMER", [(0x1780, 0x17FF), (0x19E0, 0x19FF)]),
    ("OL_CHIKI", [(0x1C50, 0x1C7F)]),
    ("TIFINAGH", [(0x2D30, 0x2D7F)]),
    ("ETHIOPIC", [(0x1200, 0x137F), (0x1380, 0x139F), (0x2D80, 0x2DDF), (0xAB00, 0xAB2F)]),
    ("ORIYA", [(0x0B00, 0x0B7F)]),
    ("LAO", [(0x0E80, 0x0EFF)]),
    ("MONGOLIAN", [(0x1800, 0x18AF)]),
    ("CANADIAN_ABORIGINAL", [(0x1400, 0x167F), (0x18B0, 0x18FF)]),
    ("BOPOMOFO", [(0x3100, 0x312F), (0x31A0, 0x31BF)]),
    ("THAANA", [(0x0780, 0x07BF)]),
    ("NKO", [(0x07C0, 0x07FF)]),
    ("SYRIAC", [(0x0700, 0x074F), (0x0860, 0x086F)]),
    ("COPTIC", [(0x2C80, 0x2CFF)]),
]

SCRIPT_OTHER = "OTHER"

# Romanised via anyascii; decomposed to Jamo. Both lists are what the index was
# built with β€” changing either invalidates 72.7M stored vectors.
_ROMANISE_SCRIPTS = frozenset({"CJK", "HIRAGANA", "KATAKANA"})
_DECOMPOSE_SCRIPTS = frozenset({"HANGUL"})


def _build_codepoint_map() -> Dict[int, str]:
    codepoint_map: Dict[int, str] = {}
    for script, ranges in _SCRIPT_UNICODE_RANGES:
        for start, end in ranges:
            for cp in range(start, end + 1):
                codepoint_map[cp] = script
    return codepoint_map


_CODEPOINT_MAP = _build_codepoint_map()

# --- Hangul β†’ Jamo -------------------------------------------------------
_HANGUL_START = 0xAC00
_HANGUL_END = 0xD7A3
_N_VOWEL = 21
_N_TAIL = 28
_CHOSEONG = ['γ„±', 'γ„²', 'γ„΄', 'γ„·', 'γ„Έ', 'γ„Ή', 'ㅁ', 'γ…‚', 'γ…ƒ', 'γ……',
             'γ…†', 'γ…‡', 'γ…ˆ', 'γ…‰', 'γ…Š', 'γ…‹', 'γ…Œ', 'ㅍ', 'γ…Ž']
_JUNGSEONG = ['ㅏ', 'ㅐ', 'γ…‘', 'γ…’', 'γ…“', 'γ…”', 'γ…•', 'γ…–', 'γ…—', 'γ…˜',
              'γ…™', 'γ…š', 'γ…›', 'γ…œ', 'ㅝ', 'γ…ž', 'γ…Ÿ', 'γ… ', 'γ…‘', 'γ…’', 'γ…£']
_JONGSEONG = ['', 'γ„±', 'γ„²', 'γ„³', 'γ„΄', 'γ„΅', 'γ„Ά', 'γ„·', 'γ„Ή', 'γ„Ί',
              'γ„»', 'γ„Ό', 'γ„½', 'γ„Ύ', 'γ„Ώ', 'γ…€', 'ㅁ', 'γ…‚', 'γ…„', 'γ……',
              'γ…†', 'γ…‡', 'γ…ˆ', 'γ…Š', 'γ…‹', 'γ…Œ', 'ㅍ', 'γ…Ž']


def decompose_hangul(text: str) -> str:
    """Hangul syllables β†’ Jamo; every other character passes through."""
    out: List[str] = []
    for char in text:
        code = ord(char)
        if _HANGUL_START <= code <= _HANGUL_END:
            code -= _HANGUL_START
            out.append(_CHOSEONG[code // (_N_VOWEL * _N_TAIL)])
            out.append(_JUNGSEONG[(code % (_N_VOWEL * _N_TAIL)) // _N_TAIL])
            tail = code % _N_TAIL
            if tail:
                out.append(_JONGSEONG[tail])
        else:
            out.append(char)
    return ''.join(out)


def detect_script(text: str) -> str:
    """The dominant script name, counting only alphabetic characters.

    Ties go to the script seen first, which is what `collections.Counter`'s
    `most_common(1)` does in the vocabulary implementation.
    """
    counts: Dict[str, int] = {}
    for char in text:
        if not char.isalpha():
            continue
        script = _CODEPOINT_MAP.get(ord(char), SCRIPT_OTHER)
        counts[script] = counts.get(script, 0) + 1
    if not counts:
        return SCRIPT_OTHER
    return max(counts.items(), key=lambda kv: kv[1])[0]


def preprocess_text(text: str, script: Optional[str] = None) -> str:
    """Romanise CJK/Kana, decompose Hangul, NFC-normalise everything else."""
    if script is None:
        script = detect_script(text)

    if script in _ROMANISE_SCRIPTS:
        if _anyascii is None:
            raise RuntimeError("anyascii required for CJK romanization")
        return _anyascii(text).lower()

    if script in _DECOMPOSE_SCRIPTS:
        return decompose_hangul(text)

    return unicodedata.normalize('NFC', text)


def encode_chars(
    text: str,
    char_to_id: Dict[str, int],
    script: Optional[str] = None,
    max_length: Optional[int] = None,
) -> List[int]:
    """Preprocessed text β†’ character ids, with growth disabled.

    ``' '`` becomes ``<SPACE>`` (2) and never a dictionary lookup β€” the vocab
    file happens to carry a literal space at id 12588, which is the row the
    gateway was emitting and the training tokeniser can never produce. Other
    whitespace is dropped. An id at or beyond the vocabulary's own size is
    vocabulary corruption and degrades to ``<UNK>`` rather than indexing off
    the end of the embedding table.

    THE ONE DELIBERATE DEVIATION from ``CharacterVocabulary.encode``: an empty
    result becomes ``[<UNK>]`` rather than ``[]``. A zero-length sequence is
    not merely useless, it is fatal β€” ``pack_padded_sequence`` raises
    ``Cannot pack empty tensors``, and in a batch **one** such item takes the
    whole batch down with ``Length of all samples has to be greater than 0``.
    That already happens today for an empty query (a 500 from ``/api/embed``);
    dropping non-space whitespace widens the class from ``""`` alone to any
    whitespace-only input, so the guard ships with the change that widens it.
    ``<UNK>`` and not ``<SPACE>``: it says "input I cannot represent", which is
    true, where ``<SPACE>`` would assert the input was a space.

    It cannot affect a name that produces any id at all, so it cannot alter a
    single-word Latin name and cannot force a re-embed. Nor can it disagree
    with the index: ``update_es.py`` filters ``name IS NOT NULL AND TRIM(name)
    != ''``, so no indexed document is whitespace-only.
    """
    vocab_size = len(char_to_id)
    ids: List[int] = []
    for char in preprocess_text(text, script):
        if char == ' ':
            ids.append(SPACE_ID)
        elif not char.strip():
            continue
        else:
            cid = char_to_id.get(char)
            if cid is None or cid >= vocab_size:
                cid = UNK_ID
            ids.append(cid)

    if max_length is not None and len(ids) > max_length:
        ids = ids[:max_length]
    if not ids:
        return [UNK_ID]
    return ids


def encode_lang(lang: Optional[str], lang_to_id: Dict[str, int]) -> int:
    """Language tag β†’ id. Lowercased and stripped before lookup."""
    if lang is None or lang == '':
        return LANG_UNK_ID
    return lang_to_id.get(lang.lower().strip(), LANG_UNK_ID)


def encode_script(script: str, script_to_id: Dict[str, int]) -> int:
    """Script name β†’ id, falling back to OTHER and then to 0.

    The fallback is the point. The pre-fix path did
    ``script_to_id.get(script_name, 0)`` β€” and 0 is not a sentinel, it is
    **LATIN**. So a script the DETECTOR could name but the VOCABULARY could not
    represent was silently embedded as Latin. That is not hypothetical: the
    old detector knew GURMUKHI, the 20-script vocabulary does not, and twelve
    Punjabi documents in production were embedded as Latin because of it β€”
    identified by their stored vectors matching a script-id-0 recomputation at
    cosine 1.0000 exactly. Falling back to a genuine OTHER repaired that as a
    side effect of the rewrite; nobody designed it.

    ⚠ The detector's table and this vocabulary must be kept in step, and the two
    directions are NOT equally visible:

      * a script MISSING from the range table is behaviourally detectable β€” it
        has a vocab id, so its text moves from (say) THAI to OTHER;
      * a script EXTRA in the range table is INVISIBLE β€” it has no vocab id, so
        the fallback lands on OTHER, which is where it already was.

    So a mutation test cannot find an extra script, and neither can a
    differential corpus generated from the implementer's own table: it would
    never generate the cases. Only comparing the two tables directly sees both
    directions. (Measured: mutating GURMUKHI into the range table produces 0 of
    6,271 differences.)
    """
    if script in script_to_id:
        return script_to_id[script]
    return script_to_id.get(SCRIPT_OTHER, 0)


def tokenise(
    text: str,
    lang: Optional[str],
    char_to_id: Dict[str, int],
    lang_to_id: Dict[str, int],
    script_to_id: Dict[str, int],
    script: Optional[str] = None,
    max_length: Optional[int] = None,
) -> Tuple[List[int], int, int]:
    """``(text, lang)`` β†’ ``(char_ids, script_id, lang_id)``.

    The single entry point every caller should use. No torch, no model.
    """
    if script is None:
        script = detect_script(text)
    char_ids = encode_chars(text, char_to_id, script, max_length)
    return char_ids, encode_script(script, script_to_id), encode_lang(lang, lang_to_id)

# --- END CANONICAL TOKENISER ---


# ---------------------------------------------------------------------------
# Main model class
# ---------------------------------------------------------------------------

class SymphonymModel:
    """
    High-level wrapper for Symphonym v7 inference.

    Parameters
    ----------
    model_dir : str or Path, optional
        Directory containing ``model.safetensors`` (or ``final_model.pt``),
        ``vocab/char_vocab.json``, ``vocab/lang_vocab.json``, and
        ``vocab/script_vocab.json``.  Defaults to the directory of this file.
    device : str, optional
        ``"cpu"`` (default) or ``"cuda"``.

    Examples
    --------
    >>> model = SymphonymModel()
    >>> model.similarity("London", "en", "Π›ΠΎΠ½Π΄ΠΎΠ½", "ru")
    0.991
    >>> embeddings = model.batch_embed([("London", "en"), ("Π›ΠΎΠ½Π΄ΠΎΠ½", "ru")])
    >>> embeddings.shape
    (2, 128)
    """

    def __init__(
        self,
        model_dir: Union[str, Path, None] = None,
        device: str = "cpu",
    ):
        if model_dir is None:
            model_dir = Path(__file__).parent
        model_dir = Path(model_dir)

        self.device = torch.device(device)

        # --- Load vocabularies ---
        vocab_dir = model_dir / "vocab"
        with open(vocab_dir / "char_vocab.json") as f:
            cv = json.load(f)
        with open(vocab_dir / "lang_vocab.json") as f:
            lv = json.load(f)
        with open(vocab_dir / "script_vocab.json") as f:
            sv = json.load(f)

        self._char_to_id:   dict[str, int] = cv.get("char_to_id",   cv)
        self._lang_to_id:   dict[str, int] = lv.get("lang_to_id",   lv)
        self._script_to_id: dict[str, int] = sv.get("script_to_id", sv)

        # --- Build model from config ---
        cfg_path = model_dir / "config.json"
        with open(cfg_path) as f:
            cfg = json.load(f)

        self._model = UniversalEncoder(
            vocab_size          = cfg.get("vocab_size",          len(self._char_to_id) + 1),
            num_scripts         = cfg.get("num_scripts",         25),
            num_langs           = cfg.get("num_langs",           len(self._lang_to_id) + 1),
            char_embed_dim      = cfg.get("char_embed_dim",      64),
            script_embed_dim    = cfg.get("script_embed_dim",    16),
            lang_embed_dim      = cfg.get("lang_embed_dim",      16),
            hidden_dim          = cfg.get("hidden_dim",          128),
            embed_dim           = cfg.get("embed_dim",           128),
            num_layers          = cfg.get("num_layers",          2),
            num_attention_heads = cfg.get("num_attention_heads", 2),
            dropout             = cfg.get("dropout",             0.2),
            lang_dropout        = cfg.get("lang_dropout",        0.5),
            num_length_buckets  = cfg.get("num_length_buckets",  16),
            length_embed_dim    = cfg.get("length_embed_dim",    8),
        )

        # --- Load weights (prefer safetensors, fall back to .pt) ---
        st_path = model_dir / "model.safetensors"
        pt_path = model_dir / "final_model.pt"
        if st_path.exists():
            from safetensors.torch import load_file
            state = load_file(str(st_path), device=str(self.device))
            self._model.load_state_dict(state)
        elif pt_path.exists():
            ckpt = torch.load(str(pt_path), map_location=self.device)
            state = ckpt.get("model_state_dict", ckpt.get("model_state", ckpt))
            self._model.load_state_dict(state)
        else:
            raise FileNotFoundError(
                f"No model weights found in {model_dir}. "
                "Expected model.safetensors or final_model.pt"
            )

        self._model.to(self.device).eval()
        self._sanitize_vocab_ids()

    def _sanitize_vocab_ids(self) -> int:
        """Clamp any vocab id that exceeds its trained embedding table to <UNK>.

        The on-disk vocab files can carry ids beyond the model's char/script/lang
        embedding tables (vocab/model size drift). An out-of-range id is a hard
        failure at lookup time β€” a CUDA device-side assert that poisons the whole
        context on GPU, or an ``IndexError`` (HTTP 500) on CPU β€” even though such
        a token *should* simply fall back to <UNK>. The tokeniser maps via
        ``dict.get(key, in_range_default)``, so clamping every dict VALUE into
        range makes those tokens degrade gracefully to <UNK>. Valid entries are
        untouched β†’ embeddings for normal inputs are byte-for-byte identical.
        """
        vsz = self._model.char_embed.num_embeddings
        ssz = self._model.script_embed.num_embeddings
        lsz = self._model.lang_embed.num_embeddings
        unk_char = self._char_to_id.get("<UNK>", 1)
        unk_lang = self._lang_to_id.get("<UNK>", 0)
        unk_char = unk_char if 0 <= unk_char < vsz else 0
        unk_lang = unk_lang if 0 <= unk_lang < lsz else 0
        remapped = 0
        for d, size, default in ((self._char_to_id, vsz, unk_char),
                                 (self._script_to_id, ssz, 0),
                                 (self._lang_to_id, lsz, unk_lang)):
            for k, v in list(d.items()):
                if not (0 <= v < size):
                    d[k] = default
                    remapped += 1
        if remapped:
            import sys
            print(f"[SymphonymModel] sanitised {remapped} out-of-table vocab id(s) "
                  f"β†’ UNK (char<{vsz}, script<{ssz}, lang<{lsz})", file=sys.stderr)
        return remapped

    # ------------------------------------------------------------------
    # Tokenisation
    # ------------------------------------------------------------------

    def _tokenise(self, text: str, lang: str) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
        """Convert a single (text, lang) pair to model inputs."""
        char_ids, script_id, lang_id = tokenise(
            text, lang, self._char_to_id, self._lang_to_id, self._script_to_id)
        length = len(char_ids)

        return (
            torch.tensor([char_ids],  dtype=torch.long),
            torch.tensor([script_id], dtype=torch.long),
            torch.tensor([lang_id],   dtype=torch.long),
            torch.tensor([length],    dtype=torch.long),
        )

    def _pad_batch(
        self,
        items: List[Tuple[str, str]],
    ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
        """Tokenise and pad a list of (text, lang) pairs."""
        char_seqs, script_ids, lang_ids, lengths = [], [], [], []
        for text, lang in items:
            char_ids, script_id, lang_id = tokenise(
                text, lang, self._char_to_id, self._lang_to_id, self._script_to_id)
            char_seqs.append(char_ids)
            script_ids.append(script_id)
            lang_ids.append(lang_id)
            lengths.append(len(char_ids))

        max_len = max(lengths)
        padded  = [ids + [0] * (max_len - len(ids)) for ids in char_seqs]

        return (
            torch.tensor(padded,      dtype=torch.long),
            torch.tensor(script_ids,  dtype=torch.long),
            torch.tensor(lang_ids,    dtype=torch.long),
            torch.tensor(lengths,     dtype=torch.long),
        )

    # ------------------------------------------------------------------
    # Public API
    # ------------------------------------------------------------------

    @torch.no_grad()
    def embed(self, text: str, lang: str = "und") -> np.ndarray:
        """
        Compute a 128-dimensional L2-normalised phonetic embedding.

        Parameters
        ----------
        text : str
            Toponym in any script.
        lang : str, optional
            ISO 639-1 language code (e.g. ``"en"``, ``"ar"``, ``"zh"``).
            Use ``"und"`` (undetermined) if unknown β€” the model will fall
            back to script-level generalisation.

        Returns
        -------
        numpy.ndarray of shape (128,)
        """
        char_ids, script_ids, lang_ids, lengths = self._tokenise(text, lang)
        char_ids   = char_ids.to(self.device)
        script_ids = script_ids.to(self.device)
        lang_ids   = lang_ids.to(self.device)
        emb = self._model(char_ids, script_ids, lang_ids, lengths)
        return emb.cpu().numpy()[0]

    @torch.no_grad()
    def batch_embed(self, items: List[Tuple[str, str]]) -> np.ndarray:
        """
        Compute embeddings for a list of (text, lang) pairs.

        Parameters
        ----------
        items : list of (text, lang) tuples

        Returns
        -------
        numpy.ndarray of shape (N, 128)
        """
        char_ids, script_ids, lang_ids, lengths = self._pad_batch(items)
        char_ids   = char_ids.to(self.device)
        script_ids = script_ids.to(self.device)
        lang_ids   = lang_ids.to(self.device)
        emb = self._model(char_ids, script_ids, lang_ids, lengths)
        return emb.cpu().numpy()

    def similarity(
        self,
        text1: str, lang1: str,
        text2: str, lang2: str,
    ) -> float:
        """
        Cosine similarity between two toponyms.

        Returns a float in [-1, 1]; embeddings are L2-normalised so this
        equals the dot product.  Values above 0.75 generally indicate
        phonetically similar names.
        """
        e1 = self.embed(text1, lang1)
        e2 = self.embed(text2, lang2)
        return float(np.dot(e1, e2))


# ---------------------------------------------------------------------------
# CLI smoke test
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    model = SymphonymModel()
    pairs = [
        ("London",   "en", "Π›ΠΎΠ½Π΄ΠΎΠ½",          "ru"),
        ("London",   "en", "伦敦",              "zh"),
        ("London",   "en", "Ω„Ω†Ψ―Ω†",              "ar"),
        ("London",   "en", "Londres",           "fr"),
        ("Tokyo",    "en", "東京",              "ja"),
        ("Beijing",  "en", "εŒ—δΊ¬",              "zh"),
        ("Jerusalem","en", "Χ™Χ¨Χ•Χ©ΧœΧ™Χ",           "he"),
        ("Baghdad",  "en", "Ψ¨ΨΊΨ―Ψ§Ψ―",             "ar"),
        ("Tbilisi",  "en", "αƒ—αƒ‘αƒ˜αƒšαƒ˜αƒ‘αƒ˜",           "ka"),
    ]
    print(f"\n{'Name 1':<14} {'Name 2':<16} {'Lang':<6} {'Sim':>6}")
    print("-" * 46)
    for t1, l1, t2, l2 in pairs:
        sim = model.similarity(t1, l1, t2, l2)
        print(f"{t1:<14} {t2:<16} {l1}β†’{l2:<3}  {sim:>6.3f}")