def metric_max_over_ground_truths(metric_fn, prediction, ground_truths):
    """
    Maximizes metric function over available gold spans. Because there can be
    multiple legal answers, we do not penalize the model by only testing on
    the first gold span.

    Args:
        metric_fn: Function to maximize over.
        prediction: Predicted answer span (string).
        ground_truths: List of true answer spans (each string).

    Returns:
        Max score.
    """
    scores_for_ground_truths = []
    for ground_truth in ground_truths:
        score = metric_fn(prediction, ground_truth)
        scores_for_ground_truths.append(score)
    return max(scores_for_ground_truths)