Beispiel #1
0
 def inject_context(self, entity, metadata=None):
     """
     Args:
         entity(object):
             format {'data': 'Entity tag as <str>',
                     'key': 'entity proper name as <str>',
                      'confidence': <float>'
                      }
         metadata(object): dict, arbitrary metadata about the entity being
         added
     """
     metadata = metadata or {}
     try:
         if len(self.frame_stack) > 0:
             top_frame = self.frame_stack[0]
         else:
             top_frame = None
         if top_frame and top_frame[0].metadata_matches(metadata):
             top_frame[0].merge_context(entity, metadata)
         else:
             frame = ContextManagerFrame(entities=[entity],
                                         metadata=metadata.copy())
             self.frame_stack.insert(0, (frame, time.time()))
     except (IndexError, KeyError):
         pass
Beispiel #2
0
    def testMergeContext(self):
        frame1 = ContextManagerFrame(entities=['foo'],
                                     metadata={
                                         'domain': 'music',
                                         'foo': 'test'
                                     })

        self.assertFalse(frame1.metadata_matches({'bar': 'test'}),
                         "Should not match before merging context")

        frame1.merge_context('bar', {'domain': 'music', 'bar': 'test'})
        self.assertTrue(frame1.metadata_matches({'domain': 'music'}),
                        "Should continue to match subset of metadata")
        self.assertTrue(frame1.metadata_matches({'bar': 'test'}),
                        "Should match after merging context")
Beispiel #3
0
    def testMetadataMatches(self):
        frame1 = ContextManagerFrame(entities=['foo'],
                                     metadata={
                                         'domain': 'music',
                                         'foo': 'test'
                                     })

        self.assertTrue(frame1.metadata_matches({'domain': 'music'}),
                        "Should match subset of metadata")

        self.assertFalse(frame1.metadata_matches({'domain': 'weather'}),
                         "Should not match metadata value mismatch")
        self.assertTrue(
            frame1.metadata_matches({
                'domain': 'music',
                'foo': 'test'
            }), "Should match exact metadata")
        self.assertFalse(
            frame1.metadata_matches({
                'domain': 'music',
                'foo': 'test',
                'bar': 'test'
            }), "Should not match superset of metadata")