extractFeaturesFromBytes function

ModelFeatures extractFeaturesFromBytes(
  1. Uint8List content, {
  2. int paddingToken = 256,
  3. int begSize = 512,
  4. int midSize = 512,
  5. int endSize = 512,
})

Implementation

ModelFeatures extractFeaturesFromBytes(
  Uint8List content, {
  int paddingToken = 256,
  int begSize = 512,
  int midSize = 512,
  int endSize = 512,
}) {
  List<int> trimBytes(List<int> bytes) {
    int start = 0;
    int end = bytes.length - 1;

    // Identifying leading white spaces/new lines.
    while (start <= end &&
        (bytes[start] == 32 || bytes[start] == 10 || bytes[start] == 13)) {
      start++;
    }

    // Identifying trailing white spaces/new lines.
    while (end >= start &&
        (bytes[end] == 32 || bytes[end] == 10 || bytes[end] == 13)) {
      end--;
    }

    // If there's nothing to trim, return the original bytes; otherwise, return the trimmed subsection.
    return (start <= end) ? bytes.sublist(start, end + 1) : [];
  }

  content = Uint8List.fromList(trimBytes(content));

  List<int> beg = [];
  List<int> mid = [];
  List<int> end = [];

  if (begSize > 0) {
    if (begSize < content.length) {
      beg = List<int>.filled(begSize, 0);
      for (int i = 0; i < begSize; i++) {
        beg[i] = content[i];
      }
    } else {
      final paddingSize = begSize - content.length;
      beg = [...content, ...List<int>.filled(paddingSize, paddingToken)];
    }
  }
  assert(beg.length == begSize);

  // Middle chunk
  if (midSize > 0) {
    final midIdx = (content.length ~/ 2);
    if (midSize < content.length) {
      final leftIndex = midIdx - (midSize ~/ 2);

      final midSizeIsEven = midSize.isEven;
      final rightIndex = midIdx + (midSize ~/ 2) + (midSizeIsEven ? 0 : 1);
      mid = content.sublist(leftIndex, rightIndex);
    } else {
      final totalPaddingSize = midSize - content.length;
      final leftPaddingSize = totalPaddingSize ~/ 2;
      final paddingIsEven = totalPaddingSize.isEven;
      final rightPaddingSize = leftPaddingSize + (paddingIsEven ? 0 : 1);

      mid = [
        ...List<int>.filled(leftPaddingSize, paddingToken),
        ...content,
        ...List<int>.filled(rightPaddingSize, paddingToken),
      ];
    }
  } else {
    mid = [];
  }
  assert(mid.length == midSize, 'Mid length: ${mid.length}');

  if (endSize > 0) {
    if (endSize < content.length) {
      end = List<int>.filled(endSize, 0);
      for (int i = 0; i < endSize; i++) {
        end[i] = content[content.length - endSize + i];
      }
    } else {
      final paddingSize = endSize - content.length;
      end = [...List<int>.filled(paddingSize, paddingToken), ...content];
    }
  }
  assert(end.length == endSize);

  // Convert lists back to Uint8List
  return ModelFeatures(beg: beg, mid: mid, end: end);
}