Skip to content

Commit 250d435

Browse files
committed
Generate formal names for algorithmic character ranges
Assisted-By: GPT-6
1 parent 6c391ac commit 250d435

2 files changed

Lines changed: 139 additions & 3 deletions

File tree

‎scripts/parse-names.mjs‎

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,104 @@
11
import utils from './utils.mjs';
22
import regenerate from 'regenerate';
33

4+
// Short jamo names used by the Hangul Syllable Name Generation algorithm.
5+
const leading = [
6+
'G',
7+
'GG',
8+
'N',
9+
'D',
10+
'DD',
11+
'R',
12+
'M',
13+
'B',
14+
'BB',
15+
'S',
16+
'SS',
17+
'',
18+
'J',
19+
'JJ',
20+
'C',
21+
'K',
22+
'T',
23+
'P',
24+
'H',
25+
];
26+
const vowels = [
27+
'A',
28+
'AE',
29+
'YA',
30+
'YAE',
31+
'EO',
32+
'E',
33+
'YEO',
34+
'YE',
35+
'O',
36+
'WA',
37+
'WAE',
38+
'OE',
39+
'YO',
40+
'U',
41+
'WEO',
42+
'WE',
43+
'WI',
44+
'YU',
45+
'EU',
46+
'YI',
47+
'I',
48+
];
49+
const trailing = [
50+
'',
51+
'G',
52+
'GG',
53+
'GS',
54+
'N',
55+
'NJ',
56+
'NH',
57+
'D',
58+
'L',
59+
'LG',
60+
'LM',
61+
'LB',
62+
'LS',
63+
'LT',
64+
'LP',
65+
'LH',
66+
'M',
67+
'B',
68+
'BS',
69+
'S',
70+
'SS',
71+
'NG',
72+
'J',
73+
'C',
74+
'K',
75+
'T',
76+
'P',
77+
'H',
78+
];
79+
80+
const getRangeName = (rangeName, codePoint) => {
81+
if (rangeName === 'Hangul Syllable') {
82+
const index = codePoint - 0xac00;
83+
return `HANGUL SYLLABLE ${leading[Math.floor(index / 588)]}${vowels[Math.floor(index / 28) % 21]}${trailing[index % 28]}`;
84+
}
85+
let prefix;
86+
if (/^CJK Ideograph(?: Extension [A-Z])?$/.test(rangeName)) {
87+
prefix = 'CJK UNIFIED IDEOGRAPH';
88+
} else if (rangeName === 'CJK Compatibility Ideograph') {
89+
prefix = 'CJK COMPATIBILITY IDEOGRAPH';
90+
} else if (/^Tangut Ideograph(?: Supplement)?$/.test(rangeName)) {
91+
prefix = 'TANGUT IDEOGRAPH';
92+
} else if (rangeName === 'Jurchen Character') {
93+
prefix = 'JURCHEN CHARACTER';
94+
} else if (rangeName === 'Seal Character') {
95+
prefix = 'SMALL SEAL CHARACTER';
96+
} else {
97+
throw new Error(`Unknown algorithmic name range: ${rangeName}.`);
98+
}
99+
return `${prefix}-${codePoint.toString(16).toUpperCase().padStart(4, '0')}`;
100+
};
101+
4102
const parseNames = async (version) => {
5103
const map = {};
6104
const source = utils.readDataFile(version, 'database');
@@ -22,16 +120,22 @@ const parseNames = async (version) => {
22120
if (/<.+, Last>/.test(name)) {
23121
flag = false;
24122
const rangeName = /<(.+), Last>/.exec(name)[1];
25-
map[rangeName] ??= regenerate();
26-
map[rangeName].addRange(first, codePoint);
123+
// Surrogates and private-use code points have no formal Name.
124+
if (data[2] !== 'Cs' && data[2] !== 'Co') {
125+
for (let cp = first; cp <= codePoint; cp++) {
126+
const derivedName = getRangeName(rangeName, cp);
127+
map[derivedName] ??= regenerate();
128+
map[derivedName].add(cp);
129+
}
130+
}
27131
} else {
28132
throw new Error('Database exception');
29133
}
30134
} else {
31135
if (/<.+, First>/.test(name)) {
32136
flag = true;
33137
first = codePoint;
34-
} else {
138+
} else if (!name.startsWith('<')) {
35139
map[name] ??= regenerate();
36140
map[name].add(codePoint);
37141
}

‎tests/parsers.mjs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { test } from 'node:test';
22
import fs from 'node:fs';
33
import parseCaseFolding from '../scripts/parse-case-folding.mjs';
44
import parseBidiClass from '../scripts/parse-bidi-class.mjs';
5+
import parseNames from '../scripts/parse-names.mjs';
56
import utils from '../scripts/utils.mjs';
67
import parseDecompositionMapping from '../scripts/parse-decomposition-mapping.mjs';
78
import parseIndicConjunctBreak from '../scripts/parse-indic-conjunct-break.mjs';
@@ -26,6 +27,37 @@ test('Hangul has pairwise canonical decomposition mappings', async (t) => {
2627
t.assert.equal(await parseDecompositionMapping('1.1.5'), undefined);
2728
});
2829

30+
test('Algorithmic ranges have individual formal names', async (t) => {
31+
for (const version of ['2.0.14', '3.2.0', '18.0.0']) {
32+
const names = await parseNames(version);
33+
for (const [cp, name] of [
34+
[0xac00, 'HANGUL SYLLABLE GA'],
35+
[0xac01, 'HANGUL SYLLABLE GAG'],
36+
[0xd7a3, 'HANGUL SYLLABLE HIH'],
37+
[0x4e00, 'CJK UNIFIED IDEOGRAPH-4E00'],
38+
[0xf900, 'CJK COMPATIBILITY IDEOGRAPH-F900'],
39+
]) {
40+
t.assert.ok(names[name]?.contains(cp), `${version}: ${name}`);
41+
}
42+
t.assert.equal(names['Hangul Syllable'], undefined);
43+
t.assert.equal(names['CJK Ideograph'], undefined);
44+
t.assert.equal(names['Private Use'], undefined);
45+
t.assert.equal(names['<control>'], undefined);
46+
}
47+
const names = await parseNames('18.0.0');
48+
for (const [cp, name] of [
49+
[0x20000, 'CJK UNIFIED IDEOGRAPH-20000'],
50+
[0x17000, 'TANGUT IDEOGRAPH-17000'],
51+
[0x18d00, 'TANGUT IDEOGRAPH-18D00'],
52+
[0x18e00, 'JURCHEN CHARACTER-18E00'],
53+
[0x19191, 'JURCHEN CHARACTER-19191'],
54+
[0x3d000, 'SMALL SEAL CHARACTER-3D000'],
55+
[0x3fc3f, 'SMALL SEAL CHARACTER-3FC3F'],
56+
]) {
57+
t.assert.ok(names[name]?.contains(cp), name);
58+
}
59+
});
60+
2961
test('Bidi class applies historical and current unassigned defaults', async (t) => {
3062
for (const version of [
3163
'3.0.0',

0 commit comments

Comments
 (0)