predictNextWords method

Future<List<FutoWordScore>> predictNextWords(
  1. List<String> previousWords,
  2. int count
)

Highest-scoring exact-vocabulary words after previousWords.

Implementation

Future<List<FutoWordScore>> predictNextWords(
  List<String> previousWords,
  int count,
) async {
  final state = await _contextStateFor(previousWords);
  final embeddings = _embeddings.exactEmbeddings;
  final biases = _embeddings.exactBiases;
  final best = <FutoWordScore>[];
  for (var id = 1; id < futoContextVocabularySize; id++) {
    var score = biases[id].toDouble();
    final row = id * futoContextDimension;
    for (var d = 0; d < futoContextDimension; d++) {
      score += state[d] * embeddings[row + d];
    }
    if (best.length == count && score <= best.last.score) continue;
    var index = best.length;
    while (index > 0 && best[index - 1].score < score) {
      index--;
    }
    best.insert(index, FutoWordScore(_vocabulary[id - 1], score));
    if (best.length > count) best.removeLast();
  }
  return List<FutoWordScore>.unmodifiable(best);
}