def calc_exact_match(test_examples,
                     predictions,
                     is_print_error = False):
  """Calculates exact match for a test set and its corresponding predictions."""

  correct = 0
  incorrect = 0

  for test_example, prediction in zip(test_examples, predictions):
    utterance = test_example.utterance
    gold_program = test_example.program

    if prediction is not None and prediction.lower() == gold_program.lower():
      correct += 1
    else:
      incorrect += 1
      if is_print_error:
        print("Incorrect: %s.\nTarg: %s\nPred: %s" %
              (utterance, gold_program, prediction))

  em = float(correct) / float(correct + incorrect)
  print("correct: %s" % correct)
  print("incorrect: %s" % incorrect)
  print("EM: %s" % em)
  return em