calculateF1Score static method

double calculateF1Score(
  1. List<String> groundTruth,
  2. List<String> response
)

Computes the F1 score between groundTruth and response token lists.

Implementation

static double calculateF1Score(
    List<String> groundTruth, List<String> response) {
  if (groundTruth.isEmpty) throw ArgumentError('groundTruth cannot be empty.');
  if (response.isEmpty) throw ArgumentError('response cannot be empty.');

  final refCounts = MatchCounter<String>(groundTruth);
  final predCounts = MatchCounter<String>(response);
  final common = predCounts.intersect(refCounts);
  final numCommon = common.sum();

  if (numCommon == 0) return 0.0;

  final precision = numCommon / response.length;
  final recall = numCommon / groundTruth.length;
  return (2.0 * precision * recall) / (precision + recall);
}