Exemplo n.º 1
0
    def __init__(self, training_ds, cf):
        self.training_ds = training_ds
        self.cf = cf

        t = ""
        self.esdc_structures = [
            ExtendedSdc(esdcType="PATH",
                        entireText=t,
                        r=[TextStandoff(t, (0, 0))],
                        l=[
                            ExtendedSdc(esdcType="OBJECT",
                                        f=[TextStandoff(t, (0, 0))])
                        ]),
            #ExtendedSdc(esdcType="EVENT", entireText=t,
            #            r=[TextStandoff(t, (0, 0))],
            #            l=[TextStandoff(t, (0, 0))],
            #            l2=[TextStandoff(t, (0, 0))])
        ]

        self.esdc_field_to_texts = {}

        groundings = set([])
        for ex in self.training_ds.observations:
            for sdc in ex.sdcs:
                for field in ExtendedSdc.fieldNames:
                    self.esdc_field_to_texts.setdefault(field, [])
                    text = sdc.childText(field)
                    if text != "":
                        self.esdc_field_to_texts[field].append(text)
            if len(groundings) < 10:
                for glist in ex.annotation.groundings:
                    groundings.update(glist)

        self.groundings = list(groundings)
Exemplo n.º 2
0
    def testBreadthFirstTraverseLoop(self):
        cmd = "Move in front of the pallet of boxes in the center and wait."
        esdc = ExtendedSdc(
            'EVENT',
            r=[TextStandoff(cmd, (0, 4)),
               TextStandoff(cmd, (5, 7))],
            l2=[],
            f=[TextStandoff(cmd, (8, 13))])
        esdc.l = [esdc]

        def callback(esdcFromParent):
            print "callback"

        dataStructures.breadthFirstTraverse(esdc, callback)
Exemplo n.º 3
0
def fromYaml(entireText, esdcsYaml, use_ids=False):
    """
    Reads a list of esdcs as yaml (maps, lists, strings, as recovered
    from yaml.load), and makes them into proper ESDCs.

    It makes the following conversions:

    * Landmarks that are strings are converted to OBJECT esdcs
      automatically.

    * It parses tokens with ranges, to build standoff tags.

    * Tokens without ranges are automatically converted to standoffs.
      An error is raised if this cannot be done uniquely.  Then the
      token must be specified with a range.
    """
    esdcsYaml = wrapValueInList(esdcsYaml)
    esdcs = []
    for esdcYaml in esdcsYaml:
        try:
            esdcType, outputDict = handleEsdc(esdcYaml, entireText)
            esdcs.append(ExtendedSdc(esdcType, **outputDict))
        except:
            print "Trouble with", esdcYaml
            print "entire text", entireText
            raise
    objectifyLandmarks(esdcs)
    objectifyFiguresOfEvents(esdcs)
    addEmptyFigures(esdcs)

    return ExtendedSdcGroup(esdcs, entireText, use_ids=use_ids)
Exemplo n.º 4
0
    def testCandidate(self):
        cmd = "turn and move to the truck on the right"
        esdcg = ExtendedSdcGroup([ExtendedSdc('EVENT', r=[TextStandoff(cmd, (0, 4))],l2=[],l=[ExtendedSdc('PATH', r=[TextStandoff(cmd, (14, 16))],l2=[],l=[ExtendedSdc('OBJECT', r=[TextStandoff(cmd, (27, 29))],l2=[],l=[TextStandoff(cmd, (30, 33)), TextStandoff(cmd, (34, 39))],f=[TextStandoff(cmd, (17, 20)), TextStandoff(cmd, (21, 26))])],f=[])],f=[ExtendedSdc('OBJECT', r=[],l2=[],l=[],f=[], entireText=cmd)]), ExtendedSdc('EVENT', r=[TextStandoff(cmd, (9, 13))],l2=[],l=[],f=[ExtendedSdc('OBJECT', r=[],l2=[],l=[],f=[], entireText=cmd)])], cmd)
        

        candidates = esdcCandidates.makeCandidatesForEsdcGroup(esdcg)
        self.assertEqual(len(candidates), 5)
