splitToSyllables<T> static method

List<T> splitToSyllables<T>(
  1. String value, {
  2. bool removePunctuation = true,
})

Implementation

static List<T> splitToSyllables<T>(
  String value, {
  bool removePunctuation = true,
}) {
  assert(
    T == String || T == SyllableData,
    'T can only be a String or a SyllableData',
  );
  value = value.replaceAll('\'', '');
  var simplified = simplifyPinyin(value);

  /// тут надо вставить предопределенные пробелы, чтобы
  /// избежать случаев, когда, анприметр nine делится не как
  /// ni ne, а как nin e
  for (var kv in _splittableExceptions.entries) {
    if (simplified.contains(kv.key)) {
      int indexOfSpace = kv.value.indexOf(' ');
      int indexOfFoundValue = simplified.indexOf(kv.key);
      final presplit = value.split('');
      presplit.insert(indexOfFoundValue + indexOfSpace, ' ');
      value = presplit.join();
      return splitToSyllables(value);
    }
  }

  if (value.contains(_spaceRegex)) {
    final list = value.split(_spaceRegex);
    final res = <T>[];
    for (var subsentence in list) {
      final results = splitToSyllables<T>(subsentence);
      res.addAll(results);
    }
    return res;
  }
  List<_Sentence> allPossibleSentences = [];
  _findSentenceWithBiggerScore(
    value,
    allPossibleSentences: allPossibleSentences,
  );
  // print(allPossibleSentences);
  if (allPossibleSentences.isNotEmpty) {
    _Sentence? sentence;
    // print(allPossibleSentences);
    if (allPossibleSentences.length > 1) {
      for (var s in allPossibleSentences) {
        s.toCorrectSequence(true);
      }
      allPossibleSentences.sort(
        (a, b) => a.totalUseRank.compareTo(b.totalUseRank),
      );
    }
    sentence = allPossibleSentences.first;
    final correctSequence = sentence.toCorrectSequence();
    if (T == String) {
      return correctSequence.map((e) => e.value).toList() as List<T>;
    }
    return correctSequence as List<T>;
  }

  return [];
}