aboutsummaryrefslogtreecommitdiff
path: root/ext/js/language/ja/japanese.js
blob: 571abf26e0e09ce4d3c9ad3a638a127bc684cb44 (plain)
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/*
 * Copyright (C) 2024  Yomitan Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import {CJK_IDEOGRAPH_RANGES, isCodePointInRange, isCodePointInRanges} from '../CJK-util.js';


const HIRAGANA_SMALL_TSU_CODE_POINT = 0x3063;
const KATAKANA_SMALL_TSU_CODE_POINT = 0x30c3;
const KATAKANA_SMALL_KA_CODE_POINT = 0x30f5;
const KATAKANA_SMALL_KE_CODE_POINT = 0x30f6;
const KANA_PROLONGED_SOUND_MARK_CODE_POINT = 0x30fc;

/** @type {import('CJK-util').CodepointRange} */
const HIRAGANA_RANGE = [0x3040, 0x309f];
/** @type {import('CJK-util').CodepointRange} */
const KATAKANA_RANGE = [0x30a0, 0x30ff];

/** @type {import('CJK-util').CodepointRange} */
const HIRAGANA_CONVERSION_RANGE = [0x3041, 0x3096];
/** @type {import('CJK-util').CodepointRange} */
const KATAKANA_CONVERSION_RANGE = [0x30a1, 0x30f6];

/** @type {import('CJK-util').CodepointRange[]} */
const KANA_RANGES = [HIRAGANA_RANGE, KATAKANA_RANGE];

/**
 * Japanese character ranges, roughly ordered in order of expected frequency.
 * @type {import('CJK-util').CodepointRange[]}
 */
const JAPANESE_RANGES = [
    HIRAGANA_RANGE,
    KATAKANA_RANGE,

    ...CJK_IDEOGRAPH_RANGES,

    [0xff66, 0xff9f], // Halfwidth katakana

    [0x30fb, 0x30fc], // Katakana punctuation
    [0xff61, 0xff65], // Kana punctuation
    [0x3000, 0x303f], // CJK punctuation

    [0xff10, 0xff19], // Fullwidth numbers
    [0xff21, 0xff3a], // Fullwidth upper case Latin letters
    [0xff41, 0xff5a], // Fullwidth lower case Latin letters

    [0xff01, 0xff0f], // Fullwidth punctuation 1
    [0xff1a, 0xff1f], // Fullwidth punctuation 2
    [0xff3b, 0xff3f], // Fullwidth punctuation 3
    [0xff5b, 0xff60], // Fullwidth punctuation 4
    [0xffe0, 0xffee], // Currency markers
];

const SMALL_KANA_SET = new Set('ぁぃぅぇぉゃゅょゎァィゥェォャュョヮ');

const HALFWIDTH_KATAKANA_MAPPING = new Map([
    ['・', '・--'],
    ['ヲ', 'ヲヺ-'],
    ['ァ', 'ァ--'],
    ['ィ', 'ィ--'],
    ['ゥ', 'ゥ--'],
    ['ェ', 'ェ--'],
    ['ォ', 'ォ--'],
    ['ャ', 'ャ--'],
    ['ュ', 'ュ--'],
    ['ョ', 'ョ--'],
    ['ッ', 'ッ--'],
    ['ー', 'ー--'],
    ['ア', 'ア--'],
    ['イ', 'イ--'],
    ['ウ', 'ウヴ-'],
    ['エ', 'エ--'],
    ['オ', 'オ--'],
    ['カ', 'カガ-'],
    ['キ', 'キギ-'],
    ['ク', 'クグ-'],
    ['ケ', 'ケゲ-'],
    ['コ', 'コゴ-'],
    ['サ', 'サザ-'],
    ['シ', 'シジ-'],
    ['ス', 'スズ-'],
    ['セ', 'セゼ-'],
    ['ソ', 'ソゾ-'],
    ['タ', 'タダ-'],
    ['チ', 'チヂ-'],
    ['ツ', 'ツヅ-'],
    ['テ', 'テデ-'],
    ['ト', 'トド-'],
    ['ナ', 'ナ--'],
    ['ニ', 'ニ--'],
    ['ヌ', 'ヌ--'],
    ['ネ', 'ネ--'],
    ['ノ', 'ノ--'],
    ['ハ', 'ハバパ'],
    ['ヒ', 'ヒビピ'],
    ['フ', 'フブプ'],
    ['ヘ', 'ヘベペ'],
    ['ホ', 'ホボポ'],
    ['マ', 'マ--'],
    ['ミ', 'ミ--'],
    ['ム', 'ム--'],
    ['メ', 'メ--'],
    ['モ', 'モ--'],
    ['ヤ', 'ヤ--'],
    ['ユ', 'ユ--'],
    ['ヨ', 'ヨ--'],
    ['ラ', 'ラ--'],
    ['リ', 'リ--'],
    ['ル', 'ル--'],
    ['レ', 'レ--'],
    ['ロ', 'ロ--'],
    ['ワ', 'ワ--'],
    ['ン', 'ン--'],
]);