Exemplo n.º 5
0
def recipesToDataset(recipes):
    observations = []
    task_planner = nodeSearch.BeamSearch(CostFnRandom())
    fe = kitchen_features.GGGFeatures()
    for recipe in recipes:
        print "training", recipe.name


        for idx in range(recipe.num_instructions):
            instruction = recipe.idx_to_instruction(idx)
            esdc = ExtendedSdc("EVENT", instruction, 
                               r=TextStandoff(instruction, (0, len(instruction))))
            ggg = ggg_from_esdc(esdc)

            states = recipe.idx_to_states(idx)
            example_id = "%s_%d" % (recipe.name, idx)
            features = fe.features(ggg, ggg.factors[0], states)
            obs = dataset.ContinuousObservation(example_id, True, True, features,
                                                sdcs=[esdc])
            observations.append(obs)
            negative_obs = negativeExamples(task_planner, 
                                            fe,
                                            recipe.idx_to_start_state(idx),
                                            esdc, 
                                            ggg,
                                            example_id, states)
            observations.extend(negative_obs)
            
            
    return dataset.ContinuousDataset(observations)
Exemplo n.º 6
0
def fromRouteDirectionSdc(sdcs):
    from routeDirectionCorpusReader import childrenMap, ancestorMap
    sdc_to_children = childrenMap(sdcs)
    sdc_to_esdc = {}

    i = 0

    while len(sdc_to_esdc) < len(sdcs):
        for parent, children in sdc_to_children.iteritems():
            if not parent in sdc_to_esdc:
                if all([child in sdc_to_esdc for child in children]):

                    esdcType = computeEsdcType(parent)
                    field_to_child = fieldToChild(parent, children,
                                                  sdc_to_esdc)
                    esdcFields = toEsdcFields(field_to_child)
                    sdc_to_esdc[parent] = ExtendedSdc(esdcType=esdcType,
                                                      **esdcFields)
        i += 1

    sdc_to_ancestors = ancestorMap(sdcs)

    esdcList = []
    for sdc in sdcs:
        if len(sdc_to_ancestors[sdc]) == 0:
            esdc = sdc_to_esdc[sdc]
            esdcList.append(esdc)

            #print sdc
            #print esdc
            #print
    objectifyLandmarks(esdcList)
    return ds.ExtendedSdcGroup(esdcList), sdc_to_esdc
Exemplo n.º 7
0
def toEsdcFields(field_to_child):
    outMap = {}
    if field_to_child["verb"] != None:
        outMap["r"] = field_to_child["verb"]
        if field_to_child["spatialRelation"] != None:
            if field_to_child["spatialRelation"] in ["at", "on", "in"]:
                esdcType = "PLACE"
            else:
                esdcType = "PATH"

            outMap["l"] = [
                ExtendedSdc(esdcType=esdcType,
                            r=field_to_child["spatialRelation"],
                            l=field_to_child["landmark"],
                            l2=field_to_child["landmark2"])
            ]
    elif field_to_child["spatialRelation"] != None:
        outMap["r"] = field_to_child["spatialRelation"]

    outMap["f"] = field_to_child["figure"]
    if not "l" in outMap:
        outMap["l"] = field_to_child["landmark"]

    outMap["l2"] = field_to_child["landmark2"]
    return outMap
Exemplo n.º 8
0
    def testRep(self):
        esdc = ExtendedSdc('OBJECT',
                           r=[],
                           l2=[],
                           l=[],
                           f=[],
                           entireText="Robots are awesome.")
        self.assertEqual(eval(repr(esdc)), esdc)

        esdcGroup = ExtendedSdcGroup([esdc], esdc.entireText)
        print repr(esdcGroup)
