Ejemplo n.º 1
0
 def testNothing(self):
     input = hypothesis_pb2.IntentHypothesisMetadata()
     input.source_transcript = "balalalal"
     output = []
     self._extractor.Extract(input, output.append)
     output = filter(lambda x: x.metadata.confidence_score > 0.7, output)
     self.assertEqual(len(output), 0)
Ejemplo n.º 2
0
 def testNotFamiliarNameFail(self):
     input = hypothesis_pb2.IntentHypothesisMetadata()
     input.source_transcript = "Ba namy isy Xheye"
     output = []
     self._extractor.Extract(input, output.append)
     output = filter(lambda x: x.metadata.confidence_score > 0.3, output)
     self.assertEqual(len(output), 0)
Ejemplo n.º 3
0
 def testDontKnow(self):
     input = hypothesis_pb2.IntentHypothesisMetadata()
     input.source_transcript = "Can you say that again"
     output = []
     self._say_again_extractor.Extract(input, output.append)
     self.assertGreater(len(output), 0)
     for result in output:
         self.assertTrue(result.intent.HasField("say_again"))
Ejemplo n.º 4
0
 def testWantTour(self):
   input = hypothesis_pb2.IntentHypothesisMetadata()
   input.source_transcript = "Can you please show us around"
   output = []
   self._extractor.Extract(input, output.append)
   output = filter(lambda x: x.metadata.confidence_score > 0.7, output)
   output = filter(lambda x: x.intent.HasField("want_tour"), output)
   self.assertGreater(len(output), 0)
Ejemplo n.º 5
0
 def testWave(self):
     input = hypothesis_pb2.IntentHypothesisMetadata()
     input.source_transcript = "please wave"
     output = []
     self._extractor.Extract(input, output.append)
     self.assertGreater(len(output), 0)
     for result in output:
         self.assertTrue(result.intent.HasField("want_wave"))
Ejemplo n.º 6
0
 def testTwo(self):
     input = hypothesis_pb2.IntentHypothesisMetadata()
     input.source_transcript = "two"
     output = []
     self._number_extractor.Extract(input, output.append)
     self.assertGreater(len(output), 0)
     for result in output:
         self.assertEqual(result.intent.number.number, 2)
Ejemplo n.º 7
0
 def testDontKnow(self):
     input = hypothesis_pb2.IntentHypothesisMetadata()
     input.source_transcript = "I dont know"
     output = []
     self._dont_know_extractor.Extract(input, output.append)
     self.assertGreater(len(output), 0)
     for result in output:
         self.assertTrue(result.intent.HasField("dont_know"))
Ejemplo n.º 8
0
 def testNo(self):
     input = hypothesis_pb2.IntentHypothesisMetadata()
     input.source_transcript = "No"
     output = []
     self._yes_no_extractor.Extract(input, output.append)
     self.assertGreater(len(output), 0)
     for result in output:
         self.assertTrue(result.intent.yes_no.is_no)
         self.assertFalse(result.intent.yes_no.is_yes)
Ejemplo n.º 9
0
 def testHenrikOffice(self):
   input = hypothesis_pb2.IntentHypothesisMetadata()
   input.source_transcript = "I am visiting Henriks Office"
   output = []
   self._extractor.Extract(input, output.append)
   output = filter(lambda x: x.metadata.confidence_score > 0.7, output)
   output = filter(
       lambda x: x.intent.want_to_go_location.location_id == "henrik", output)
   self.assertGreater(len(output), 0)
Ejemplo n.º 10
0
 def testMaybe(self):
     input = hypothesis_pb2.IntentHypothesisMetadata()
     input.source_transcript = "Maybe"
     output = []
     self._yes_no_extractor.Extract(input, output.append)
     self.assertGreater(len(output), 0)
     for result in output:
         self.assertTrue(result.intent.HasField("maybe"))
         self.assertFalse(result.intent.HasField("yes_no"))
