import json


def _get_example(line):
  """Extract relevant fields from json.

  Args:
    line: String for json line.

  Returns:
    example_id: integer.
    question: string.
    contexts: List of strings.
    context_indices: List of (int32, int32) tuples.
  """
  json_example = json.loads(line)
  example_id = json_example["example_id"]
  long_answer_candidates = json_example["long_answer_candidates"]

  contexts = []
  context_indices = []
  for candidate in long_answer_candidates:
    if candidate["top_level"]:
      # Get candidate start and end indices.
      start_index = candidate["start_token"]
      end_index = candidate["end_token"]
      context_indices.append((start_index, end_index))

      # Get candidate contexts.
      candidate_tokens = json_example["document_tokens"][start_index:end_index]
      candidate_tokens = [t["token"] for t in candidate_tokens]
      candidate_str = u" ".join(candidate_tokens)
      candidate_str = candidate_str.lower()
      contexts.append(candidate_str)

  # Get question.
  question = u" ".join(json_example["question_tokens"])
  question = question.lower()

  return example_id, question, contexts, context_indices