Exemplo n.º 9
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
Exemplo n.º 10
0
    def esdcGroupFromDependencies(self, sentence, dependencies):
        if len(dependencies.dependencies) == 0:
            return ExtendedSdcGroup(
                [ExtendedSdc("EVENT", r=dependencies.tokens[0])],
                entireText=sentence,
                score=dependencies.score,
                metadata=dependencies)
        else:
            esdcList = self.extractEsdcList(sentence, dependencies)
            hierarchicalEsdcs = self.extractHierarchy(dependencies, esdcList)
            objectifyLandmarks(hierarchicalEsdcs)
            objectifyFiguresOfEvents(hierarchicalEsdcs)
            addEmptyFigures(hierarchicalEsdcs)

            hierarchicalEsdcs.sort(key=lambda e: e.startIdx)
            return ExtendedSdcGroup(hierarchicalEsdcs,
                                    entireText=sentence,
                                    score=dependencies.score,
                                    metadata=dependencies)
Exemplo n.º 11
0
    def setUp(self):

        self.sentence = "Pick up the tire pallet."
        self.esdc1 = ExtendedSdc("EVENT",
                                 r=TextStandoff(self.sentence, (0, 7)),
                                 l=TextStandoff(self.sentence, (8, 23)))

        self.esdc2 = ExtendedSdc("EVENT",
                                 r=TextStandoff(self.sentence, (0, 7)),
                                 l=TextStandoff(self.sentence, (8, 23)))

        self.esdc3 = ExtendedSdc("EVENT",
                                 r=TextStandoff(self.sentence, (0, 7)),
                                 l=TextStandoff(self.sentence, (8, 22)))

        self.sentence2 = "Pick up the tire pallet near the truck."
        self.childEsdc = ExtendedSdc("OBJECT",
                                     f=TextStandoff(self.sentence2, (8, 23)),
                                     r=TextStandoff(self.sentence2, (24, 28)),
                                     l=TextStandoff(self.sentence2, (29, 38)))
        self.parentEsdc = ExtendedSdc("EVENT",
                                      r=TextStandoff(self.sentence2, (0, 7)),
                                      l=self.childEsdc)