Ejemplo n.º 11
0
 def testBad(self):
   input = hypothesis_pb2.IntentHypothesisMetadata()
   input.source_transcript = "Could use some beer"
   output = []
   self._how_are_you_extractor.Extract(input, output.append)
   self.assertGreater(len(output), 0)
   for result in output:
     self.assertTrue(result.intent.how_are_you_response.is_negative)
     self.assertFalse(result.intent.how_are_you_response.is_positive)
Ejemplo n.º 12
0
 def testMyNameIs(self):
     input = hypothesis_pb2.IntentHypothesisMetadata()
     input.source_transcript = "My name is Shengye"
     output = []
     self._extractor.Extract(input, output.append)
     output = filter(lambda x: x.intent.HasField("human_name"), output)
     self.assertGreater(len(output), 0)
     for result in output:
         self.assertEqual(result.intent.human_name.name.lower(), "shengye")
Ejemplo n.º 13
0
 def testOne(self):
     input = hypothesis_pb2.IntentHypothesisMetadata()
     input.source_transcript = "one"
     output = []
     self._extractor.Extract(input, output.append)
     output = filter(lambda x: x.intent.HasField("number"), output)
     self.assertGreater(len(output), 0)
     for result in output:
         self.assertEqual(result.intent.number.number, 1)
Ejemplo n.º 14
0
 def testGeneralWords(self):
     input = hypothesis_pb2.IntentHypothesisMetadata()
     input.source_transcript = "Night gathers, and now my watch begins."
     output = []
     self._extractor.Extract(input, output.append)
     self.assertEqual(len(output), 1)
     for result in output:
         self.assertTrue(result.intent.HasField("general_text"))
         self.assertEqual(result.metadata.confidence_score, 0.7)
Ejemplo n.º 15
0
 def testNameTwoWords(self):
     input = hypothesis_pb2.IntentHypothesisMetadata()
     input.source_transcript = "Shengye Wang"
     output = []
     self._extractor.Extract(input, output.append)
     self.assertGreater(len(output), 0)
     for result in output:
         self.assertTrue(result.intent.HasField("human_name"))
         self.assertEqual(result.intent.human_name.name.lower(),
                          "shengye wang")
Ejemplo n.º 16
0
 def testFamiliarName(self):
     input = hypothesis_pb2.IntentHypothesisMetadata()
     input.source_transcript = "Ba namy isy Shengye"
     output = []
     self._extractor.Extract(input, output.append)
     output = filter(lambda x: x.metadata.confidence_score > 0.3, output)
     self.assertGreater(len(output), 0)
     for result in output:
         self.assertTrue(result.intent.HasField("human_name"))
         self.assertEqual(result.intent.human_name.name.lower(), "shengye")
Ejemplo n.º 17
0
 def testWrongAnswerHotAir(self):
     input = hypothesis_pb2.IntentHypothesisMetadata()
     input.source_transcript = "heavier"
     output = []
     self._extractor.Extract(input, output.append)
     output = filter(lambda x: x.metadata.confidence_score > 0.7, output)
     output = filter(
         lambda x: x.intent.quiz_answer.matched_wrong_answer == "hot_air",
         output)
     self.assertGreater(len(output), 0)
Ejemplo n.º 18
0
 def testWhiteHouse(self):
     input = hypothesis_pb2.IntentHypothesisMetadata()
     input.source_transcript = "white house"
     output = []
     self._extractor.Extract(input, output.append)
     output = filter(lambda x: x.metadata.confidence_score > 0.7, output)
     output = filter(
         lambda x: x.intent.quiz_answer.matched_answer == "white_house",
         output)
     self.assertGreater(len(output), 0)
 def testPresidentQuestion(self):
     input = hypothesis_pb2.IntentHypothesisMetadata()
     input.source_transcript = "who is the second president of United States?"
     output = []
     self._extractor.Extract(input, output.append)
     self.assertGreater(len(output), 0)
     for result in output:
         self.assertTrue(result.intent.HasField("president_question"))
         self.assertEqual(result.intent.president_question.president_number,
                          2)