const VOWEL_TO_KANA_MAPPING = new Map([
    ['a', 'ぁあかがさざただなはばぱまゃやらゎわヵァアカガサザタダナハバパマャヤラヮワヵヷ'],
    ['i', 'ぃいきぎしじちぢにひびぴみりゐィイキギシジチヂニヒビピミリヰヸ'],
    ['u', 'ぅうくぐすずっつづぬふぶぷむゅゆるゥウクグスズッツヅヌフブプムュユルヴ'],
    ['e', 'ぇえけげせぜてでねへべぺめれゑヶェエケゲセゼテデネヘベペメレヱヶヹ'],
    ['o', 'ぉおこごそぞとどのほぼぽもょよろをォオコゴソゾトドノホボポモョヨロヲヺ'],
    ['', 'のノ'],
]);

/** @type {Map<string, string>} */
const KANA_TO_VOWEL_MAPPING = new Map();
for (const [vowel, characters] of VOWEL_TO_KANA_MAPPING) {
    for (const character of characters) {
        KANA_TO_VOWEL_MAPPING.set(character, vowel);
    }
}

const kana = 'うゔ-かが-きぎ-くぐ-けげ-こご-さざ-しじ-すず-せぜ-そぞ-ただ-ちぢ-つづ-てで-とど-はばぱひびぴふぶぷへべぺほぼぽワヷ-ヰヸ-ウヴ-ヱヹ-ヲヺ-カガ-キギ-クグ-ケゲ-コゴ-サザ-シジ-スズ-セゼ-ソゾ-タダ-チヂ-ツヅ-テデ-トド-ハバパヒビピフブプヘベペホボポ';
/** @type {Map<string, {character: string, type: import('japanese-util').DiacriticType}>} */
const DIACRITIC_MAPPING = new Map();
for (let i = 0, ii = kana.length; i < ii; i += 3) {
    const character = kana[i];
    const dakuten = kana[i + 1];
    const handakuten = kana[i + 2];
    DIACRITIC_MAPPING.set(dakuten, {character, type: 'dakuten'});
    if (handakuten !== '-') {
        DIACRITIC_MAPPING.set(handakuten, {character, type: 'handakuten'});
    }
}

/**
 * @param {string} previousCharacter
 * @returns {?string}
 */
function getProlongedHiragana(previousCharacter) {
    switch (KANA_TO_VOWEL_MAPPING.get(previousCharacter)) {
        case 'a': return 'あ';
        case 'i': return 'い';
        case 'u': return 'う';
        case 'e': return 'え';
        case 'o': return 'う';
        default: return null;
    }
}

/**
 * @param {string} text
 * @param {string} reading
 * @returns {import('japanese-util').FuriganaSegment}
 */
function createFuriganaSegment(text, reading) {
    return {text, reading};
}

/**
 * @param {string} reading
 * @param {string} readingNormalized
 * @param {import('japanese-util').FuriganaGroup[]} groups
 * @param {number} groupsStart
 * @returns {?(import('japanese-util').FuriganaSegment[])}
 */
