def test_to_indexed_instance_converts_correctly(self):
     instance = QuestionAnswerInstance("a A b", ["d", "e f D"], 1)
     data_indexer = DataIndexer()
     a_index = data_indexer.add_word_to_index("a")
     d_index = data_indexer.add_word_to_index("d")
     oov_index = data_indexer.get_word_index(data_indexer._oov_token)  # pylint: disable=protected-access
     indexed_instance = instance.to_indexed_instance(data_indexer)
     assert indexed_instance.question_indices == [a_index, a_index, oov_index]
     assert len(indexed_instance.option_indices) == 2
     assert indexed_instance.option_indices[0] == [d_index]
     assert indexed_instance.option_indices[1] == [oov_index, oov_index, d_index]
示例#2
0
 def read_instance_message(self, instance_message):
     # pylint: disable=redefined-variable-type
     instance_type = instance_message.type
     if instance_type == message_pb2.TRUE_FALSE:
         text = instance_message.question
         instance = TrueFalseInstance(text, None, None)
     elif instance_type == message_pb2.MULTIPLE_TRUE_FALSE:
         options = []
         for instance in instance_message.contained_instances:
             options.append(self.read_instance_message(instance))
         instance = MultipleTrueFalseInstance(options)
     elif instance_type == message_pb2.QUESTION_ANSWER:
         question = instance_message.question
         options = instance_message.answer_options
         instance = QuestionAnswerInstance(question, options, None, None)
     elif instance_type == message_pb2.CHARACTER_SPAN:
         question = instance_message.question
         passage = instance_message.passage
         instance = CharacterSpanInstance(question, passage, None, None)
     else:
         raise RuntimeError("Unrecognized instance type: " + instance_type)
     if instance_message.background_instances:
         background = instance_message.background_instances
         background_instances = [
             self.read_instance_message(instance) for instance in background
         ]
         instance = BackgroundInstance(instance, background_instances)
     return instance
 def test_read_from_line_handles_three_column(self):
     question = "what is the answer"
     answers = ['a', 'b', 'c']
     label = 1
     line = self.instance_to_line(question, answers, label)
     instance = QuestionAnswerInstance.read_from_line(line)
     assert instance.question_text == question
     assert instance.answer_options == answers
     assert instance.label is label
     assert instance.index is None
 def test_words_includes_question_and_answers(self):
     instance = QuestionAnswerInstance("a b c", ["d", "e f"], 1)
     assert instance.words() == {'words': ['a', 'b', 'c', 'd', 'e', 'f']}