Ejemplo n.º 20
0
 def testVeryLongWords(self):
     input = hypothesis_pb2.IntentHypothesisMetadata()
     input.source_transcript = (
         "Night gathers, and now my watch begins. It shall not end until my "
         "death. I shall take no wife, hold no lands, father no children. I "
         "shall wear no crowns and win no glory. I shall live and die at my "
         "post. I am the sword in the darkness. I am the watcher on the walls. "
         "I am the shield that guards the realms of men. I pledge my life and "
         "honor to the Night's Watch, for this night and all the nights to "
         "come.")
     output = []
     self._extractor.Extract(input, output.append)
     self.assertEqual(len(output), 1)
     for result in output:
         self.assertTrue(result.intent.HasField("general_text"))
         self.assertEqual(result.metadata.confidence_score, 1.0)
Ejemplo n.º 21
0
    def SelectImpl(self, input_queue):
        # input_queue is a Queue. This function returns an IntentHypothesis or None.

        start_time = time.time()
        local_input_queue = []
        input_stopped = False
        speaking_stopped = False
        last_received_time = time.time()
        is_silence = True

        while True:
            # Caller would set a deadline, and if reading from input_queue is None,
            # this function must return.

            # Moves everything from input_queue to local_input_queue.
            while True:
                try:
                    recognition_result = input_queue.get(
                        block=True,
                        timeout=FLAGS.
                        timed_best_intent_selector_polling_interval_secs)
                    last_received_time = time.time()
                except Queue.Empty:
                    break

                if recognition_result is None:
                    # This means we have reached the end of the input queue, should try
                    # no more.
                    input_stopped = True
                    break
                else:
                    assert isinstance(recognition_result, RecognitionResult)
                    is_silence = False
                    local_input_queue.append(recognition_result)
                    if time.time() - start_time > self._init_wait_time:
                        break

            time_since_start = time.time() - start_time

            if not input_stopped and time_since_start < self._init_wait_time:
                continue
            elif (self._blank_timeout > 0
                  and time.time() - last_received_time > self._blank_timeout):
                # This means we have been waiting too long, the human should have
                # stopped speaking.
                speaking_stopped = True

            # Starting here, we can compare all the results and select the best one.
            newly_extracted_results = []

            for recognition_result in local_input_queue:

                # Creates a metadata object for the extractors to use as input.
                input_metadata = hypothesis_pb2.IntentHypothesisMetadata()
                input_metadata.source_transcript = recognition_result.source_transcript
                input_metadata.source_confidence_score = (
                    recognition_result.source_confidence_score)
                input_metadata.source_is_complete_utterance = (
                    recognition_result.source_is_complete_utterance)
                input_metadata.source_timestamp.seconds = int(
                    math.floor(recognition_result.source_timestamp))
                input_metadata.source_timestamp.nanos = int(
                    1000000000 * (recognition_result.source_timestamp -
                                  input_metadata.source_timestamp.seconds))

                for extractor in self._extractors:
                    extractor.Extract(input_metadata,
                                      newly_extracted_results.append)

            newly_extracted_results = map(self._map_func,
                                          newly_extracted_results)
            newly_extracted_results = [
                r for r in newly_extracted_results if r is not None
            ]

            newly_extracted_results = [
                r for r in newly_extracted_results
                if (r.intent.WhichOneof("intent") in self._intent_types and
                    r.metadata.confidence_score > self._confidence_threshold)
            ]

            if len(newly_extracted_results):
                return max(newly_extracted_results,
                           key=lambda x: x.metadata.confidence_score)
            else:
                if input_stopped or speaking_stopped:
                    result = hypothesis_pb2.IntentHypothesis()
                    result.metadata.extractor_name = self.__class__.__name__
                    current_time = time.time()
                    result.metadata.source_timestamp.seconds = int(
                        math.floor(current_time))
                    result.metadata.source_timestamp.nanos = int(
                        1000000000 *
                        (current_time -
                         result.metadata.source_timestamp.seconds))
                    # Only set resutl.intent.intent when silence, if none of the extractor
                    # are able to find out anything, not to set result.intent so
                    # "WhichOneof" would return None, and we can tell it is unknown.
                    if is_silence:
                        result.intent.silence.SetInParent()
                    return result
                else:
                    # Clears the local_input_queue. The previously received results are
                    # all useless now.
                    local_input_queue = []