(input_file, is_training)
return examples
Read a SQuAD json file into a list of SquadExample.
Read a SQuAD json file into a list of SquadExample.
Read a SQuAD json file into a list of SquadExample .
def read_squad_examples(input_file, is_training):
  """Read a SQuAD json file into a list of SquadExample."""
  with tf.gfile.Open(input_file, "r") as reader:
    input_data = json.load(reader)["data"]

  examples = []
  for entry in input_data:
    for paragraph in entry["paragraphs"]:
      paragraph_text = paragraph["context"]

      for qa in paragraph["qas"]:
        qas_id = qa["id"]
        question_text = qa["question"]
        start_position = None
        orig_answer_text = None
        is_impossible = False

        if is_training:
          if "is_impossible" in qa:
            is_impossible = qa["is_impossible"]
          else:
            is_impossible = False
          if (len(qa["answers"]) != 1) and (not is_impossible):
            raise ValueError(
                "For training, each question should have exactly 1 answer.")
          if not is_impossible:
            answer = qa["answers"][0]
            orig_answer_text = answer["text"]
            start_position = answer["answer_start"]
          else:
            start_position = -1
            orig_answer_text = ""

        example = SquadExample(
            qas_id=qas_id,
            question_text=question_text,
            paragraph_text=paragraph_text,
            orig_answer_text=orig_answer_text,
            start_position=start_position,
            is_impossible=is_impossible)
        examples.append(example)

  return examples
def read_squad_examples ( input_file , is_training ) : with tf . gfile . Open ( input_file , "r" ) as reader : input_data = json . load ( reader ) [ "data" ] examples = [ ] for entry in input_data : for paragraph in entry [ "paragraphs" ] : paragraph_text = paragraph [ "context" ] for qa in paragraph [ "qas" ] : qas_id = qa [ "id" ] question_text = qa [ "question" ] start_position = None orig_answer_text = None is_impossible = False if is_training : if "is_impossible" in qa : is_impossible = qa [ "is_impossible" ] else : is_impossible = False if ( len ( qa [ "answers" ] ) != 1 ) and ( not is_impossible ) : raise ValueError ( "For training, each question should have exactly 1 answer." ) if not is_impossible : answer = qa [ "answers" ] [ 0 ] orig_answer_text = answer [ "text" ] start_position = answer [ "answer_start" ] else : start_position = - 1 orig_answer_text = "" example = SquadExample ( qas_id = qas_id , question_text = question_text , paragraph_text = paragraph_text , orig_answer_text = orig_answer_text , start_position = start_position , is_impossible = is_impossible ) examples . append ( example ) return examples