Example #1
0
    def fromYaml(yamlData):
        entireText, esdcsYaml = yamlData['command']
        try:
            yamlGroundings = yamlData.get('objectGroundings')
            if yamlGroundings != None:
                g = [
                    groundings.fromYaml(grounding)
                    for grounding in yamlGroundings
                ]
            else:
                g = None

            groundingIsCorrect = yamlData.get('groundingIsCorrect')
            source = yamlData.get("source")

            if 'context' in yamlData:
                context = Context.fromYaml(yamlData['context'])
            else:
                context = None
            return Annotation(
                yamlData['assignmentId'],
                esdcIo.fromYaml(entireText, esdcsYaml,
                                use_ids=True), g, groundingIsCorrect,
                groundings.PhysicalObject.fromYaml(yamlData.get('agent')),
                context, source)

        except:
            print "couldn't make esdcs", entireText
            raise
Example #2
0
    def testAnnotationsWriter(self):

        
        annotations = yamlReader.load("data/forklift_open_ended.yaml")
        print annotations
        print annotations[0]
        entireText, yamlData = esdcIo.toYaml(annotations[0].esdcs)
        self.assertEqual(entireText, "Forklift stop.")
        self.assertEqual(yamlData, [{'EVENT': {'r': 'stop', 'f': 'Forklift'}}])

        entireText, yamlData = esdcIo.toYaml(annotations[1].esdcs)
        self.assertEqual(entireText, "to the truck")
        self.assertEqual(yamlData, [{'PATH': {'r': 'to', 'l': 'the truck'}}])

        entireText, yamlData = esdcIo.toYaml(annotations[2].esdcs)
        self.assertEqual(entireText, "Go between the truck and the pallet.")
        self.assertEqual(yamlData, [{'EVENT': {'r': 'Go',
                                              'l': {'PATH':{'r':'between',
                                                            'l': [{'OBJECT':{'f':'the truck'}},
                                                                  {'OBJECT':{'f':'the pallet'}}]}}}}])
        
        
        for annotatedSentence in annotations:
            entireText, yamlData = esdcIo.toYaml(annotatedSentence.esdcs)
            try:
                rereadAnnotations = esdcIo.fromYaml(entireText, yamlData)
                self.assertEqual(rereadAnnotations, annotatedSentence.esdcs)
            except:
                print "starting esdcs", [e.asPrettyMap() for e in annotatedSentence.esdcs]
                print "text", entireText
                print "data", yamlData
                raise
Example #3
0
def make_esdc_candidate(esdc_structure, esdc_field_to_texts):
    field_map = {}
    entire_text = ""
    for field in ExtendedSdc.fieldNames:
        if not esdc_structure.childIsEmpty(field):
            text = random.choice(esdc_field_to_texts[field])
            entire_text += text + " "
            start = len(entire_text) - len(text) - 1
            end = start + len(text)
            field_map[field] = (start, end)
    field_standoffs = dict((f, [[entire_text.__getslice__(*r), r]])
                           for f, r in field_map.iteritems())
    tokenizer = IndexedTokenizer()
    for field, standoffs in field_standoffs.iteritems():
        results = []
        for text, range_tuple in standoffs:
            start = range_tuple[0]
            indexes, tokens = tokenizer.tokenize(text)

            for idx, word in zip(indexes, tokens):
                range_tuple = [start + idx, start + idx + len(word)]
                results.append([word, range_tuple])
        field_standoffs[field] = results

    esdc_candidate = esdcIo.fromYaml(entire_text,
                                     {esdc_structure.type: field_standoffs})
    return esdc_candidate
Example #4
0
    def testAllObjectArguments(self):
        esdcGroup = fromYaml(
            "the pallets on the right of you",
            yaml.load("""
        - OBJECT:
            f: the pallets
            r: 'on'
            l:
              OBJECT:
                f: the right
                r: of
                l: you
        """))
        esdc = esdcGroup[0]

        self.assertEqual(all_object_arguments(esdc), True)
        self.assertEqual(all_object_arguments(esdc.f[0]), True)
        self.assertEqual(all_object_arguments(esdc.l[0]), True)

        esdcGroup = fromYaml(
            "Pick up the pallet",
            yaml.load("""
        - EVENT:
            r: Pick up
            l: the pallet
        """))
        esdc = esdcGroup[0]

        self.assertEqual(all_object_arguments(esdc), True)
        self.assertEqual(all_object_arguments(esdc.l[0]), True)

        esdcGroup = fromYaml(
            "Go to the pallet",
            yaml.load("""
        - EVENT:
            r: Go 
            l: 
              PATH:
                r:  to
                l:  the pallet
        """))
        esdc = esdcGroup[0]

        self.assertEqual(all_object_arguments(esdc), False)
        self.assertEqual(all_object_arguments(esdc.l[0]), True)
