jntajis-python 0.2.0__cp314-cp314-win32.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jntajis/__init__.py +129 -0
- jntajis/_jntajis.cp314-win32.pyd +4 -0
- jntajis/_jntajis.h +704511 -0
- jntajis/_jntajis.pyi +19 -0
- jntajis/_jntajis.pyx +1157 -0
- jntajis/_version.py +24 -0
- jntajis/gen.py +850 -0
- jntajis/py.typed +0 -0
- jntajis/pythoncapi_compat_shim.h +20 -0
- jntajis/xlsx_parser/__init__.py +1 -0
- jntajis/xlsx_parser/parser.py +591 -0
- jntajis/xlsx_parser/xmlutils.py +332 -0
- jntajis_python-0.2.0.dist-info/METADATA +119 -0
- jntajis_python-0.2.0.dist-info/RECORD +16 -0
- jntajis_python-0.2.0.dist-info/WHEEL +4 -0
- jntajis_python-0.2.0.dist-info/licenses/LICENSE +29 -0
jntajis/gen.py
ADDED
|
@@ -0,0 +1,850 @@
|
|
|
1
|
+
import enum
|
|
2
|
+
import json
|
|
3
|
+
import re
|
|
4
|
+
import typing
|
|
5
|
+
|
|
6
|
+
import click
|
|
7
|
+
import jinja2
|
|
8
|
+
|
|
9
|
+
from .xlsx_parser import read_xlsx
|
|
10
|
+
|
|
11
|
+
invalid_unicode = 0x7FFFFFFF
|
|
12
|
+
|
|
13
|
+
memo_regexp = re.compile("類似字形([uU]+[0-9a-fA-F]+)は本文字に変換する。")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class JISCharacterClass(enum.IntEnum):
|
|
17
|
+
RESERVED = 0
|
|
18
|
+
KANJI_LEVEL_1 = 1
|
|
19
|
+
KANJI_LEVEL_2 = 2
|
|
20
|
+
KANJI_LEVEL_3 = 3
|
|
21
|
+
KANJI_LEVEL_4 = 4
|
|
22
|
+
JISX0208_NON_KANJI = 9
|
|
23
|
+
JISX0213_NON_KANJI = 11
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
category_name_to_enum_map: typing.Mapping[str, JISCharacterClass] = {
|
|
27
|
+
"非漢字": JISCharacterClass.JISX0208_NON_KANJI,
|
|
28
|
+
"追加非漢字": JISCharacterClass.JISX0213_NON_KANJI,
|
|
29
|
+
"JIS1水": JISCharacterClass.KANJI_LEVEL_1,
|
|
30
|
+
"JIS2水": JISCharacterClass.KANJI_LEVEL_2,
|
|
31
|
+
"JIS3水": JISCharacterClass.KANJI_LEVEL_3,
|
|
32
|
+
"JIS4水": JISCharacterClass.KANJI_LEVEL_4,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class ShrinkingTransliterationMapping(typing.NamedTuple):
|
|
37
|
+
jis: int
|
|
38
|
+
"""packed men-ku-ten code"""
|
|
39
|
+
us: typing.Tuple[int, ...]
|
|
40
|
+
"""corresponding Unicode character"""
|
|
41
|
+
sus: typing.Tuple[int, ...]
|
|
42
|
+
"""corresponding Unicode character (secondary)"""
|
|
43
|
+
class_: JISCharacterClass
|
|
44
|
+
"""JIS character class"""
|
|
45
|
+
tx_jis: typing.Tuple[int, ...] = ()
|
|
46
|
+
"""transliterated form in packed men-ku-ten code"""
|
|
47
|
+
tx_us: typing.Tuple[int, ...] = ()
|
|
48
|
+
"""transliterated form in Unicode"""
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class URangeToJISMapping(typing.NamedTuple):
|
|
52
|
+
start: int
|
|
53
|
+
end: int
|
|
54
|
+
jis: typing.Sequence[int]
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class Outer(typing.NamedTuple):
|
|
58
|
+
u: int
|
|
59
|
+
n: typing.List[ShrinkingTransliterationMapping]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
code_template = """#include <stdint.h>
|
|
63
|
+
#include <stddef.h>
|
|
64
|
+
|
|
65
|
+
typedef enum JISCharacterClass {
|
|
66
|
+
{%- for x in JISCharacterClass %}
|
|
67
|
+
JISCharacterClass_{{ x.name }} = {{ x.value }}{% if not loop.last %},{% endif %}
|
|
68
|
+
{%- endfor %}
|
|
69
|
+
} JISCharacterClass;
|
|
70
|
+
|
|
71
|
+
typedef struct ShrinkingTransliterationMapping {
|
|
72
|
+
uint16_t jis;
|
|
73
|
+
uint32_t us[2];
|
|
74
|
+
uint32_t sus[2];
|
|
75
|
+
JISCharacterClass class_;
|
|
76
|
+
size_t tx_len:8;
|
|
77
|
+
uint16_t tx_jis[4];
|
|
78
|
+
uint32_t tx_us[4];
|
|
79
|
+
} ShrinkingTransliterationMapping;
|
|
80
|
+
|
|
81
|
+
typedef struct URangeToJISMapping {
|
|
82
|
+
uint32_t start:24, end:24;
|
|
83
|
+
const uint16_t *jis;
|
|
84
|
+
} URangeToJISMapping;
|
|
85
|
+
|
|
86
|
+
static const ShrinkingTransliterationMapping tx_mappings[2 * 94 * 94] = {
|
|
87
|
+
{%- for m in tx_mappings %}
|
|
88
|
+
{{ "{" }}
|
|
89
|
+
{{m.jis}},
|
|
90
|
+
{{"{"}}{% for e in m.us|iter_pad(2, "-1") %}{% if not loop.first %}, {% endif %}{{ e }}{% endfor %}},
|
|
91
|
+
{{"{"}}{% for e in m.sus|iter_pad(2, "-1") %}{% if not loop.first %}, {% endif %}{{ e }}{% endfor %}},
|
|
92
|
+
JISCharacterClass_{{ m.class_.name }},
|
|
93
|
+
{{ m.tx_jis|length }},
|
|
94
|
+
{{"{"}}{% for e in m.tx_jis %}{% if not loop.first %}, {% endif %}{{ e }}{% else %}0{% endfor %}},
|
|
95
|
+
{{"{"}}{% for e in m.tx_us %}{% if not loop.first %}, {% endif %}{{ e }}{% else %}0{% endfor %}}
|
|
96
|
+
}{% if not loop.last %},{% endif %}
|
|
97
|
+
{%- endfor %}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
{% for m in uni_range_to_jis_mappings %}
|
|
101
|
+
static const uint16_t jis_urange_{{ "%06x"|format(m.start) }}_{{ "%06x"|format(m.end) }}[{{ m.jis|length }}] = {
|
|
102
|
+
{%- for e in m.jis %}
|
|
103
|
+
{{ e }}{% if not loop.last %},{% endif %}
|
|
104
|
+
{%- endfor %}
|
|
105
|
+
};
|
|
106
|
+
{%- endfor %}
|
|
107
|
+
|
|
108
|
+
static const URangeToJISMapping urange_to_jis_mappings[] = {
|
|
109
|
+
{%- for m in uni_range_to_jis_mappings %}
|
|
110
|
+
{{ "{" }}{{ m.start }}, {{ m.end }}, jis_urange_{{ "%06x"|format(m.start) }}_{{ "%06x"|format(m.end) }}}{% if not loop.last %},{% endif %}
|
|
111
|
+
{%- endfor %}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
typedef struct SMUniToJISTuple {
|
|
115
|
+
int state;
|
|
116
|
+
uint32_t u;
|
|
117
|
+
} SMUniToJISTuple;
|
|
118
|
+
|
|
119
|
+
static uint16_t sm_uni_to_jis_mapping(int *state, uint32_t u)
|
|
120
|
+
{
|
|
121
|
+
uint16_t j = 0;
|
|
122
|
+
int _state = *state;
|
|
123
|
+
reenter:
|
|
124
|
+
switch (_state) {
|
|
125
|
+
case 0:
|
|
126
|
+
if (u < {{ uni_pairs_to_jis_mappings[0].u }} || u > {{ uni_pairs_to_jis_mappings[-1].u }}) {
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
switch (u) {
|
|
130
|
+
{%- for m in uni_pairs_to_jis_mappings %}
|
|
131
|
+
case {{m.u}}:
|
|
132
|
+
_state = {{ loop.index }};
|
|
133
|
+
break;
|
|
134
|
+
{%- endfor %}
|
|
135
|
+
}
|
|
136
|
+
break;
|
|
137
|
+
{%- for m in uni_pairs_to_jis_mappings %}
|
|
138
|
+
case {{ loop.index }}:
|
|
139
|
+
switch (u) {
|
|
140
|
+
{%- for e in m.n %}
|
|
141
|
+
case {{ e.us[1] }}:
|
|
142
|
+
j = {{ e.jis }};
|
|
143
|
+
_state = -1;
|
|
144
|
+
break;
|
|
145
|
+
{%- endfor %}
|
|
146
|
+
default:
|
|
147
|
+
_state = 0;
|
|
148
|
+
goto reenter;
|
|
149
|
+
}
|
|
150
|
+
break;
|
|
151
|
+
{%- endfor %}
|
|
152
|
+
}
|
|
153
|
+
*state = _state;
|
|
154
|
+
return j;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
typedef enum MJShrinkScheme {
|
|
158
|
+
{%- for x in MJShrinkScheme %}
|
|
159
|
+
JISCharacterClass_{{ x.name }} = {{ x.value }}{% if not loop.last %},{% endif %}
|
|
160
|
+
{%- endfor %}
|
|
161
|
+
} MJShrinkScheme;
|
|
162
|
+
|
|
163
|
+
typedef struct UIVSPair {
|
|
164
|
+
uint32_t u:22;
|
|
165
|
+
unsigned int v:1;
|
|
166
|
+
unsigned int sv:1;
|
|
167
|
+
uint8_t s;
|
|
168
|
+
} UIVSPair;
|
|
169
|
+
|
|
170
|
+
typedef struct MJMapping {
|
|
171
|
+
uint32_t mj:24;
|
|
172
|
+
uint32_t iu;
|
|
173
|
+
UIVSPair v[{{ max_variants + 1 }}];
|
|
174
|
+
} MJMapping;
|
|
175
|
+
|
|
176
|
+
typedef struct MJMappingSet {
|
|
177
|
+
size_t l:8;
|
|
178
|
+
MJMapping ms[{{ max_mss }}];
|
|
179
|
+
} MJMappingSet;
|
|
180
|
+
|
|
181
|
+
typedef struct URangeToMJMappings {
|
|
182
|
+
uint32_t start:24, end:24;
|
|
183
|
+
const MJMappingSet* mss;
|
|
184
|
+
} URangeToMJMappings;
|
|
185
|
+
|
|
186
|
+
typedef struct MJShrinkMappingUnicodeSet {
|
|
187
|
+
{%- for x in MJShrinkScheme %}
|
|
188
|
+
uint32_t _{{ x|int }}[{{ digested_shrink_mappings.max_lens[x] }}];
|
|
189
|
+
{%- endfor %}
|
|
190
|
+
} MJShrinkMappingUnicodeSet;
|
|
191
|
+
|
|
192
|
+
{% for cm in chunked_mj_mappings %}
|
|
193
|
+
static const MJMappingSet chunked_mj_mappings_{{ "%06x"|format(cm.start) }}_{{ "%06x"|format(cm.end) }}[{{ cm.mss|length }}] = {
|
|
194
|
+
{%- for ms in cm.mss %}
|
|
195
|
+
{{ "{" -}}
|
|
196
|
+
{{- ms|length -}}, {{ "{" -}}
|
|
197
|
+
{%- for m in ms -%}
|
|
198
|
+
{{ "{" }}{{ m.mj }}, {{ m.iu }}, {{"{"}}
|
|
199
|
+
{%- for v in m.v -%}
|
|
200
|
+
{{- "{" }}{{ v.u }}, 1, {{ "1" if v.s >= 0 else "0" }}, {{ v.s if v.s >= 0 else 0 }}{{ "}" }}{% if not loop.last %},{% endif %}
|
|
201
|
+
{%- endfor -%}{{ ", {0, 0, 0, 0}}}" }}{% if not loop.last %},{% endif %}
|
|
202
|
+
{%- else -%}
|
|
203
|
+
{-1, 0, {{ "{{0, 0, 0, 0}}}" -}}
|
|
204
|
+
{%- endfor -%}
|
|
205
|
+
{{- "}" -}}
|
|
206
|
+
{{- "}" -}}{% if not loop.last %},{% endif -%}
|
|
207
|
+
{%- endfor %}
|
|
208
|
+
};
|
|
209
|
+
{% endfor %}
|
|
210
|
+
static const URangeToMJMappings urange_to_mj_mappings[{{ chunked_mj_mappings|length }}] = {
|
|
211
|
+
{%- for cm in chunked_mj_mappings %}
|
|
212
|
+
{{ "{" }}{{ cm.start }}, {{ cm.end }}, chunked_mj_mappings_{{ "%06x"|format(cm.start) }}_{{ "%06x"|format(cm.end) }}}{% if not loop.last %},{% endif %}
|
|
213
|
+
{%- endfor %}
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
static const MJShrinkMappingUnicodeSet mj_shrink_mappings[{{ digested_shrink_mappings.mje + 1 }}] = {
|
|
217
|
+
{%- for sms in digested_shrink_mappings.smss %}
|
|
218
|
+
{{ "{" }}
|
|
219
|
+
{%- for us in sms %}
|
|
220
|
+
{{ "{" }}{% for u in us|iter_pad(digested_shrink_mappings.max_lens[loop.index0], "-1") %}{{u}}{% if not loop.last %},{% endif %}{%- endfor %}}{% if not loop.last %},{% endif %}
|
|
221
|
+
{%- endfor %}
|
|
222
|
+
}{% if not loop.last %},{% endif %}
|
|
223
|
+
{%- endfor %}
|
|
224
|
+
};
|
|
225
|
+
"""
|
|
226
|
+
|
|
227
|
+
men_ku_ten_regexp = re.compile(r"(\d+)-(\d+)-(\d+)$")
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def parse_men_ku_ten_repr(v: str) -> int:
|
|
231
|
+
m = men_ku_ten_regexp.match(v)
|
|
232
|
+
if m is None:
|
|
233
|
+
raise ValueError(f"invalid men-ku-ten string: {v}")
|
|
234
|
+
|
|
235
|
+
men = int(m.group(1))
|
|
236
|
+
ku = int(m.group(2))
|
|
237
|
+
ten = int(m.group(3))
|
|
238
|
+
|
|
239
|
+
if men < 1 or men > 2:
|
|
240
|
+
raise ValueError(f"invalid men value: {men}")
|
|
241
|
+
|
|
242
|
+
if ku < 1 or ku > 94:
|
|
243
|
+
raise ValueError(f"invalid ku value: {ku}")
|
|
244
|
+
|
|
245
|
+
if ten < 1 or ten > 94:
|
|
246
|
+
raise ValueError(f"invalid ten value: {ten}")
|
|
247
|
+
|
|
248
|
+
return (men - 1) * 94 * 94 + (ku - 1) * 94 + (ten - 1)
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
uni_repr_regexp = re.compile(r"[uU]\+([0-9a-fA-F]+)$")
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def parse_uni_repr(v: str) -> int:
|
|
255
|
+
try:
|
|
256
|
+
m = uni_repr_regexp.match(v)
|
|
257
|
+
if m is None:
|
|
258
|
+
raise ValueError()
|
|
259
|
+
ucp = int(m.group(1), 16)
|
|
260
|
+
except ValueError:
|
|
261
|
+
raise ValueError(f"invalid unicode repr: {v}")
|
|
262
|
+
|
|
263
|
+
if ucp < 0 or ucp > 0x10FFFF:
|
|
264
|
+
raise ValueError(f"invalid unicode code point: {ucp:08x}")
|
|
265
|
+
|
|
266
|
+
return ucp
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def parse_uni_seq_repr(v: str) -> typing.Tuple[int, ...]:
|
|
270
|
+
return tuple(parse_uni_repr(c) for c in v.split())
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def parse_underscore_delimited_uni_hex_repr(v: str) -> typing.Tuple[int, ...]:
|
|
274
|
+
return tuple(int(c, 16) for c in v.split("_"))
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def take_until_empty(
|
|
278
|
+
f: typing.Callable[[str], int], v: typing.Iterable[str]
|
|
279
|
+
) -> typing.Iterator[int]:
|
|
280
|
+
for c in v:
|
|
281
|
+
if not c:
|
|
282
|
+
break
|
|
283
|
+
yield f(c)
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
class InvalidFormatError(Exception):
|
|
287
|
+
@property
|
|
288
|
+
def message(self):
|
|
289
|
+
return self.args[0]
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def none_as_empty(v: typing.Optional[str]) -> str:
|
|
293
|
+
if v is None:
|
|
294
|
+
return ""
|
|
295
|
+
else:
|
|
296
|
+
return v
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def read_jnta_excel_file(f: str) -> typing.Sequence[ShrinkingTransliterationMapping]:
|
|
300
|
+
mappings: typing.List[ShrinkingTransliterationMapping] = []
|
|
301
|
+
|
|
302
|
+
wb = read_xlsx(f)
|
|
303
|
+
|
|
304
|
+
ws = wb.get_worksheet_by_index(1)
|
|
305
|
+
ri = ws.iter_rows()
|
|
306
|
+
|
|
307
|
+
try:
|
|
308
|
+
row = next(ri)
|
|
309
|
+
except StopIteration:
|
|
310
|
+
raise InvalidFormatError("too few rows")
|
|
311
|
+
|
|
312
|
+
# assert if it is formatted in the expected manner
|
|
313
|
+
if (
|
|
314
|
+
row[0] != "変換元の文字(JISX0213:1-4水)"
|
|
315
|
+
or row[4] != "コード変換(1対1変換)"
|
|
316
|
+
or row[7] != "文字列変換(追加非漢字や、1対nの文字変換を行う)"
|
|
317
|
+
or row[16] != "備考"
|
|
318
|
+
):
|
|
319
|
+
raise InvalidFormatError("a column of the first row does not match to the expected values")
|
|
320
|
+
|
|
321
|
+
try:
|
|
322
|
+
row = next(ri)
|
|
323
|
+
except StopIteration:
|
|
324
|
+
raise InvalidFormatError("too few rows")
|
|
325
|
+
if (
|
|
326
|
+
row[0] != "面区点コード"
|
|
327
|
+
or row[1] != "Unicode"
|
|
328
|
+
or row[2] != "字形"
|
|
329
|
+
or row[3] != "JIS区分"
|
|
330
|
+
or row[4] != "面区点コード"
|
|
331
|
+
or row[5] != "Unicode"
|
|
332
|
+
or row[6] != "字形"
|
|
333
|
+
or row[7] != "面区点コード①"
|
|
334
|
+
or row[8] != "面区点コード②"
|
|
335
|
+
or row[9] != "面区点コード③"
|
|
336
|
+
or row[10] != "面区点コード④"
|
|
337
|
+
or row[11] != "Unicode①"
|
|
338
|
+
or row[12] != "Unicode②"
|
|
339
|
+
or row[13] != "Unicode③"
|
|
340
|
+
or row[14] != "Unicode④"
|
|
341
|
+
or row[15] != "字形"
|
|
342
|
+
):
|
|
343
|
+
raise InvalidFormatError(
|
|
344
|
+
"a column of the second row does not match to the expected values"
|
|
345
|
+
)
|
|
346
|
+
|
|
347
|
+
lj = -1
|
|
348
|
+
for ro, row in enumerate(ri):
|
|
349
|
+
if not row[0]:
|
|
350
|
+
break
|
|
351
|
+
|
|
352
|
+
_row = [none_as_empty(c) for c in row]
|
|
353
|
+
|
|
354
|
+
class_ = category_name_to_enum_map.get(_row[3])
|
|
355
|
+
|
|
356
|
+
if class_ is None:
|
|
357
|
+
raise InvalidFormatError(f"unknown category name: {_row[3]}")
|
|
358
|
+
|
|
359
|
+
try:
|
|
360
|
+
jis = parse_men_ku_ten_repr(_row[0])
|
|
361
|
+
except ValueError as e:
|
|
362
|
+
raise InvalidFormatError(f"failed to parse men-ku-ten at row {ro + 2}") from e
|
|
363
|
+
|
|
364
|
+
us: typing.Tuple[int, ...]
|
|
365
|
+
try:
|
|
366
|
+
us = parse_uni_seq_repr(_row[1])
|
|
367
|
+
if len(us) > 2:
|
|
368
|
+
raise ValueError()
|
|
369
|
+
except ValueError:
|
|
370
|
+
raise InvalidFormatError(f"failed to parse rune at row {ro + 2}")
|
|
371
|
+
|
|
372
|
+
sus: typing.Tuple[int, ...] = ()
|
|
373
|
+
|
|
374
|
+
tx_jis: typing.Tuple[int, ...] = ()
|
|
375
|
+
tx_us: typing.Tuple[int, ...] = ()
|
|
376
|
+
if lj + 1 < jis:
|
|
377
|
+
for i in range(lj + 1, jis):
|
|
378
|
+
mappings.append(
|
|
379
|
+
ShrinkingTransliterationMapping(
|
|
380
|
+
jis=i,
|
|
381
|
+
us=us,
|
|
382
|
+
sus=sus,
|
|
383
|
+
class_=JISCharacterClass.RESERVED,
|
|
384
|
+
)
|
|
385
|
+
)
|
|
386
|
+
|
|
387
|
+
if _row[4]:
|
|
388
|
+
if not _row[5]:
|
|
389
|
+
raise InvalidFormatError(
|
|
390
|
+
f"non-empty men-ku-ten code followed by empty Unicode at row {ro + 2}"
|
|
391
|
+
)
|
|
392
|
+
|
|
393
|
+
try:
|
|
394
|
+
tx_jis = (parse_men_ku_ten_repr(_row[4]),)
|
|
395
|
+
except ValueError as e:
|
|
396
|
+
raise InvalidFormatError(f"failed to parse men-ku-ten at row {ro + 2}") from e
|
|
397
|
+
|
|
398
|
+
try:
|
|
399
|
+
tx_us = (parse_uni_repr(_row[5]),)
|
|
400
|
+
except ValueError:
|
|
401
|
+
raise InvalidFormatError(f"failed to parse rune at row {ro + 2}")
|
|
402
|
+
elif _row[7]:
|
|
403
|
+
if not _row[11]:
|
|
404
|
+
raise InvalidFormatError(
|
|
405
|
+
f"empty single-mapping rune followed by empty runes at row {ro + 2}"
|
|
406
|
+
)
|
|
407
|
+
try:
|
|
408
|
+
tx_jis = tuple(take_until_empty(parse_men_ku_ten_repr, _row[7:11]))
|
|
409
|
+
except ValueError as e:
|
|
410
|
+
raise InvalidFormatError(f"failed to parse men-ku-ten at row {ro + 2}") from e
|
|
411
|
+
try:
|
|
412
|
+
tx_us = tuple(take_until_empty(parse_uni_repr, _row[11:15]))
|
|
413
|
+
except ValueError as e:
|
|
414
|
+
raise InvalidFormatError(f"failed to parse rune at row {ro + 2}") from e
|
|
415
|
+
|
|
416
|
+
if len(tx_jis) != len(tx_us):
|
|
417
|
+
raise InvalidFormatError(
|
|
418
|
+
f"number of characters for the transliteration form does not agree between JIS and Unicode at row {ro + 2}"
|
|
419
|
+
)
|
|
420
|
+
|
|
421
|
+
if _row[16]:
|
|
422
|
+
m = memo_regexp.match(_row[16])
|
|
423
|
+
if m is not None:
|
|
424
|
+
try:
|
|
425
|
+
sus = (parse_uni_repr(m.group(1)),)
|
|
426
|
+
except ValueError as e:
|
|
427
|
+
raise InvalidFormatError(
|
|
428
|
+
f"failed to parse rune in memo ({_row[16]}) at row {ro + 2}"
|
|
429
|
+
) from e
|
|
430
|
+
|
|
431
|
+
mappings.append(
|
|
432
|
+
ShrinkingTransliterationMapping(
|
|
433
|
+
jis=jis,
|
|
434
|
+
us=us,
|
|
435
|
+
sus=sus,
|
|
436
|
+
class_=class_,
|
|
437
|
+
tx_jis=tx_jis,
|
|
438
|
+
tx_us=tx_us,
|
|
439
|
+
)
|
|
440
|
+
)
|
|
441
|
+
lj = jis
|
|
442
|
+
|
|
443
|
+
return mappings
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
MJ_FIELDS = [
|
|
447
|
+
"図形",
|
|
448
|
+
"font",
|
|
449
|
+
"MJ文字図形名",
|
|
450
|
+
"対応するUCS",
|
|
451
|
+
"実装したUCS",
|
|
452
|
+
"実装したMoji_JohoコレクションIVS",
|
|
453
|
+
"実装したSVS",
|
|
454
|
+
"戸籍統一文字番号",
|
|
455
|
+
"住基ネット統一文字コード",
|
|
456
|
+
"入管正字コード",
|
|
457
|
+
"入管外字コード",
|
|
458
|
+
"漢字施策",
|
|
459
|
+
"対応する互換漢字",
|
|
460
|
+
"X0213",
|
|
461
|
+
"X0213 包摂連番",
|
|
462
|
+
"X0213 包摂区分",
|
|
463
|
+
"X0212",
|
|
464
|
+
"MJ文字図形バージョン",
|
|
465
|
+
"登記統一文字番号(参考)",
|
|
466
|
+
"部首1(参考)",
|
|
467
|
+
"内画数1(参考)",
|
|
468
|
+
"部首2(参考)",
|
|
469
|
+
"内画数2(参考)",
|
|
470
|
+
"部首3(参考)",
|
|
471
|
+
"内画数3(参考)",
|
|
472
|
+
"部首4(参考)",
|
|
473
|
+
"内画数4(参考)",
|
|
474
|
+
"総画数(参考)",
|
|
475
|
+
"読み(参考)",
|
|
476
|
+
"大漢和",
|
|
477
|
+
"日本語漢字辞典",
|
|
478
|
+
"新大字典",
|
|
479
|
+
"大字源",
|
|
480
|
+
"大漢語林",
|
|
481
|
+
"更新履歴",
|
|
482
|
+
"備考",
|
|
483
|
+
]
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
class UIVSPair(typing.NamedTuple):
|
|
487
|
+
u: int
|
|
488
|
+
s: int
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
class MJMapping(typing.NamedTuple):
|
|
492
|
+
mj: int
|
|
493
|
+
# 実装したUCS; 0 if none
|
|
494
|
+
iu: int
|
|
495
|
+
v: typing.Tuple[UIVSPair, ...]
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
mj_repr_regexp = re.compile(r"MJ([0-9]+)$")
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
def parse_mj_repr(v: str) -> int:
|
|
502
|
+
try:
|
|
503
|
+
m = mj_repr_regexp.match(v)
|
|
504
|
+
if m is None:
|
|
505
|
+
raise ValueError()
|
|
506
|
+
return int(m.group(1))
|
|
507
|
+
except ValueError:
|
|
508
|
+
raise ValueError(f"invalid MJ repr: {v}")
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
def resolve_ivs_no(n: int) -> int:
|
|
512
|
+
# VS1 to VS16
|
|
513
|
+
if n >= 0xFE00 and n < 0xFE10:
|
|
514
|
+
return n - 0xFE00
|
|
515
|
+
# VS17 to VS256
|
|
516
|
+
if n >= 0xE0100 and n < 0xE01F0:
|
|
517
|
+
return n - 0xE00F0
|
|
518
|
+
raise ValueError(f"U+{n:06x} is not an IVS")
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
def read_mj_excel_file(f: str) -> typing.Tuple[typing.Sequence[MJMapping], int]:
|
|
522
|
+
mappings: typing.List[MJMapping] = []
|
|
523
|
+
|
|
524
|
+
wb = read_xlsx(f)
|
|
525
|
+
ws = wb.get_worksheet_by_index(1)
|
|
526
|
+
ri = ws.iter_rows()
|
|
527
|
+
|
|
528
|
+
try:
|
|
529
|
+
row = next(ri)
|
|
530
|
+
except StopIteration:
|
|
531
|
+
raise InvalidFormatError("too few rows")
|
|
532
|
+
|
|
533
|
+
# assert if it is formatted in the expected manner
|
|
534
|
+
if not all(c == f for c, f in zip(row, MJ_FIELDS)):
|
|
535
|
+
raise InvalidFormatError("a column of the first row does not match to the expected values")
|
|
536
|
+
|
|
537
|
+
max_variants = 0
|
|
538
|
+
|
|
539
|
+
for row in ri:
|
|
540
|
+
if not row[2]:
|
|
541
|
+
continue
|
|
542
|
+
mj = parse_mj_repr(none_as_empty(row[2]))
|
|
543
|
+
if not row[3]:
|
|
544
|
+
continue
|
|
545
|
+
uivps: typing.Set[UIVSPair] = set()
|
|
546
|
+
u = parse_uni_repr(none_as_empty(row[3]))
|
|
547
|
+
uivps.add(UIVSPair(u, -1))
|
|
548
|
+
iu = 0
|
|
549
|
+
if row[4]:
|
|
550
|
+
iu = parse_uni_repr(none_as_empty(row[4]))
|
|
551
|
+
uivps.add(UIVSPair(iu, -1))
|
|
552
|
+
if row[5]:
|
|
553
|
+
useqs = none_as_empty(row[5]).split(";")
|
|
554
|
+
for r in useqs:
|
|
555
|
+
us = parse_underscore_delimited_uni_hex_repr(r)
|
|
556
|
+
assert len(us) == 2
|
|
557
|
+
ivn = resolve_ivs_no(us[1])
|
|
558
|
+
uivps.add(
|
|
559
|
+
UIVSPair(us[0], ivn),
|
|
560
|
+
)
|
|
561
|
+
if row[6]:
|
|
562
|
+
useqs = none_as_empty(row[6]).split(";")
|
|
563
|
+
for r in useqs:
|
|
564
|
+
us = parse_underscore_delimited_uni_hex_repr(r)
|
|
565
|
+
assert len(us) == 2
|
|
566
|
+
ivn = resolve_ivs_no(us[1])
|
|
567
|
+
uivps.add(
|
|
568
|
+
UIVSPair(us[0], ivn),
|
|
569
|
+
)
|
|
570
|
+
max_variants = max(max_variants, len(uivps))
|
|
571
|
+
mappings.append(
|
|
572
|
+
MJMapping(
|
|
573
|
+
mj=mj,
|
|
574
|
+
iu=iu,
|
|
575
|
+
v=tuple(sorted(uivps, key=lambda uivp: (uivp.u, uivp.s))),
|
|
576
|
+
),
|
|
577
|
+
)
|
|
578
|
+
|
|
579
|
+
return (mappings, max_variants)
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
MJShrinkMappingUnicodeSet = typing.Tuple[
|
|
583
|
+
typing.Sequence[int],
|
|
584
|
+
typing.Sequence[int],
|
|
585
|
+
typing.Sequence[int],
|
|
586
|
+
typing.Sequence[int],
|
|
587
|
+
]
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
class MJShrinkMapping(typing.NamedTuple):
|
|
591
|
+
src_mj: int
|
|
592
|
+
us: MJShrinkMappingUnicodeSet
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
class MJShrinkScheme(enum.IntEnum):
|
|
596
|
+
JIS_INCORPORATION_UCS_UNIFICATION_RULE = 0
|
|
597
|
+
INFERENCE_BY_READING_AND_GLYPH = 1
|
|
598
|
+
MOJ_NOTICE_582 = 2
|
|
599
|
+
MOJ_FAMILY_REGISTER_ACT_RELATED_NOTICE = 3
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
enum_to_shrink_scheme_name_map: typing.Mapping[MJShrinkScheme, str] = {
|
|
603
|
+
MJShrinkScheme.JIS_INCORPORATION_UCS_UNIFICATION_RULE: "JIS包摂規準・UCS統合規則",
|
|
604
|
+
MJShrinkScheme.INFERENCE_BY_READING_AND_GLYPH: "読み・字形による類推",
|
|
605
|
+
MJShrinkScheme.MOJ_NOTICE_582: "法務省告示582号別表第四",
|
|
606
|
+
MJShrinkScheme.MOJ_FAMILY_REGISTER_ACT_RELATED_NOTICE: "法務省戸籍法関連通達・通知",
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
def read_mj_shrink_file(src: str) -> typing.Sequence[MJShrinkMapping]:
|
|
611
|
+
data: typing.Dict[str, typing.Any]
|
|
612
|
+
with open(src) as f:
|
|
613
|
+
data = json.load(f)
|
|
614
|
+
|
|
615
|
+
content = typing.cast(typing.List[typing.Dict[str, typing.Any]], data["content"])
|
|
616
|
+
|
|
617
|
+
mappings: typing.List[MJShrinkMapping] = []
|
|
618
|
+
scheme_names = [enum_to_shrink_scheme_name_map[k] for k in MJShrinkScheme]
|
|
619
|
+
|
|
620
|
+
for entry in content:
|
|
621
|
+
us = typing.cast(
|
|
622
|
+
MJShrinkMappingUnicodeSet,
|
|
623
|
+
tuple(
|
|
624
|
+
tuple(
|
|
625
|
+
sorted(set(parse_uni_repr(x["UCS"]) for x in entry[k])) if k in entry else ()
|
|
626
|
+
)
|
|
627
|
+
for k in scheme_names
|
|
628
|
+
),
|
|
629
|
+
)
|
|
630
|
+
if not any(u for u in us):
|
|
631
|
+
continue
|
|
632
|
+
mappings.append(
|
|
633
|
+
MJShrinkMapping(
|
|
634
|
+
src_mj=parse_mj_repr(entry["MJ文字図形名"]),
|
|
635
|
+
us=us,
|
|
636
|
+
),
|
|
637
|
+
)
|
|
638
|
+
|
|
639
|
+
return mappings
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
T = typing.TypeVar("T")
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
def iter_pad(
|
|
646
|
+
in_: typing.Union[typing.Iterable[T], typing.Iterator[T]], n: int, p: T
|
|
647
|
+
) -> typing.Iterator[T]:
|
|
648
|
+
c = 0
|
|
649
|
+
for v in iter(in_):
|
|
650
|
+
if c < n:
|
|
651
|
+
yield v
|
|
652
|
+
else:
|
|
653
|
+
yield p
|
|
654
|
+
c += 1
|
|
655
|
+
while c < n:
|
|
656
|
+
yield p
|
|
657
|
+
c += 1
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
def build_reverse_mappings(
|
|
661
|
+
mappings: typing.Sequence[ShrinkingTransliterationMapping],
|
|
662
|
+
gap_thr: int,
|
|
663
|
+
) -> typing.Tuple[typing.Sequence[URangeToJISMapping], typing.Sequence[Outer]]:
|
|
664
|
+
rm: typing.List[URangeToJISMapping] = []
|
|
665
|
+
x: typing.List[ShrinkingTransliterationMapping] = []
|
|
666
|
+
|
|
667
|
+
for m in mappings:
|
|
668
|
+
x.append(m)
|
|
669
|
+
if m.sus:
|
|
670
|
+
x.append(m._replace(us=m.sus))
|
|
671
|
+
|
|
672
|
+
x.sort(key=lambda v: v.us[0])
|
|
673
|
+
|
|
674
|
+
lr: int = -1
|
|
675
|
+
sr: int = -1
|
|
676
|
+
js: typing.List[int] = []
|
|
677
|
+
for m in x:
|
|
678
|
+
if m.class_ == JISCharacterClass.RESERVED:
|
|
679
|
+
continue
|
|
680
|
+
if len(m.us) > 1:
|
|
681
|
+
continue
|
|
682
|
+
|
|
683
|
+
r = m.us[0]
|
|
684
|
+
if lr == -1:
|
|
685
|
+
sr = r
|
|
686
|
+
else:
|
|
687
|
+
g = r - lr
|
|
688
|
+
if g >= gap_thr:
|
|
689
|
+
rm.append(URangeToJISMapping(start=sr, end=lr, jis=js))
|
|
690
|
+
js = []
|
|
691
|
+
sr = r
|
|
692
|
+
else:
|
|
693
|
+
for _ in range(1, g):
|
|
694
|
+
js.append(-1)
|
|
695
|
+
|
|
696
|
+
js.append(m.jis)
|
|
697
|
+
lr = r
|
|
698
|
+
|
|
699
|
+
if lr != -1:
|
|
700
|
+
rm.append(URangeToJISMapping(start=sr, end=lr, jis=js))
|
|
701
|
+
|
|
702
|
+
rpm: typing.List[Outer] = []
|
|
703
|
+
|
|
704
|
+
for m in x:
|
|
705
|
+
if m.class_ == JISCharacterClass.RESERVED:
|
|
706
|
+
continue
|
|
707
|
+
if len(m.us) < 2:
|
|
708
|
+
continue
|
|
709
|
+
u = m.us[0]
|
|
710
|
+
|
|
711
|
+
for i in range(0, len(rpm)):
|
|
712
|
+
if rpm[i].u == u:
|
|
713
|
+
rpm[i].n.append(m)
|
|
714
|
+
break
|
|
715
|
+
else:
|
|
716
|
+
rpm.append(Outer(u=u, n=[m]))
|
|
717
|
+
|
|
718
|
+
return rm, rpm
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
class ShrinkMappings(typing.NamedTuple):
|
|
722
|
+
smss: typing.Sequence[MJShrinkMappingUnicodeSet]
|
|
723
|
+
mje: int
|
|
724
|
+
max_lens: typing.Tuple[int, int, int, int]
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
def build_digested_shrink_mappings(
|
|
728
|
+
shrink_mappings: typing.Sequence[MJShrinkMapping],
|
|
729
|
+
) -> ShrinkMappings:
|
|
730
|
+
smss: typing.List[MJShrinkMappingUnicodeSet] = []
|
|
731
|
+
max_lens: typing.List[int] = [0, 0, 0, 0]
|
|
732
|
+
|
|
733
|
+
s = 0
|
|
734
|
+
for sm in sorted(shrink_mappings, key=lambda sm: sm.src_mj):
|
|
735
|
+
for i in range(s, sm.src_mj):
|
|
736
|
+
smss.append(((), (), (), ()))
|
|
737
|
+
else:
|
|
738
|
+
for i, us in enumerate(sm.us):
|
|
739
|
+
max_lens[i] = max(max_lens[i], len(us))
|
|
740
|
+
smss.append(sm.us)
|
|
741
|
+
s = sm.src_mj + 1
|
|
742
|
+
|
|
743
|
+
return ShrinkMappings(
|
|
744
|
+
smss=smss,
|
|
745
|
+
mje=s,
|
|
746
|
+
max_lens=typing.cast(typing.Tuple[int, int, int, int], tuple(max_lens)),
|
|
747
|
+
)
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
class URangeToMJMappings(typing.NamedTuple):
|
|
751
|
+
start: int
|
|
752
|
+
end: int
|
|
753
|
+
mss: typing.Sequence[typing.Sequence[MJMapping]]
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
def build_chunked_mj_mappings(
|
|
757
|
+
mappings: typing.Sequence[MJMapping],
|
|
758
|
+
gap_thr: int = 64,
|
|
759
|
+
) -> typing.Tuple[typing.Sequence[URangeToMJMappings], int]:
|
|
760
|
+
uni_to_mj_mappings = typing.DefaultDict[int, typing.List[MJMapping]](list)
|
|
761
|
+
|
|
762
|
+
max_mss = 0
|
|
763
|
+
for m in mappings:
|
|
764
|
+
for uivp in m.v:
|
|
765
|
+
li = uni_to_mj_mappings[uivp.u]
|
|
766
|
+
li.append(m)
|
|
767
|
+
max_mss = max(max_mss, len(li))
|
|
768
|
+
|
|
769
|
+
retval: typing.List[URangeToMJMappings] = []
|
|
770
|
+
s = -1
|
|
771
|
+
e = -1
|
|
772
|
+
chunk: typing.List[typing.List[MJMapping]] = []
|
|
773
|
+
for u, ms in sorted(uni_to_mj_mappings.items(), key=lambda pair: pair[0]):
|
|
774
|
+
if u - e >= gap_thr:
|
|
775
|
+
if chunk:
|
|
776
|
+
retval.append(
|
|
777
|
+
URangeToMJMappings(
|
|
778
|
+
start=s,
|
|
779
|
+
end=e,
|
|
780
|
+
mss=chunk,
|
|
781
|
+
),
|
|
782
|
+
)
|
|
783
|
+
chunk = []
|
|
784
|
+
s = u
|
|
785
|
+
else:
|
|
786
|
+
for uu in range(e, u - 1):
|
|
787
|
+
chunk.append([])
|
|
788
|
+
chunk.append(ms)
|
|
789
|
+
e = u
|
|
790
|
+
if chunk:
|
|
791
|
+
retval.append(
|
|
792
|
+
URangeToMJMappings(
|
|
793
|
+
start=s,
|
|
794
|
+
end=e,
|
|
795
|
+
mss=chunk,
|
|
796
|
+
),
|
|
797
|
+
)
|
|
798
|
+
return (retval, max_mss)
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
def do_jnta(dest: str, src_jnta: str, src_mj: str, src_mj_shrink: str, gap_thr: int = 256) -> None:
|
|
802
|
+
e = jinja2.Environment()
|
|
803
|
+
e.filters["iter_pad"] = iter_pad
|
|
804
|
+
t = e.from_string(code_template)
|
|
805
|
+
|
|
806
|
+
print(f"reading {src_jnta}...")
|
|
807
|
+
mappings = read_jnta_excel_file(src_jnta)
|
|
808
|
+
|
|
809
|
+
print(f"reading {src_mj}...")
|
|
810
|
+
mj_mappings, max_variants = read_mj_excel_file(src_mj)
|
|
811
|
+
|
|
812
|
+
print(f"reading {src_mj_shrink}...")
|
|
813
|
+
mj_shrink_mappings = read_mj_shrink_file(src_mj_shrink)
|
|
814
|
+
|
|
815
|
+
print("building digested shrink mappings from MJ Shrink mappings...")
|
|
816
|
+
digested_shrink_mappings = build_digested_shrink_mappings(mj_shrink_mappings)
|
|
817
|
+
|
|
818
|
+
print("chunking MJ mappings...")
|
|
819
|
+
chunked_mj_mappings, max_mss = build_chunked_mj_mappings(mj_mappings)
|
|
820
|
+
|
|
821
|
+
print("building reverse mappings...")
|
|
822
|
+
rm, rpm = build_reverse_mappings(mappings, gap_thr)
|
|
823
|
+
|
|
824
|
+
gen = t.generate(
|
|
825
|
+
JISCharacterClass=JISCharacterClass,
|
|
826
|
+
tx_mappings=mappings,
|
|
827
|
+
uni_range_to_jis_mappings=rm,
|
|
828
|
+
uni_pairs_to_jis_mappings=rpm,
|
|
829
|
+
MJShrinkScheme=MJShrinkScheme,
|
|
830
|
+
max_variants=max_variants,
|
|
831
|
+
digested_shrink_mappings=digested_shrink_mappings,
|
|
832
|
+
chunked_mj_mappings=chunked_mj_mappings,
|
|
833
|
+
max_mss=max_mss,
|
|
834
|
+
)
|
|
835
|
+
with open(dest, "w") as f:
|
|
836
|
+
for c in gen:
|
|
837
|
+
f.write(c)
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
@click.command()
|
|
841
|
+
@click.argument("dest", required=True)
|
|
842
|
+
@click.argument("src_jnta", required=True)
|
|
843
|
+
@click.argument("src_mj", required=True)
|
|
844
|
+
@click.argument("src_mj_shrink", required=True)
|
|
845
|
+
def main(dest, src_jnta, src_mj, src_mj_shrink):
|
|
846
|
+
do_jnta(dest, src_jnta, src_mj, src_mj_shrink)
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
if __name__ == "__main__":
|
|
850
|
+
main()
|