-
Notifications
You must be signed in to change notification settings - Fork 3
/
num.ts
243 lines (214 loc) · 8.25 KB
/
num.ts
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
import { assertNever } from "../util";
const reKanjiNum = /((\S*)千)?((\S*)百)?((\S*)十)?(\S*)/;
export const parseKanjiNum = (text: string): number | null => {
if (text === "元") return 1;
const m = reKanjiNum.exec(text);
if (m) {
if (!m[1] && !m[2] && !m[3] && m[7] && m[7].length > 1) {
const ds = m[7].split("").map(c => (kanjiDigitToNumDict[c as keyof typeof kanjiDigitToNumDict] || 0).toString()).join("");
const ret = Number(ds);
if (Number.isNaN(ret)) return null;
return ret;
} else {
const d1000 = m[1] ? kanjiDigitToNumDict[m[2] as keyof typeof kanjiDigitToNumDict] || 1 : 0;
const d100 = m[3] ? kanjiDigitToNumDict[m[4] as keyof typeof kanjiDigitToNumDict] || 1 : 0;
const d10 = m[5] ? kanjiDigitToNumDict[m[6] as keyof typeof kanjiDigitToNumDict] || 1 : 0;
const d1 = kanjiDigitToNumDict[m[7] as keyof typeof kanjiDigitToNumDict] || 0;
return d1000 * 1000 + d100 * 100 + d10 * 10 + d1;
}
}
return null;
};
const kanjiDigitToNumDict = {
"〇": 0, "一": 1, "二": 2, "三": 3, "四": 4,
"五": 5, "六": 6, "七": 7, "八": 8, "九": 9,
} as const;
const numToKanjiDigitDict = {
0: "〇", 1: "一", 2: "二", 3: "三", 4: "四",
5: "五", 6: "六", 7: "七", 8: "八", 9: "九",
} as Record<number, string>;
const digitToKanjiDigitDict = {
"0": "〇", "1": "一", "2": "二", "3": "三", "4": "四",
"5": "五", "6": "六", "7": "七", "8": "八", "9": "九",
} as Record<string, string>;
export const digitsToKanjiNum = (digits: number | string, type: "positional" | "non-positional"): string => {
if (type === "positional") {
const numStr = replaceWideNum(typeof digits === "string" ? digits : digits.toString());
return Array.from(numStr).map(n => digitToKanjiDigitDict[n]).join("");
} else {
const num = typeof digits === "string" ? Number(replaceWideNum(digits)) : digits;
const d1 = num % 10;
const d10 = Math.floor((num % 100) / 10);
const d100 = Math.floor((num % 1000) / 100);
const d1000 = Math.floor(num / 1000);
const parts: string[] = [];
if (d1000) parts.push(`${d1000 === 1 ? "" : numToKanjiDigitDict[d1000]}千`);
if (d100) parts.push(`${d100 === 1 ? "" : numToKanjiDigitDict[d100]}百`);
if (d10) parts.push(`${d10 === 1 ? "" : numToKanjiDigitDict[d10]}十`);
if (d1) parts.push(numToKanjiDigitDict[d1]);
return parts.join("");
}
};
export const circledDigitChars = "⓪①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿";
export const irohaChars = "イロハニホヘトチリヌルヲワカヨタレソツネナラムウヰノオクヤマケフコエテアサキユメミシヱヒモセスン";
export const aiuChars = "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン";
const reKanjiNamedNum = /^(○?)第?([〇一二三四五六七八九十百千]+)\S*?([のノ―〇一二三四五六七八九十百千]*)$/;
const reArabicNamedNum = /^(○?)第?([01234567890123456789]+)\S*?([のノ―01234567890123456789]*)$/;
const reIrohaChar = new RegExp(`[${irohaChars}]`);
const reAiuChar = new RegExp(`[${aiuChars}]`);
const reCircledDigit = new RegExp(`[${circledDigitChars}]`);
const reItemNum = /^\D*(\d+)\D*$/;
const parseRomanNum = (text: string): number => {
let num = 0;
for (let i = 0; i < text.length; i++) {
const char = text[i];
const nextChar = text[i + 1] || "";
if (/[iIiI]/.exec(char)) {
if (/[vVvVxXxX]/.exec(nextChar)) num -= 1;
else num += 1;
}
if (/[vVvV]/.exec(char)) {
num += 5;
}
if (/[xXxX]/.exec(char)) {
num += 10;
}
}
return num;
};
const reWideDigits: Array<[RegExp, string]> = [
[/0/g, "0"],
[/1/g, "1"],
[/2/g, "2"],
[/3/g, "3"],
[/4/g, "4"],
[/5/g, "5"],
[/6/g, "6"],
[/7/g, "7"],
[/8/g, "8"],
[/9/g, "9"],
];
const replaceWideNum = (text: string): string => {
let ret = text;
for (const [reWide, narrow] of reWideDigits) {
ret = ret.replace(reWide, narrow);
}
return ret;
};
export enum KanaMode {
// eslint-disable-next-line no-unused-vars
Iroha = "Iroha",
// eslint-disable-next-line no-unused-vars
Aiu = "Aiu",
}
export const parseNamedNum = (text: string, kanaMode: KanaMode = KanaMode.Iroha): string => {
const numsGroup: string[] = [];
const subtexts = text
.split(/\s+/)[0]
.replace("及び", "、")
.replace("から", "、")
.replace("まで", "")
.replace("~", "、")
.replace("・", "、")
.replace("乃至", "、")
.replace("及ビ", "、")
.replace("及", "、")
.split("、");
for (const subtext of subtexts) {
{
const m = reKanjiNamedNum.exec(subtext);
if (m) {
const nums = [parseKanjiNum(m[2])];
if (m[3]) {
const bs = m[3].split(/[のノ―]/g);
for (const b of bs) {
if (!b) continue;
nums.push(parseKanjiNum(b));
}
}
numsGroup.push(nums.join("_"));
continue;
}
}
{
const m = reArabicNamedNum.exec(subtext);
if (m) {
const nums = [replaceWideNum(m[2])];
if (m[3]) {
const bs = m[3].split(/[のノ―]/g);
for (const b of bs) {
if (!b) continue;
nums.push(replaceWideNum(b));
}
}
numsGroup.push(nums.join("_"));
continue;
}
}
if (kanaMode === KanaMode.Iroha) {
const m = reIrohaChar.exec(subtext);
if (m) {
numsGroup.push(String(irohaChars.indexOf(m[0]) + 1));
continue;
}
} else if (kanaMode === KanaMode.Aiu) {
const m = reAiuChar.exec(subtext);
if (m) {
numsGroup.push(String(aiuChars.indexOf(m[0]) + 1));
continue;
}
} else { throw assertNever(kanaMode); }
{
const m = reCircledDigit.exec(subtext);
if (m) {
numsGroup.push(String(circledDigitChars.indexOf(m[0])));
continue;
}
}
{
const replacedSubtext = replaceWideNum(subtext);
const m = reItemNum.exec(replacedSubtext);
if (m) {
numsGroup.push(m[1]);
continue;
}
const romanNum = parseRomanNum(replacedSubtext);
if (romanNum !== 0) {
numsGroup.push(String(romanNum));
}
}
}
return numsGroup.join(":");
};
// export const setItemNum = (els: EL[]): void => {
// const items: Array<Diff<std.ParagraphItem, std.Paragraph>> = [];
// for (const el of els) {
// if (std.isParagraphItem(el) && el.tag !== "Paragraph") {
// items.push(el);
// }
// }
// if (items.length) {
// let kanaMode = KanaMode.Iroha;
// for (const child of items[0].children) {
// if (std.isParagraphItemTitle(child)) {
// if (/ア/.exec(child.text())) {
// kanaMode = KanaMode.Aiu;
// break;
// }
// }
// }
// for (const item of items) {
// let paragraphItemTitle = "";
// for (const child of item.children) {
// if (std.isParagraphItemTitle(child)) {
// paragraphItemTitle = child.text();
// break;
// }
// }
// const num = parseNamedNum(paragraphItemTitle, kanaMode);
// if (num) {
// item.attr.Num = num;
// }
// }
// }
// };