encode method

List<int> encode(
  1. String text
)

Encodes a string into a fixed-length integer sequence.

Characters not in the vocabulary map to unknownIndex. Sequences shorter than maxLength are right-padded with 0. Sequences longer than maxLength are truncated.

Implementation

List<int> encode(String text) {
  final encoded = <int>[];

  for (int i = 0; i < text.length && i < _maxLength; i++) {
    encoded.add(_charToIndex[text[i]] ?? _unknownIndex);
  }

  // Pad to maxLength
  while (encoded.length < _maxLength) {
    encoded.add(_padIndex);
  }

  return encoded;
}