getCJKRatio static method

double getCJKRatio(
  1. String text
)

Get the dominant script type for a text span Returns the ratio of CJK characters in the text

Implementation

static double getCJKRatio(String text) {
  if (text.isEmpty) return 0.0;

  int cjkCount = 0;
  int totalCount = 0;

  for (int i = 0; i < text.length; i++) {
    int codePoint = text.codeUnitAt(i);
    // Handle surrogate pairs
    if (codePoint >= 0xD800 && codePoint <= 0xDBFF && i + 1 < text.length) {
      int low = text.codeUnitAt(i + 1);
      if (low >= 0xDC00 && low <= 0xDFFF) {
        codePoint = 0x10000 + ((codePoint - 0xD800) << 10) + (low - 0xDC00);
        i++; // Skip the low surrogate
      }
    }

    // Skip whitespace and punctuation for ratio calculation
    if (codePoint == 0x20 || codePoint == 0x09 || codePoint == 0x0A || codePoint == 0x0D) {
      continue;
    }

    totalCount++;
    if (isCJKCharacter(codePoint)) {
      cjkCount++;
    }
  }

  return totalCount > 0 ? cjkCount / totalCount.toDouble() : 0.0;
}