def _utterance_to_one_best_sql_map(predictions_dicts):
  """Get map of utterance to best prediction."""
  # Assume sorted in descending order by score.
  utterance_to_one_best_sql_map = {}
  for prediction in predictions_dicts:
    if not prediction['predictions']:
      print('No predictions for example: %s' % prediction)
      continue

    utterance = prediction['utterance']
    paired_preds_and_scores = zip(prediction['predictions'],
                                  prediction['scores'])
    sorted_by_scores = sorted(
        paired_preds_and_scores, key=lambda x: x[1], reverse=True)

    if FLAGS.use_executable_sql_only:
      empty_path = prediction['empty_database_path']
      try:
        empty_conn = sqlite3.connect(empty_path)
        empty_conn.text_factory = str
      except sqlite3.OperationalError as e:
        print(e)
        print(empty_path)
        exit()

      cursor = empty_conn.cursor()
      best_prediction = None
      for _, (pred, _) in enumerate(sorted_by_scores):
        # Try predicting
        print('Trying to execute query:\n\t' + pred)
        print('... on empty database')
        temp_exception_str = official_evaluation.try_executing_query(
            pred, cursor, case_sensitive=True)[1]

        if temp_exception_str:
          if temp_exception_str == 'timeout':
            # Technically, this query didn't have a syntax problem, so
            # continue and set this as the best prediction.
            best_prediction = pred
            break
        else:
          best_prediction = pred
          break

      one_best_prediction = best_prediction
    else:
      one_best_prediction = sorted_by_scores[0][0]
    utterance_to_one_best_sql_map[utterance] = one_best_prediction
  return utterance_to_one_best_sql_map
Get map of utterance to best prediction.
language/xsp/evaluation/convert_preds_for_spider_eval.py