Example #5
0
    def testIsEmptyObjectEsdc(self):
        from esdcs import esdcIo
        esdcs = esdcIo.fromYaml("to the truck",
                                {"PATH": {
                                    "r": "to",
                                    "l": "the truck"
                                }})

        self.assertEqual(esdcs[0].l[0].isLeafObject(), True)
        self.assertEqual(esdcs[0].isLeafObject(), False)
Example #6
0
    def testNestedRepeatedStrings(self):
        from esdcs.dataStructures import ExtendedSdc, ExtendedSdcGroup
        from standoff import TextStandoff
        txt = "Move to the right side of the trailer of the trailer on the right and wait."

        esdcs = [ExtendedSdc('EVENT', r=[TextStandoff("Move to the right side of the trailer of the trailer on the right and wait.", (0, 4))],l2=[],l=[ExtendedSdc('PATH', r=[TextStandoff("Move to the right side of the trailer of the trailer on the right and wait.", (5, 7))],l2=[],l=[ExtendedSdc('OBJECT', r=[TextStandoff("Move to the right side of the trailer of the trailer on the right and wait.", (23, 25))],l2=[ExtendedSdc('OBJECT', r=[],l2=[],l=[],f=[TextStandoff("Move to the right side of the trailer of the trailer on the right and wait.", (41, 44)), TextStandoff("Move to the right side of the trailer of the trailer on the right and wait.", (45, 52))])],l=[ExtendedSdc('OBJECT', r=[],l2=[],l=[],f=[TextStandoff("Move to the right side of the trailer of the trailer on the right and wait.", (26, 29)), TextStandoff("Move to the right side of the trailer of the trailer on the right and wait.", (30, 37))])],f=[ExtendedSdc('OBJECT', r=[],l2=[],l=[],f=[TextStandoff("Move to the right side of the trailer of the trailer on the right and wait.", (8, 11)), TextStandoff("Move to the right side of the trailer of the trailer on the right and wait.", (12, 17)), TextStandoff("Move to the right side of the trailer of the trailer on the right and wait.", (18, 22))])])],f=[])],f=[ExtendedSdc('OBJECT', r=[],l2=[],l=[],f=[], entireText='Move to the right side of the trailer of the trailer on the right and wait.')]), ExtendedSdc('OBJECT', r=[TextStandoff("Move to the right side of the trailer of the trailer on the right and wait.", (53, 55))],l2=[],l=[ExtendedSdc('OBJECT', r=[],l2=[],l=[],f=[TextStandoff("Move to the right side of the trailer of the trailer on the right and wait.", (56, 59)), TextStandoff("Move to the right side of the trailer of the trailer on the right and wait.", (60, 65))])],f=[ExtendedSdc('OBJECT', r=[],l2=[],l=[],f=[TextStandoff("Move to the right side of the trailer of the trailer on the right and wait.", (41, 44)), TextStandoff("Move to the right side of the trailer of the trailer on the right and wait.", (45, 52))])])]

        entireText, yamlData = esdcIo.toYaml(ExtendedSdcGroup(esdcs))
        rereadAnnotations = esdcIo.fromYaml(entireText, yamlData)
        try:
            self.assertEqual(list(rereadAnnotations), esdcs)
        except:
            print "start with", [e.asPrettyMap() for e in esdcs]
            print "ended with", [e.asPrettyMap() for e in rereadAnnotations]
            raise
Example #7
0
    def testCandidateGroup(self):
        esdcGroup = fromYaml(
            "the pallets on the right of you",
            yaml.load("""
        - OBJECT:
            f: the pallets
            r: 'on'
            l:
              OBJECT:
                f: the right
                r: of
                l: you
        """))

        candidate_esdc_groups = candidates.makeCandidatesForEsdcGroup(
            esdcGroup)
        for esdc_group in candidate_esdc_groups:
            self.assertEqual(len(esdc_group), 1)

        self.assertEqual(len(candidate_esdc_groups), 2)
Example #8
0
    def testCandidates(self):
        esdcGroup = fromYaml(
            "the pallets on the right of you",
            yaml.load("""
        - OBJECT:
            f: the pallets
            r: 'on'
            l:
              OBJECT:
                f: the right
                r: of
                l: you
        """))
        esdc = esdcGroup[0]
        candidate_esdcs = candidates.makeCandidatesForEsdc(esdc)

        print "candidates"
        for cesdc in candidate_esdcs:
            print cesdc

        self.assertTrue(esdc in candidate_esdcs)
        self.assertEqual(len(candidate_esdcs), 2)