function segmentizeFurigana(reading, readingNormalized, groups, groupsStart) {
    const groupCount = groups.length - groupsStart;
    if (groupCount <= 0) {
        return reading.length === 0 ? [] : null;
    }

    const group = groups[groupsStart];
    const {isKana, text} = group;
    const textLength = text.length;
    if (isKana) {
        const {textNormalized} = group;
        if (textNormalized !== null && readingNormalized.startsWith(textNormalized)) {
            const segments = segmentizeFurigana(
                reading.substring(textLength),
                readingNormalized.substring(textLength),
                groups,
                groupsStart + 1,
            );
            if (segments !== null) {
                if (reading.startsWith(text)) {
                    segments.unshift(createFuriganaSegment(text, ''));
                } else {
                    segments.unshift(...getFuriganaKanaSegments(text, reading));
                }
                return segments;
            }
        }
        return null;
    } else {
        let result = null;
        for (let i = reading.length; i >= textLength; --i) {
            const segments = segmentizeFurigana(
                reading.substring(i),
                readingNormalized.substring(i),
                groups,
                groupsStart + 1,
            );
            if (segments !== null) {
                if (result !== null) {
                    // More than one way to segmentize the tail; mark as ambiguous
                    return null;
                }
                const segmentReading = reading.substring(0, i);
                segments.unshift(createFuriganaSegment(text, segmentReading));
                result = segments;
            }
            // There is only one way to segmentize the last non-kana group
            if (groupCount === 1) {
                break;
            }
        }
        return result;
    }
}

/**
 * @param {string} text
 * @param {string} reading
 * @returns {import('japanese-util').FuriganaSegment[]}
 */
function getFuriganaKanaSegments(text, reading) {
    const textLength = text.length;
    const newSegments = [];
    let start = 0;
    let state = (reading[0] === text[0]);
    for (let i = 1; i < textLength; ++i) {
        const newState = (reading[i] === text[i]);
        if (state === newState) { continue; }
        newSegments.push(createFuriganaSegment(text.substring(start, i), state ? '' : reading.substring(start, i)));
        state = newState;
        start = i;
    }
    newSegments.push(createFuriganaSegment(text.substring(start, textLength), state ? '' : reading.substring(start, textLength)));
    return newSegments;
}

/**
 * @param {string} text1
 * @param {string} text2
 * @returns {number}
 */
function getStemLength(text1, text2) {
    const minLength = Math.min(text1.length, text2.length);
    if (minLength === 0) { return 0; }

    let i = 0;
    while (true) {
        const char1 = /** @type {number} */ (text1.codePointAt(i));
        const char2 = /** @type {number} */ (text2.codePointAt(i));
        if (char1 !== char2) { break; }
        const charLength = String.fromCodePoint(char1).length;
        i += charLength;
        if (i >= minLength) {
            if (i > minLength) {
                i -= charLength; // Don't consume partial UTF16 surrogate characters
            }
            break;
        }
    }
    return i;
}


// Character code testing functions

/**
 * @param {number} codePoint
 * @returns {boolean}
 */
export function isCodePointKanji(codePoint) {
    return isCodePointInRanges(codePoint, CJK_IDEOGRAPH_RANGES);
}

/**
 * @param {number} codePoint
 * @returns {boolean}
 */
export function isCodePointKana(codePoint) {
    return isCodePointInRanges(codePoint, KANA_RANGES);
}

/**
 * @param {number} codePoint
 * @returns {boolean}
 */
export function isCodePointJapanese(codePoint) {
    return isCodePointInRanges(codePoint, JAPANESE_RANGES);
}


// String testing functions

/**
 * @param {string} str
 * @returns {boolean}
 */
export function isStringEntirelyKana(str) {
    if (str.length === 0) { return false; }
    for (const c of str) {
        if (!isCodePointInRanges(/** @type {number} */ (c.codePointAt(0)), KANA_RANGES)) {
            return false;
        }
    }
    return true;
}

/**
 * @param {string} str
 * @returns {boolean}
 */
export function isStringPartiallyJapanese(str) {
    if (str.length === 0) { return false; }
    for (const c of str) {
        if (isCodePointInRanges(/** @type {number} */ (c.codePointAt(0)), JAPANESE_RANGES)) {
            return true;
        }
    }
    return false;
}


// Mora functions

/**
 * @param {number} moraIndex
 * @param {number} pitchAccentDownstepPosition
 * @returns {boolean}
 */
