convert method

  1. @override
G2pResult convert(
  1. String text
)
override

Converts text into exact phoneme output and optional token details.

Implementation

@override
G2pResult convert(String text) {
  if (text.isEmpty) {
    return G2pResult(phonemes: '', tokens: null);
  }
  final normalized = normalizeJapaneseCutletText(text);
  final words = _analyze(normalized);
  final grouped = _group(words);
  final rendered = <_RenderedCutletToken>[];

  for (var index = 0; index < grouped.length; index++) {
    final word = grouped[index];
    final romanized = _romanizeWord(word, index);
    final token = _RenderedCutletToken(romanized);
    final previous = rendered.isEmpty ? null : rendered.last;
    // Python's `needle in haystack` is substring membership, including the
    // observable quirk that the empty string is contained in every string.
    if (_openingSurfaces.contains(word.surface) ||
        _openingRomanizations.contains(romanized)) {
      if (previous != null) {
        previous.space = true;
      }
    } else if (_closingSurfaces.contains(word.surface) ||
        _closingRomanizations.contains(romanized)) {
      if (previous != null) {
        previous.space = false;
      }
      token.space = true;
    } else if (romanized == ' ') {
      token.space = false;
    } else {
      token.space = true;
    }
    rendered.add(token);
  }

  final output = StringBuffer();
  for (final token in rendered) {
    output.write(token.surface.replaceAll('っ', ''));
    if (token.space) {
      output.write(' ');
    }
  }
  var phonemes = _collapsePythonWhitespace(
    output.toString(),
  ).replaceAll('(', '«').replaceAll(')', '»');
  phonemes = _removeSokuonSpaces(phonemes);
  return G2pResult(phonemes: phonemes, tokens: null);
}