util/romaji_transliteration: add functions to generate transliteration spans
All checks were successful
Build and test / evals (push) Successful in 18m58s

This commit is contained in:
2026-03-02 18:21:06 +09:00
parent d14e3909d4
commit a86f857553
2 changed files with 154 additions and 0 deletions

View File

@@ -37,6 +37,35 @@ void main() {
});
});
group('Romaji -> Hiragana Spans', () {
void Function() expectSpans(String input, List<String> expected) => () {
final result = transliterateLatinToHiraganaSpan(input);
final trans = transliterateLatinToHiragana(input);
for (int i = 0; i < result.length; i++) {
expect(
trans.substring(
result[i].$2,
i == result.length - 1 ? trans.length : result[i + 1].$2,
),
expected[i],
);
}
};
test('Basic test', expectSpans('katamari', ['', '', '', '']));
test(
'Basic test with diacritics',
expectSpans('gadamari', ['', '', '', '']),
);
test('wi and we', expectSpans('wiwe', ['うぃ', 'うぇ']));
test('nb = mb', expectSpans('kanpai', ['', '', '', '']));
test('nb = mb', expectSpans('kampai', ['', '', '', '']));
test('Double n', expectSpans('konnichiha', ['', '', '', '', '']));
// TODO: fix the implementation
// test('Double consonant', expectSpans('kappa', ['か', 'っぱ']));
});
group('Hiragana -> Romaji', () {
test('Basic test', () {
final result = transliterateHiraganaToLatin('かたまり');
@@ -63,4 +92,31 @@ void main() {
expect(result, 'kappa');
});
});
group('Hiragana -> Romaji Spans', () {
void Function() expectSpans(String input, List<String> expected) => () {
final result = transliterateHiraganaToLatinSpan(input);
final trans = transliterateHiraganaToLatin(input);
for (int i = 0; i < result.length; i++) {
expect(
trans.substring(
result[i].$2,
i == result.length - 1 ? trans.length : result[i + 1].$2,
),
expected[i],
);
}
};
test('Basic test', expectSpans('かたまり', ['ka', 'ta', 'ma', 'ri']));
test(
'Basic test with diacritics',
expectSpans('がだまり', ['ga', 'da', 'ma', 'ri']),
);
test('wi and we', expectSpans('うぃうぇ', ['whi', 'whe']));
test('Double n', expectSpans('こんにちは', ['ko', 'n', 'ni', 'chi', 'ha']));
// TODO: fix the implementation
// test('Double consonant', expectSpans('かっぱ', ['ka', 'ppa']));
});
}