def top_metrics(targets: List[str],
                predictions: List[str],
                error_on_invalid_target: bool = True) -> Dict[str, float]:
  """Returns eval metrics for TOP and MTOP datasets."""
  num_correct = 0
  num_total = 0
  num_invalid = 0

  num_intent_correct = 0
  num_frame_correct = 0

  for target, predicted in zip(targets, predictions):
    if target == predicted:
      num_correct += 1
    num_total += 1

    target_lf = top_utils.deserialize_top(target)
    predicted_lf = top_utils.deserialize_top(predicted)

    if error_on_invalid_target:
      assert target_lf is not None
    elif target_lf is None:
      continue
    if not predicted_lf:
      num_invalid += 1
      continue

    target_frame = top_utils.get_frame_top(target_lf)
    predicted_frame = top_utils.get_frame_top(predicted_lf)
    target_intent = target_frame.split("-")[0]
    predicted_intent = predicted_frame.split("-")[0]

    num_intent_correct += int(predicted_intent == target_intent)
    num_frame_correct += int(predicted_frame == target_frame)

  return dict(
      num_total=num_total,
      full_accuracy=_safe_divide(num_correct, num_total),
      intent_accuracy=_safe_divide(num_intent_correct, num_total),
      intent_arg_accuracy=_safe_divide(num_frame_correct, num_total),
      invalid_predictions=_safe_divide(num_invalid, num_total))
Returns eval metrics for TOP and MTOP datasets.
language/casper/evaluate/top_metrics.py