File size: 6,764 Bytes
7feeb7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// ============================================================================
// 彝汉大词典 · 引擎 2:全彝文跨文种桥接与古今字形对照引擎
// Pan-Yi Multi-Script Bridge: Liangshan Standard ↔ Sichuan Bimo Old Yi
// ↔ Universal Classical Yi (4,120) ↔ Stone Forest Sani Yi (1,052)
// ============================================================================
(function (root) {
  'use strict';

  const SCRIPT_META = {
    nuosu: {
      code: 'nuosu',
      name: '凉山规范彝文',
      shortName: '凉山规范',
      badge: 'Unicode U+A000–A48C',
      dialect: '北部方言 · 圣乍诺苏话',
      fontClass: 'yi-nuosu',
      desc: '1980年国务院批准推行的现代音节文字,含1,165个标准编码字符(819基础音节+345次高变调音节+叠音符ꀕ)及55个部首。'
    },
    sichuanOld: {
      code: 'sichuanOld',
      name: '四川传统古彝文',
      shortName: '四川古彝',
      badge: '拉普陀 · 毕摩老彝文',
      dialect: '四川凉山传统毕摩文献',
      fontClass: 'yi-sichuan-old',
      desc: '源自《四川彝文(拉普陀)》2025公益字库(阿布木乃等整理),收录凉山地区毕摩典籍传统正体与异体古彝字,与凉山规范音节一一对照。'
    },
    universal: {
      code: 'universal',
      name: '滇川黔桂通用彝文',
      shortName: '通用古彝',
      badge: '《通用彝文字典》第二版 · 4,120字',
      dialect: '超方言(贯通云贵川桂六大方言)',
      fontClass: 'yi-universal',
      desc: '汇集四川、云南、贵州、广西四省区六大方言13个调查点的超方言表意通用古彝字,配67部首与多地实测IPA读音。'
    },
    sani: {
      code: 'sani',
      name: '石林撒尼古彝文',
      shortName: '撒尼古彝',
      badge: '《彝汉简明词典》1984 · BabelStone',
      dialect: '东南方言 · 云南石林撒尼话',
      fontClass: 'yi-sani',
      desc: '依据1984年云南民族出版社《彝汉简明词典》与 BabelStone Sani Yi (SIL OFL) 整理,收录1,052词头、400余异体字与严式松紧元音标注。'
    },
    dialect: {
      code: 'dialect',
      name: '六大方言实测对勘',
      shortName: '方言对勘',
      badge: 'CLDF / 孙宏开 / 阿细先基',
      dialect: '北·东·南·东南·西·中六大方言全覆',
      fontClass: 'yi-ipa-head',
      desc: '融合孙宏开《藏缅语语音和词汇》(1991)、Yang拉罗方言(2011)、Castro鹤庆调查(2010)、Sagart汉藏同源库(2019)与《阿细先基》植物词库。'
    }
  };

  function getSichuanOldForNuosu(yiStr) {
    const map = (root.YI_AUX_DATA && root.YI_AUX_DATA.sichuanOldMap) || {};
    const primary = [];
    const variants = [];
    for (const ch of String(yiStr || '')) {
      if (ch === '\ua015' && primary.length) {
        primary.push(primary[primary.length - 1]);
        continue;
      }
      const g = map[ch];
      if (!g) return { primary: '', variants: [] };
      const arr = Array.from(g);
      primary.push(arr[0]);
      if (String(yiStr).length === 1 && arr.length > 1) {
        variants.push(...arr.slice(1));
      }
    }
    return { primary: primary.join(''), variants };
  }

  function convertInputToAllScripts(rawInput, lexicon) {
    const q = String(rawInput || '').trim();
    if (!q) return null;

    const hasNuosu = /[\ua000-\ua4cf]/.test(q);
    const hasLatin = /^[a-zA-Z\s\-']+$/.test(q);
    const hasChinese = /[\u4e00-\u9fff]/.test(q);

    let nuosuText = '';
    let pinyinText = '';
    let ipaText = '';
    let sichuanPrimary = '';
    let sichuanVariants = [];
    const universalMatches = [];
    const saniMatches = [];
    const nuosuMatches = [];

    if (hasNuosu) {
      nuosuText = q;
      pinyinText = root.YiPhonologyEngine ? root.YiPhonologyEngine.yiToPinyin(q) : '';
      ipaText = root.YiPhonologyEngine ? root.YiPhonologyEngine.yiToIpa(q) : '';
    } else if (hasLatin && root.YiPhonologyEngine) {
      nuosuText = root.YiPhonologyEngine.pinyinToYi(q);
      pinyinText = q.toLowerCase();
      ipaText = nuosuText ? root.YiPhonologyEngine.yiToIpa(nuosuText) : '';
    }

    if (nuosuText) {
      const oldRes = getSichuanOldForNuosu(nuosuText);
      sichuanPrimary = oldRes.primary;
      sichuanVariants = oldRes.variants;
    }

    // Search live lexicon for exact/close cross-script equivalents (ranked: verified primary sense first!)
    if (Array.isArray(lexicon)) {
      const nuosuScored = [];
      for (let i = 0; i < lexicon.length; i++) {
        const e = lexicon[i];
        if (hasChinese) {
          const rawParts = (e.glossZh || '').split(/[;;,,、]+/).map(s => s.replace(/[((][^))]*[))]/g, '').trim()).filter(Boolean);
          const firstMatch = rawParts[0] === q;
          const anyMatch = firstMatch || rawParts.includes(q) || e.glossZh === q;
          if (!anyMatch) continue;
          if (e.script === 'nuosu') {
            const sc = (firstMatch ? 100 : 40) + (e.status === 'verified' ? 50 : 0) + (e.lemma.length <= 2 ? 20 : 0) - rawParts.length * 2;
            nuosuScored.push([sc, e]);
          } else if (e.script === 'universal' && universalMatches.length < 10) {
            universalMatches.push(e);
          } else if (e.script === 'sani' && saniMatches.length < 8) {
            saniMatches.push(e);
          }
        } else if (nuosuText) {
          if (e.script === 'nuosu' && e.lemma === nuosuText) {
            nuosuMatches.push(e);
            const uChars = (e.scripts && e.scripts.universal) || [];
            if (uChars.length) {
              for (let j = 0; j < lexicon.length && universalMatches.length < 8; j++) {
                if (lexicon[j].script === 'universal' && uChars.includes(lexicon[j].lemma)) {
                  universalMatches.push(lexicon[j]);
                }
              }
            }
          }
        }
      }
      if (nuosuScored.length) {
        nuosuScored.sort((a, b) => b[0] - a[0]);
        nuosuMatches.push(...nuosuScored.slice(0, 8).map(x => x[1]));
      }
      if (hasChinese && !nuosuText && nuosuMatches.length) {
        const best = nuosuMatches[0];
        nuosuText = best.lemma;
        pinyinText = best.pinyin;
        ipaText = best.ipa;
        const oldRes = getSichuanOldForNuosu(nuosuText);
        sichuanPrimary = oldRes.primary;
        sichuanVariants = oldRes.variants;
      }
    }

    return {
      query: q,
      nuosuText,
      pinyinText,
      ipaText,
      sichuanPrimary,
      sichuanVariants,
      nuosuMatches,
      universalMatches,
      saniMatches
    };
  }

  root.YiScriptBridge = {
    SCRIPT_META,
    getSichuanOldForNuosu,
    convertInputToAllScripts
  };
})(window);