termFrequencies function

Map<String, int> termFrequencies(
  1. List<String> tokens
)

Counts how many times each token appears in tokens (term frequency).

Returns an empty map for empty input; counts are raw, not normalized, so callers can divide by total length if they need relative frequency.

Example:

termFrequencies(['cat', 'cat', 'dog']); // {'cat': 2, 'dog': 1}

Audited: 2026-06-12 11:26 EDT

Implementation

Map<String, int> termFrequencies(List<String> tokens) {
  final Map<String, int> tf = <String, int>{};
  for (final String t in tokens) {
    tf[t] = (tf[t] ?? 0) + 1;
  }
  return tf;
}