export function isMoraPitchHigh(moraIndex, pitchAccentDownstepPosition) {
    switch (pitchAccentDownstepPosition) {
        case 0: return (moraIndex > 0);
        case 1: return (moraIndex < 1);
        default: return (moraIndex > 0 && moraIndex < pitchAccentDownstepPosition);
    }
}

/**
 * @param {string} text
 * @param {number} pitchAccentDownstepPosition
 * @param {boolean} isVerbOrAdjective
 * @returns {?import('japanese-util').PitchCategory}
 */
export function getPitchCategory(text, pitchAccentDownstepPosition, isVerbOrAdjective) {
    if (pitchAccentDownstepPosition === 0) {
        return 'heiban';
    }
    if (isVerbOrAdjective) {
        return pitchAccentDownstepPosition > 0 ? 'kifuku' : null;
    }
    if (pitchAccentDownstepPosition === 1) {
        return 'atamadaka';
    }
    if (pitchAccentDownstepPosition > 1) {
        return pitchAccentDownstepPosition >= getKanaMoraCount(text) ? 'odaka' : 'nakadaka';
    }
    return null;
}

/**
 * @param {string} text
 * @returns {string[]}
 */
export function getKanaMorae(text) {
    const morae = [];
    let i;
    for (const c of text) {
        if (SMALL_KANA_SET.has(c) && (i = morae.length) > 0) {
            morae[i - 1] += c;
        } else {
            morae.push(c);
        }
    }
    return morae;
}

/**
 * @param {string} text
 * @returns {number}
 */
export function getKanaMoraCount(text) {
    let moraCount = 0;
    for (const c of text) {
        if (!(SMALL_KANA_SET.has(c) && moraCount > 0)) {
            ++moraCount;
        }
    }
    return moraCount;
}


// Conversion functions

/**
 * @param {string} text
 * @param {boolean} [keepProlongedSoundMarks]
 * @returns {string}
 */
export function convertKatakanaToHiragana(text, keepProlongedSoundMarks = false) {
    let result = '';
    const offset = (HIRAGANA_CONVERSION_RANGE[0] - KATAKANA_CONVERSION_RANGE[0]);
    for (let char of text) {
        const codePoint = /** @type {number} */ (char.codePointAt(0));
        switch (codePoint) {
            case KATAKANA_SMALL_KA_CODE_POINT:
            case KATAKANA_SMALL_KE_CODE_POINT:
                // No change
                break;
            case KANA_PROLONGED_SOUND_MARK_CODE_POINT:
                if (!keepProlongedSoundMarks && result.length > 0) {
                    const char2 = getProlongedHiragana(result[result.length - 1]);
                    if (char2 !== null) { char = char2; }
                }
                break;
            default:
                if (isCodePointInRange(codePoint, KATAKANA_CONVERSION_RANGE)) {
                    char = String.fromCodePoint(codePoint + offset);
                }
                break;
        }
        result += char;
    }
    return result;
}

/**
 * @param {string} text
 * @returns {string}
 */
export function convertHiraganaToKatakana(text) {
    let result = '';
    const offset = (KATAKANA_CONVERSION_RANGE[0] - HIRAGANA_CONVERSION_RANGE[0]);
    for (let char of text) {
        const codePoint = /** @type {number} */ (char.codePointAt(0));
        if (isCodePointInRange(codePoint, HIRAGANA_CONVERSION_RANGE)) {
            char = String.fromCodePoint(codePoint + offset);
        }
        result += char;
    }
    return result;
}

/**
 * @param {string} text
 * @returns {string}
 */
export function convertAlphanumericToFullWidth(text) {
    let result = '';
    for (const char of text) {
        let c = /** @type {number} */ (char.codePointAt(0));
        if (c >= 0x30 && c <= 0x39) { // ['0', '9']
            c += 0xff10 - 0x30; // 0xff10 = '0' full width
        } else if (c >= 0x41 && c <= 0x5a) { // ['A', 'Z']
            c += 0xff21 - 0x41; // 0xff21 = 'A' full width
        } else if (c >= 0x61 && c <= 0x7a) { // ['a', 'z']
            c += 0xff41 - 0x61; // 0xff41 = 'a' full width
        }
        result += String.fromCodePoint(c);
    }
    return result;
}

/**
 * @param {string} text
 * @returns {string}
 */
