convertKoreanNumerals function

String convertKoreanNumerals(
  1. String input
)

Converts all annotated numeral spans using pinned g2pkc semantics.

The returned intermediate text can contain /B morphology markers and ^ rule blockers; KoreanG2pkcEngine consumes them in later stages.

Implementation

String convertKoreanNumerals(String input) {
  var output = input;
  final tokens = <(String, String)>{};
  for (final match in _numberPattern.allMatches(output)) {
    tokens.add((match.group(1)!, match.group(2) ?? ''));
  }
  // Upstream converts `set(re.findall(...))` in iteration order. Replacement
  // is global, so that order is observable for overlapping bare numbers and
  // bound-noun spans. Fixture generation pins CPython 3.12 with
  // PYTHONHASHSEED=0; replay its tuple hashes and set-table iteration exactly.
  for (final (number, boundNoun) in _python312Seed0SetOrder(tokens)) {
    final noun = boundNoun.trimLeft();
    final spelled = spellKoreanNumber(
      number,
      sino: !_boundNouns.contains(noun),
    );
    output = output.replaceAll('$number$boundNoun', '$spelled$noun');
  }
  for (var index = 0; index < _digitNames.length; index++) {
    output = output.replaceAll('$index', '^${_digitNames[index]}');
  }
  return output.replaceAll('십^육', '심뉵').replaceAll('백^육', '뱅뉵');
}