Exemplo n.º 12
0
class TestCase(unittest.TestCase):
    def setUp(self):

        self.sentence = "Pick up the tire pallet."
        self.esdc1 = ExtendedSdc("EVENT",
                                 r=TextStandoff(self.sentence, (0, 7)),
                                 l=TextStandoff(self.sentence, (8, 23)))

        self.esdc2 = ExtendedSdc("EVENT",
                                 r=TextStandoff(self.sentence, (0, 7)),
                                 l=TextStandoff(self.sentence, (8, 23)))

        self.esdc3 = ExtendedSdc("EVENT",
                                 r=TextStandoff(self.sentence, (0, 7)),
                                 l=TextStandoff(self.sentence, (8, 22)))

        self.sentence2 = "Pick up the tire pallet near the truck."
        self.childEsdc = ExtendedSdc("OBJECT",
                                     f=TextStandoff(self.sentence2, (8, 23)),
                                     r=TextStandoff(self.sentence2, (24, 28)),
                                     l=TextStandoff(self.sentence2, (29, 38)))
        self.parentEsdc = ExtendedSdc("EVENT",
                                      r=TextStandoff(self.sentence2, (0, 7)),
                                      l=self.childEsdc)

    def testEqual(self):

        self.assertEqual(self.esdc1, self.esdc1)
        self.assertEqual(self.esdc1, self.esdc2)
        self.assertNotEqual(self.esdc1, self.esdc3)

    def testSetter(self):
        newL = [TextStandoff(self.sentence, (0, 4))]
        self.esdc1.l = newL

        self.assertEqual(self.esdc1.fields['l'], newL)
        self.assertNotEqual(self.esdc1, self.esdc2)

    def testFlatBreadthFirstTraverse(self):
        esdcs = []

        def callback(esdc):
            esdcs.append(esdc)

        dataStructures.breadthFirstTraverse(self.esdc1, callback)
        self.assertEqual(esdcs, [self.esdc1])

    def testAccessingFields(self):
        self.assertEqual(" ".join([x.text for x in self.childEsdc.f]),
                         "the tire pallet")

        self.assertEqual(" ".join([x.text for x in self.childEsdc.r]), "near")
        self.assertEqual(" ".join([x.text for x in self.childEsdc.l]),
                         "the truck")

    def testBreadthFirstTraverse(self):
        esdcs = []

        def callback(esdc):
            esdcs.append(esdc)

        dataStructures.breadthFirstTraverse(self.parentEsdc, callback)
        self.assertEqual(esdcs, [self.parentEsdc, self.childEsdc])

    def testEntireText(self):
        self.assertEqual(_getEntireText(self.parentEsdc),
                         self.parentEsdc.entireText)

    def testChildTypeChecking(self):
        self.assertEqual(self.parentEsdc.childIsEsdcs("l"), True)
        self.assertEqual(self.parentEsdc.childIsListOfWords("l"), False)
        self.assertEqual(self.parentEsdc.childIsEmpty("l"), False)

        self.assertEqual(self.parentEsdc.childIsEsdcs("f"), False)
        self.assertEqual(self.parentEsdc.childIsListOfWords("f"), False)
        self.assertEqual(self.parentEsdc.childIsEmpty("f"), True)

    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)

    def testRep(self):
        esdc = ExtendedSdc('OBJECT',
                           r=[],
                           l2=[],
                           l=[],
                           f=[],
                           entireText="Robots are awesome.")
        self.assertEqual(eval(repr(esdc)), esdc)

        esdcGroup = ExtendedSdcGroup([esdc], esdc.entireText)
        print repr(esdcGroup)

    def testEqual1(self):
        e1 = ExtendedSdc(
            'EVENT',
            r=[TextStandoff("Load the forklift onto the trailer.", (0, 4))],
            l2=[
                ExtendedSdc(
                    'PATH',
                    r=[
                        TextStandoff("Load the forklift onto the trailer.",
                                     (18, 22))
                    ],
                    l2=[],
                    l=[
                        ExtendedSdc(
                            'OBJECT',
                            r=[],
                            l2=[],
                            l=[],
                            f=[
                                TextStandoff(
                                    "Load the forklift onto the trailer.",
                                    (23, 26)),
                                TextStandoff(
                                    "Load the forklift onto the trailer.",
                                    (27, 34))
                            ])
                    ],
                    f=[])
            ],
            l=[
                ExtendedSdc(
                    'OBJECT',
                    r=[],
                    l2=[],
                    l=[],
                    f=[
                        TextStandoff("Load the forklift onto the trailer.",
                                     (5, 8)),
                        TextStandoff("Load the forklift onto the trailer.",
                                     (9, 17))
                    ])
            ],
            f=[
                ExtendedSdc('OBJECT',
                            r=[],
                            l2=[],
                            l=[],
                            f=[],
                            entireText='Load the forklift onto the trailer.')
            ])
        e2 = ExtendedSdc(
            'EVENT',
            r=[TextStandoff("Load the forklift onto the trailer.", (0, 4))],
            l2=[
                ExtendedSdc(
                    'PATH',
                    r=[
                        TextStandoff("Load the forklift onto the trailer.",
                                     (18, 22))
                    ],
                    l2=[],
                    l=[
                        ExtendedSdc(
                            'OBJECT',
                            r=[],
                            l2=[],
                            l=[],
                            f=[
                                TextStandoff(
                                    "Load the forklift onto the trailer.",
                                    (23, 26)),
                                TextStandoff(
                                    "Load the forklift onto the trailer.",
                                    (27, 34))
                            ])
                    ],
                    f=[])
            ],
            l=[
                ExtendedSdc(
                    'OBJECT',
                    r=[],
                    l2=[],
                    l=[],
                    f=[
                        TextStandoff("Load the forklift onto the trailer.",
                                     (5, 8)),
                        TextStandoff("Load the forklift onto the trailer.",
                                     (9, 17))
                    ])
            ],
            f=[
                ExtendedSdc('OBJECT',
                            r=[],
                            l2=[],
                            l=[],
                            f=[],
                            entireText='Load the forklift onto the trailer.')
            ])
        self.assertEqual(e1, e2)

        self.assertFalse(e1 != e2)

        self.assertTrue(e1 in [e2])
        self.assertTrue(e2 in [e1])

    def testBreadthFirstTraverseLoop(self):
        cmd = "Move in front of the pallet of boxes in the center and wait."
        esdc = ExtendedSdc(
            'EVENT',
            r=[TextStandoff(cmd, (0, 4)),
               TextStandoff(cmd, (5, 7))],
            l2=[],
            f=[TextStandoff(cmd, (8, 13))])
        esdc.l = [esdc]

        def callback(esdcFromParent):
            print "callback"

        dataStructures.breadthFirstTraverse(esdc, callback)

    def testParentsToChildren(self):

        esdcs = dataStructures.parentsToChildren(self.parentEsdc)
        print[str(e) for e in esdcs]
        self.assertEqual(esdcs, [self.parentEsdc, self.childEsdc])

    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)
