sentenceBLEU static method

double sentenceBLEU(
  1. List<List<String>> references,
  2. List<String> hypothesis, {
  3. List<double>? weights,
  4. List<double> smoothingFunction(
    1. List<RationalNumber>,
    2. int
    )?,
})

Computes the sentence-level BLEU score.

Implementation

static double sentenceBLEU(
  List<List<String>> references,
  List<String> hypothesis, {
  List<double>? weights,
  List<double> Function(List<RationalNumber>, int)? smoothingFunction,
}) {
  if (references.isEmpty) throw ArgumentError('references cannot be empty.');
  if (hypothesis.isEmpty) throw ArgumentError('hypothesis cannot be empty.');

  final w = weights ?? defaultBLEUWeights;
  if (w.isEmpty) throw ArgumentError('weights cannot be empty.');

  final precisions = <RationalNumber>[];
  for (var i = 0; i < w.length; i++) {
    final prec =
        modifiedPrecision(references, hypothesis, n: i + 1);
    if (i == 0 && prec.numerator == 0) return 0.0;
    precisions.add(prec);
  }

  final hypLen = hypothesis.length;
  final bp = brevityPenalty(closestRefLength(references, hypLen), hypLen);
  final smooth = (smoothingFunction ?? SmoothingFunction.method0)(
      precisions, hypLen);

  var score = 0.0;
  for (var i = 0; i < w.length; i++) {
    if (smooth[i] > 0) score += w[i] * math.log(smooth[i]);
  }
  return bp * math.exp(score);
}