export function convertFullWidthAlphanumericToNormal(text) {
    let result = '';
    const length = text.length;
    for (let i = 0; i < length; i++) {
        let c = /** @type {number} */ (text[i].codePointAt(0));
        if (c >= 0xff10 && c <= 0xff19) { // ['0', '9']
            c -= 0xff10 - 0x30; // 0x30 = '0'
        } else if (c >= 0xff21 && c <= 0xff3a) { // ['A', 'Z']
            c -= 0xff21 - 0x41; // 0x41 = 'A'
        } else if (c >= 0xff41 && c <= 0xff5a) { // ['a', 'z']
            c -= 0xff41 - 0x61; // 0x61 = 'a'
        }
        result += String.fromCodePoint(c);
    }
    return result;
}

/**
 * @param {string} text
 * @returns {string}
 */
export function convertHalfWidthKanaToFullWidth(text) {
    let result = '';

    // This function is safe to use charCodeAt instead of codePointAt, since all
    // the relevant characters are represented with a single UTF-16 character code.
    for (let i = 0, ii = text.length; i < ii; ++i) {
        const c = text[i];
        const mapping = HALFWIDTH_KATAKANA_MAPPING.get(c);
        if (typeof mapping !== 'string') {
            result += c;
            continue;
        }

        let index = 0;
        switch (text.charCodeAt(i + 1)) {
            case 0xff9e: // Dakuten
                index = 1;
                break;
            case 0xff9f: // Handakuten
                index = 2;
                break;
        }

        let c2 = mapping[index];
        if (index > 0) {
            if (c2 === '-') { // Invalid
                index = 0;
                c2 = mapping[0];
            } else {
                ++i;
            }
        }

        result += c2;
    }

    return result;
}

/**
 * @param {string} character
 * @returns {?{character: string, type: import('japanese-util').DiacriticType}}
 */
export function getKanaDiacriticInfo(character) {
    const info = DIACRITIC_MAPPING.get(character);
    return typeof info !== 'undefined' ? {character: info.character, type: info.type} : null;
}

/**
 * @param {number} codePoint
 * @returns {boolean}
 */
function dakutenAllowed(codePoint) {
    // To reduce processing time some characters which shouldn't have dakuten but are highly unlikely to have a combining character attached are included
    // かがきぎくぐけげこごさざしじすずせぜそぞただちぢっつづてでとはばぱひびぴふぶぷへべぺほ
    // カガキギクグケゲコゴサザシジスズセゼソゾタダチヂッツヅテデトハバパヒビピフブプヘベペホ
    return ((codePoint >= 0x304B && codePoint <= 0x3068) ||
    (codePoint >= 0x306F && codePoint <= 0x307B) ||
    (codePoint >= 0x30AB && codePoint <= 0x30C8) ||
    (codePoint >= 0x30CF && codePoint <= 0x30DB));
}

/**
 * @param {number} codePoint
 * @returns {boolean}
 */
function handakutenAllowed(codePoint) {
    // To reduce processing time some characters which shouldn't have handakuten but are highly unlikely to have a combining character attached are included
    // はばぱひびぴふぶぷへべぺほ
    // ハバパヒビピフブプヘベペホ
    return ((codePoint >= 0x306F && codePoint <= 0x307B) ||
    (codePoint >= 0x30CF && codePoint <= 0x30DB));
}

/**
 * @param {string} text
 * @returns {string}
 */
export function normalizeCombiningCharacters(text) {
    let result = '';
    let i = text.length - 1;
    // Ignoring the first character is intentional, it cannot combine with anything
    while (i > 0) {
        if (text[i] === '\u3099') {
            const dakutenCombinee = text[i - 1].codePointAt(0);
            if (dakutenCombinee && dakutenAllowed(dakutenCombinee)) {
                result = String.fromCodePoint(dakutenCombinee + 1) + result;
                i -= 2;
                continue;
            }
        } else if (text[i] === '\u309A') {
            const handakutenCombinee = text[i - 1].codePointAt(0);
            if (handakutenCombinee && handakutenAllowed(handakutenCombinee)) {
                result = String.fromCodePoint(handakutenCombinee + 2) + result;
                i -= 2;
                continue;
            }
        }
        result = text[i] + result;
        i--;
    }
    // i === -1 when first two characters are combined
    if (i === 0) {
        result = text[0] + result;
    }
    return result;
}