Exemplo n.º 13
0
 def make_ggg_for_instruction(self, text):
     esdc = ExtendedSdc("EVENT", text, r=TextStandoff(text, (0, len(text))))
     ggg = ggg_from_esdc(esdc)
     return esdc, ggg
Exemplo n.º 14
0
Arquivo: stub.py Projeto: h2r/slu_core
 ExtendedSdc('EVENT',
             r=[
                 TextStandoff(
                     "Pick up the pallet of tires on the right and place them on the trailer on the left.", (45, 50))
             ],
             l2=[
                 ExtendedSdc('PLACE',
                             r=[
                                 TextStandoff(
                                     "Pick up the pallet of tires on the right and place them on the trailer on the left.", (56, 58))
                             ],
                             l2=[],
                             l=[
                                 ExtendedSdc('OBJECT',
                                             r=[
                                                 TextStandoff(
                                                     "Pick up the pallet of tires on the right and place them on the trailer on the left.", (71, 73))
                                             ],
                                             l2=[],
                                             l=[
                                                 ExtendedSdc('OBJECT',
                                                             r=[],
                                                             l2=[],
                                                             l=[],
                                                             f=[
                                                                 TextStandoff(
                                                                     "Pick up the pallet of tires on the right and place them on the trailer on the left.", (74, 77)),
                                                                 TextStandoff(
                                                                     "Pick up the pallet of tires on the right and place them on the trailer on the left.", (78, 82))
                                                             ])
                                             ],
                                             f=[
                                                 TextStandoff(
                                                     "Pick up the pallet of tires on the right and place them on the trailer on the left.", (59, 62)),
                                                 TextStandoff(
                                                     "Pick up the pallet of tires on the right and place them on the trailer on the left.", (63, 70))
                                             ])
                             ],
                             f=[])
             ],
             l=[
                 ExtendedSdc('OBJECT',
                             r=[],
                             l2=[],
                             l=[],
                             f=[
                                 TextStandoff(
                                     "Pick up the pallet of tires on the right and place them on the trailer on the left.", (51, 55))
                             ])
             ],
             f=[
                 ExtendedSdc('OBJECT',
                             r=[],
                             l2=[],
                             l=[],
                             f=[],
                             entireText='Pick up the pallet of tires on the right and place them on the trailer on the left.')
             ]): [
Exemplo n.º 15
0
    def testEqual1(self):
        e1 = ExtendedSdc(
            'EVENT',
            r=[TextStandoff("Load the forklift onto the trailer.", (0, 4))],
            l2=[
                ExtendedSdc(
                    'PATH',
                    r=[
                        TextStandoff("Load the forklift onto the trailer.",
                                     (18, 22))
                    ],
                    l2=[],
                    l=[
                        ExtendedSdc(
                            'OBJECT',
                            r=[],
                            l2=[],
                            l=[],
                            f=[
                                TextStandoff(
                                    "Load the forklift onto the trailer.",
                                    (23, 26)),
                                TextStandoff(
                                    "Load the forklift onto the trailer.",
                                    (27, 34))
                            ])
                    ],
                    f=[])
            ],
            l=[
                ExtendedSdc(
                    'OBJECT',
                    r=[],
                    l2=[],
                    l=[],
                    f=[
                        TextStandoff("Load the forklift onto the trailer.",
                                     (5, 8)),
                        TextStandoff("Load the forklift onto the trailer.",
                                     (9, 17))
                    ])
            ],
            f=[
                ExtendedSdc('OBJECT',
                            r=[],
                            l2=[],
                            l=[],
                            f=[],
                            entireText='Load the forklift onto the trailer.')
            ])
        e2 = ExtendedSdc(
            'EVENT',
            r=[TextStandoff("Load the forklift onto the trailer.", (0, 4))],
            l2=[
                ExtendedSdc(
                    'PATH',
                    r=[
                        TextStandoff("Load the forklift onto the trailer.",
                                     (18, 22))
                    ],
                    l2=[],
                    l=[
                        ExtendedSdc(
                            'OBJECT',
                            r=[],
                            l2=[],
                            l=[],
                            f=[
                                TextStandoff(
                                    "Load the forklift onto the trailer.",
                                    (23, 26)),
                                TextStandoff(
                                    "Load the forklift onto the trailer.",
                                    (27, 34))
                            ])
                    ],
                    f=[])
            ],
            l=[
                ExtendedSdc(
                    'OBJECT',
                    r=[],
                    l2=[],
                    l=[],
                    f=[
                        TextStandoff("Load the forklift onto the trailer.",
                                     (5, 8)),
                        TextStandoff("Load the forklift onto the trailer.",
                                     (9, 17))
                    ])
            ],
            f=[
                ExtendedSdc('OBJECT',
                            r=[],
                            l2=[],
                            l=[],
                            f=[],
                            entireText='Load the forklift onto the trailer.')
            ])
        self.assertEqual(e1, e2)

        self.assertFalse(e1 != e2)

        self.assertTrue(e1 in [e2])
        self.assertTrue(e2 in [e1])
Exemplo n.º 16
0
def merge(esdc1, esdc2, verbose=False):
    # event with more than one landmark
    if esdc1 == esdc2:
        return esdc1
    esdc1.updateRep()
    esdc2.updateRep()

    if esdc1.hasCycle() or esdc2.hasCycle():
        return None

    if verbose:
        print "*****************************"
        print esdc1
        print esdc2

    #if (esdc1.r == esdc2.r):
    if (all(r in esdc2.r for r in esdc1.r)
            or all(r in esdc1.r for r in esdc2.r)):
        firstEsdc, secondEsdc = firstAndSecond(esdc1, esdc2)
        newEsdc = ExtendedSdc.copy(firstEsdc)
        if verbose:
            print "merge 1"

        if len(firstEsdc.r) < len(secondEsdc.r):
            newEsdc.r = secondEsdc.r

        if firstEsdc.childIsEmpty("l"):
            newEsdc.l = secondEsdc.l
        elif firstEsdc.childIsEmpty("l2"):
            newEsdc.l2 = secondEsdc.l

        if newEsdc.childIsEmpty("l2"):
            newEsdc.l2 = secondEsdc.l2

        if firstEsdc.childIsEmpty("f"):
            newEsdc.f = secondEsdc.f
        else:
            newEsdc.f = firstEsdc.f

        if verbose:
            print newEsdc
        return newEsdc

    elif (esdc1.f == esdc2.f and esdc1.type == "OBJECT"
          and esdc2.type == "OBJECT"):
        if verbose:
            print "merge 2"
        firstEsdc, secondEsdc = firstAndSecond(esdc1, esdc2)

        newEsdc = ExtendedSdc.copy(secondEsdc)
        newEsdc.f = [firstEsdc]
        if verbose:
            print newEsdc
        return newEsdc
    elif (esdc1.type == "OBJECT"
          and baseFigureStandoff(esdc1) == baseLandmarkStandoff(esdc2)
          and not esdc1 in esdc2.l):
        if verbose:
            print "merge 3"

        newEsdc = ExtendedSdc.copy(esdc2)
        newEsdc.l = [esdc1]
        if verbose:
            print newEsdc
        return newEsdc
    else:
        return None
Exemplo n.º 17
0
    def extractEsdcList(self, sentence, dependencies):
        """
        Returns a list of ESDCs, without hierarchy.
        """

        esdcs = []

        child_esdcs = []

        leftover_deps = []

        for relation, gov, dep in dependencies.dependencies:
            #print "Relation:", relation
            if relation == "root":
                continue
            if relation == "prep":

                esdc = ExtendedSdc("EVENT",
                                   r=gov,
                                   l=ExtendedSdc("EVENT", r=dep))
            elif relation.startswith("prep"):
                prepStandoff, prep = standoffForString(relation, sentence)
                govTag = dependencies.tagForTokenStandoff(gov)
                if prep in [
                        "on", "in", "at", "near", "next to", "in front of",
                        "behind", "away from", "close to", "closer to"
                ]:
                    esdcType = "PLACE"
                else:
                    esdcType = "PATH"
                #if relation.startswith("prepc"):
                if not govTag in ["NN", "NNS", "NNP"]:
                    esdc = ExtendedSdc("EVENT",
                                       r=gov,
                                       l=ExtendedSdc(esdcType,
                                                     r=prepStandoff,
                                                     l=dep))
                    child_esdcs.extend(esdc.l)
                    esdcs.append(esdc)
                else:

                    if govTag in ["NN", "NNS", "NNP"]:
                        esdc = ExtendedSdc("OBJECT",
                                           f=gov,
                                           r=prepStandoff,
                                           l=dep)
                    else:
                        esdc = ExtendedSdc(
                            "EVENT",
                            r=gov,
                            l=[ExtendedSdc(esdcType, r=prepStandoff, l=dep)])
                        child_esdcs.extend(esdc.l)
                    esdcs.append(esdc)
            elif relation == "conj_and":
                esdcs.append(ExtendedSdc("EVENT", r=[gov]))
                esdcs.append(ExtendedSdc("EVENT", r=[dep]))
            elif self.sdh.isa(relation, "arg"):
                esdc = ExtendedSdc("EVENT", r=gov, l=dep)
                esdcs.append(esdc)

            elif self.sdh.isa(relation, "subj"):
                esdc = ExtendedSdc("EVENT", f=dep, r=gov)
                esdcs.append(esdc)
            elif relation == "conj_and":
                pass
            elif relation == "dep":
                esdc = ExtendedSdc("EVENT", r=[dep, gov])
                esdcs.append(esdc)
            else:
                leftover_deps.append((relation, gov, dep))

        for relation, gov, dep in leftover_deps:
            for esdc in flattenedEsdcs(esdcs):  #chain(esdcs, child_esdcs):
                for key, valueList in esdc:
                    if gov in valueList:
                        valueList.append(dep)

        freeze(esdcs)
        esdcs = list(sorted(set(esdcs)))
        unfreeze(esdcs)

        esdcs = mergeAll(esdcs)
        esdcs = mergeAll(esdcs)
        #esdcs = mergeAll(esdcs)

        #for esdc in flattenedEsdcs(esdcs):
        #    esdc.updateRep()

        for esdc in flattenedEsdcs(esdcs):
            esdc.updateRep()

        if len(esdcs) == 0:
            print "sentence", sentence, sentence.__class__
            sentence_standoff = TextStandoff(sentence, (0, len(sentence)))
            return [ExtendedSdc("EVENT", r=sentence_standoff)]
        return esdcs