scoreCorpus method

List<MatchResult> scoreCorpus(
  1. String query,
  2. List<String> corpus, {
  3. int? generation,
})

Score all items in corpus, returning match results.

generation is an optional corpus version; changing it invalidates the cache.

Implementation

List<MatchResult> scoreCorpus(
  String query,
  List<String> corpus, {
  int? generation,
}) {
  if (query.isEmpty) {
    _prevQuery = '';
    _cache = [];
    final results = <MatchResult>[];
    for (final title in corpus) {
      results.add(_scorer.score('', title));
    }
    return results;
  }

  final isExtension =
      _prevQuery.isNotEmpty &&
      query.startsWith(_prevQuery) &&
      query.length > _prevQuery.length &&
      (generation == null || generation == _corpusGeneration) &&
      corpus.length == _corpusLen;

  if (isExtension && _cache.isNotEmpty) {
    // Incremental: only re-score previously matched items
    _incrementalScans++;
    final newCache = <_CachedEntry>[];
    final results = List<MatchResult>.filled(
      corpus.length,
      const MatchResult.noMatch(),
    );

    for (final entry in _cache) {
      final result = _scorer.score(query, corpus[entry.index]);
      results[entry.index] = result;
      if (result.matchType != MatchType.noMatch) {
        newCache.add(_CachedEntry(index: entry.index, result: result));
      }
    }
    _totalEvaluated += _cache.length;
    _cache = newCache;
    _prevQuery = query;
    return results;
  }

  // Full scan
  _fullScans++;
  _cache = [];
  final results = <MatchResult>[];

  for (var i = 0; i < corpus.length; i++) {
    final result = _scorer.score(query, corpus[i]);
    results.add(result);
    if (result.matchType != MatchType.noMatch) {
      _cache.add(_CachedEntry(index: i, result: result));
    }
  }

  _totalEvaluated += corpus.length;
  _prevQuery = query;
  _corpusGeneration = generation ?? _corpusGeneration;
  _corpusLen = corpus.length;
  return results;
}