// Furigana distribution

/**
 * @param {string} term
 * @param {string} reading
 * @returns {import('japanese-util').FuriganaSegment[]}
 */
export function distributeFurigana(term, reading) {
    if (reading === term) {
        // Same
        return [createFuriganaSegment(term, '')];
    }

    /** @type {import('japanese-util').FuriganaGroup[]} */
    const groups = [];
    /** @type {?import('japanese-util').FuriganaGroup} */
    let groupPre = null;
    let isKanaPre = null;
    for (const c of term) {
        const codePoint = /** @type {number} */ (c.codePointAt(0));
        const isKana = isCodePointKana(codePoint);
        if (isKana === isKanaPre) {
            /** @type {import('japanese-util').FuriganaGroup} */ (groupPre).text += c;
        } else {
            groupPre = {isKana, text: c, textNormalized: null};
            groups.push(groupPre);
            isKanaPre = isKana;
        }
    }
    for (const group of groups) {
        if (group.isKana) {
            group.textNormalized = convertKatakanaToHiragana(group.text);
        }
    }

    const readingNormalized = convertKatakanaToHiragana(reading);
    const segments = segmentizeFurigana(reading, readingNormalized, groups, 0);
    if (segments !== null) {
        return segments;
    }

    // Fallback
    return [createFuriganaSegment(term, reading)];
}

/**
 * @param {string} term
 * @param {string} reading
 * @param {string} source
 * @returns {import('japanese-util').FuriganaSegment[]}
 */
export function distributeFuriganaInflected(term, reading, source) {
    const termNormalized = convertKatakanaToHiragana(term);
    const readingNormalized = convertKatakanaToHiragana(reading);
    const sourceNormalized = convertKatakanaToHiragana(source);

    let mainText = term;
    let stemLength = getStemLength(termNormalized, sourceNormalized);

    // Check if source is derived from the reading instead of the term
    const readingStemLength = getStemLength(readingNormalized, sourceNormalized);
    if (readingStemLength > 0 && readingStemLength >= stemLength) {
        mainText = reading;
        stemLength = readingStemLength;
        reading = `${source.substring(0, stemLength)}${reading.substring(stemLength)}`;
    }

    const segments = [];
    if (stemLength > 0) {
        mainText = `${source.substring(0, stemLength)}${mainText.substring(stemLength)}`;
        const segments2 = distributeFurigana(mainText, reading);
        let consumed = 0;
        for (const segment of segments2) {
            const {text} = segment;
            const start = consumed;
            consumed += text.length;
            if (consumed < stemLength) {
                segments.push(segment);
            } else if (consumed === stemLength) {
                segments.push(segment);
                break;
            } else {
                if (start < stemLength) {
                    segments.push(createFuriganaSegment(mainText.substring(start, stemLength), ''));
                }
                break;
            }
        }
    }

    if (stemLength < source.length) {
        const remainder = source.substring(stemLength);
        const segmentCount = segments.length;
        if (segmentCount > 0 && segments[segmentCount - 1].reading.length === 0) {
            // Append to the last segment if it has an empty reading
            segments[segmentCount - 1].text += remainder;
        } else {
            // Otherwise, create a new segment
            segments.push(createFuriganaSegment(remainder, ''));
        }
    }

    return segments;
}


// Miscellaneous

/**
 * @param {string} text
 * @param {boolean} fullCollapse
 * @returns {string}
 */
export function collapseEmphaticSequences(text, fullCollapse) {
    let result = '';
    let collapseCodePoint = -1;
    for (const char of text) {
        const c = char.codePointAt(0);
        if (
            c === HIRAGANA_SMALL_TSU_CODE_POINT ||
            c === KATAKANA_SMALL_TSU_CODE_POINT ||
            c === KANA_PROLONGED_SOUND_MARK_CODE_POINT
        ) {
            if (collapseCodePoint !== c) {
                collapseCodePoint = c;
                if (!fullCollapse) {
                    result += char;
                    continue;
                }
            }
        } else {
            collapseCodePoint = -1;
            result += char;
        }
    }
    return result;
}