preprocessSentence function

Vector preprocessSentence(
  1. String sentence,
  2. Map<String, int> vocabulary,
  3. int maxLength
)

Implementation

Vector preprocessSentence(String sentence, Map<String, int> vocabulary, int maxLength) {
  String cleaned = sentence.toLowerCase().replaceAll(RegExp(r'[^\w\s]+'), '');
  List<double> tokens = [];
  for (String word in cleaned.split(' ')) {
    if (vocabulary.containsKey(word)) {
      tokens.add(vocabulary[word]!.toDouble());
    }
  }
  while (tokens.length < maxLength) {
    tokens.add(0.0);
  }
  return tokens.sublist(0, maxLength);
}