def get_first_annotation(json_dict, max_passages):
  """Returns the first minimal or passage answer in the example.

  Returns the annotation with the earliest minimal answer span. If no annotation
  has a minimal answer span, then the annotation with the earliest passage
  answer will be returned.

  Args:
    json_dict: annotated example.
    max_passages: see FLAGS.max_passages.

  Returns:
    annotation: (dict) selected annotation.
    annotated_idx: (int) index of the first annotated candidate.
    annotated_span: (tuple) byte offset of the start and end token of the
      minimal answer. The end token is exclusive. This index is relative to this
      particular passage's plaintext, not the full plaintext.
  """

  if "annotations" not in json_dict:
    return None, -1, (-1, -1)

  positive_annotations = sorted(
      [a for a in json_dict["annotations"] if has_passage_answer(a)],
      key=lambda a: a["passage_answer"]["candidate_index"])

  for a in positive_annotations:
    if a["minimal_answer"]:
      # Check if it is a non null answer.
      start_byte_offset = a["minimal_answer"]["plaintext_start_byte"]
      if start_byte_offset < 0:
        continue

      idx = a["passage_answer"]["candidate_index"]
      if idx >= max_passages:
        continue
      end_byte_offset = a["minimal_answer"]["plaintext_end_byte"]
      return a, idx, (global_to_local_offset(json_dict, idx, start_byte_offset),
                      global_to_local_offset(json_dict, idx, end_byte_offset))

  for a in positive_annotations:
    idx = a["passage_answer"]["candidate_index"]
    if idx >= max_passages:
      continue
    return a, idx, (-1, -1)

  return None, -1, (-1, -1)
Returns the first minimal or passage answer in the example.

  Returns the annotation with the earliest minimal answer span. If no annotation
  has a minimal answer span, then the annotation with the earliest passage
  answer will be returned.

  Args:
    json_dict: annotated example.
    max_passages: see FLAGS.max_passages.

  Returns:
    annotation: (dict) selected annotation.
    annotated_idx: (int) index of the first annotated candidate.
    annotated_span: (tuple) byte offset of the start and end token of the
      minimal answer. The end token is exclusive. This index is relative to this
      particular passage's plaintext, not the full plaintext.
  
language/canine/tydiqa/data.py