def compute_extractive_predictions_using_results_only(result, span_encoding,
                                                      n_best_size,
                                                      max_answer_length):
  """Computes final extractive predictions based on logits."""
  prelim_predictions = []
  start_logits = [float(x) for x in result["start_logits"].flat]
  end_logits = [float(x) for x in result["end_logits"].flat]
  answer_type_logits = [float(x) for x in result["answer_type_logits"].flat]
  answer_type_index = AnswerType.EXTRACTIVE
  answer_type_logit = answer_type_logits[answer_type_index]

  if span_encoding == "independent":
    starts = get_best_indexes(start_logits, 50)
    ends = get_best_indexes(end_logits, 50)
    starts_ends = list(itertools.product(starts, ends))
    start_indexes, end_indexes = zip(*starts_ends)
  elif span_encoding == "concat-mlp":
    start_indexes, end_indexes = _get_best_joint_indexes(
        max_answer_length, start_logits, 50)
  else:
    raise ValueError("Unknown span_encoding: %s" % span_encoding)
  for start_index, end_index in zip(start_indexes, end_indexes):
    if start_index == 0 or end_index == 0:
      continue

    if end_index < start_index:
      continue
    length = end_index - start_index + 1
    if length > max_answer_length:
      continue

    if span_encoding == "independent":
      start_logit = start_logits[start_index]
      end_logit = end_logits[end_index]
    elif span_encoding == "concat-mlp":
      start_logit = start_logits[start_index * (max_answer_length - 1) +
                                 end_index]
      end_logit = 0
    else:
      raise ValueError("Unknown span_encoding: %s" % span_encoding)
    prelim_predictions.append(
        RawPrediction(
            start_index=start_index,
            end_index=end_index,
            answer_type_index=answer_type_index,
            start_logit=start_logit,
            end_logit=end_logit,
            answer_type_logit=answer_type_logit))

  prelim_predictions = sorted(
      prelim_predictions,
      # First sorts by the answer type classification, then uses the span
      # extent logits as the secondary key.
      key=lambda x: (x.answer_type_logit, x.start_logit + x.end_logit),
      reverse=True)

  nbest = prelim_predictions[:n_best_size]

  # In very rare edge cases we could have no valid predictions. So we
  # just create a nonce prediction in this case to avoid failure.
  if not nbest:
    nbest.append(
        RawPrediction(
            start_index=0,
            end_index=0,
            answer_type_index=AnswerType.UNKNOWN,
            start_logit=0,
            end_logit=0,
            answer_type_logit=0))

  assert len(nbest) >= 1
  return nbest
Computes final extractive predictions based on logits.
language/capwap/synthetic/generate_answers.py