Ejemplo n.º 1
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
Ejemplo n.º 2
0
def editDistance(esdc1, esdc2):

    yaml1 = esdcIo.toYaml(esdc1)
    yaml2 = esdcIo.toYaml(esdc2)

    str1 = yaml.dump(yaml1)
    str2 = yaml.dump(yaml2)

    score = edit_distance(str1, str2)

    score = float(score) / max([len(str1), len(str2)])
    return 1 - score
Ejemplo n.º 3
0
    def toYaml(self):
        self.checkRep()
        yamlData = {}

        yamlData['assignmentId'] = self.assignmentId
        yamlData['command'] = esdcIo.toYaml(self.esdcs)
        if self.agent != None:
            yamlData['agent'] = self.agent.toYaml()

        if self.context != None:
            yamlData['context'] = self.context.toYaml()

        try:
            g = [
                groundings.toYaml(self.esdcToGroundings[esdc])
                for esdc in self.flattenedEsdcs
            ]
        except KeyError:
            print esdc
            raise

        if any(len(x) != 0 for x in g):
            yamlData['objectGroundings'] = g
            yamlData['groundingIsCorrect'] = [
                self.isGroundingCorrect(e) for e in self.flattenedEsdcs
            ]
        yamlData['source'] = [self.getSource(e) for e in self.flattenedEsdcs]

        return yamlData
Ejemplo n.º 4
0
    def testPickUpCokeCan(self):

        esdcs = self.extractor.extractEsdcsFromSentence(
            """Pick up the Coke can""")
        self.aeq(
            toYaml(esdcs)[1], [{
                'EVENT': {
                    'r': 'Pick up',
                    'l': 'the Coke can'
                }
            }])
Ejemplo n.º 5
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
Ejemplo n.º 6
0
def main():
    parser = OptionParser()
    parser.add_option("--source_corpus",
                      dest="source_corpus",
                      help="The file or directory to read from")
    parser.add_option("--dest_corpus",
                      dest="dest_corpus",
                      help="The yaml file to which data will be written")
    (options, args) = parser.parse_args()

    corpus = readCorpus.Corpus(options.source_corpus)
    extractor = Extractor()
    #oldCorpus = annotationIo.load("%s/dataAnnotation/data/forkliftMturkEsdcs.stefie10.groundings.withPaths.yaml" % os.environ['FORKLIFT_HOME'])
    #oldAssignmentIds = set(a.assignmentId for a in oldCorpus)
    #print len(oldAssignmentIds), "old ids"
    yamlData = []
    print len(corpus.assignments), "assignments"
    filteredAnnotations = [
        x for x in corpus.assignments if (x.scenario.platform == "forklift"
                                          and x.scenario.isSimulated == True)
    ]
    print len(filteredAnnotations), "filtered annotations"
    output_annotations = []
    for i, a in enumerate(filteredAnnotations):
        esdcs = extractor.extractEsdcs(a.command)
        assignmentId = a.assignmentId
        agent = a.agent
        context = a.context
        esdcSource = [assignmentId for esdc in esdcs.flattenedEsdcs]
        output_annotations.append(
            Annotation(assignmentId=assignmentId,
                       esdcs=esdcs,
                       objectGroundings=None,
                       groundingIsCorrect=None,
                       agent=agent,
                       context=context,
                       esdcSource=esdcSource))
        esdcYaml = toYaml(esdcs)
        print "******"
        print a, esdcYaml
        print esdcs

        yamlData.append({"command": esdcYaml, "assignmentId": a.assignmentId})

    print "dumped", i, "commands."
    yaml.dump(yamlData, open(options.dest_corpus, "w"))
Ejemplo n.º 7
0
    def ain(self, esdc_groups, correct_yaml):
        current_metadata = None
        parse_i = 0
        lst = [toYaml(esdcs)[1] for esdcs in esdc_groups]

        for i, l in enumerate(lst):
            esdc = esdc_groups[i]
            if esdc.metadata != current_metadata:
                print "****************************** dependency", parse_i,

                current_metadata = esdc.metadata
                print current_metadata[0]
                parse_i += 1

            if correct_yaml == l:
                print "correct"
            else:
                print "incorrect"

            print l
        if not correct_yaml in lst:
            self.fail("Correct esdc not in results.")
Ejemplo n.º 8
0
    def testCrash(self):

        esdcs = self.extractor.extractEsdcs(
            """The trailer is to your right, and in at the end of the row of pallets."""
        )

        self.aeq(
            toYaml(esdcs)[1], [{
                'EVENT': {
                    'r': 'is',
                    'l2': {
                        'PLACE': {
                            'r': 'in',
                            'l': 'your right'
                        }
                    },
                    'l': {
                        'PATH': {
                            'r': 'to',
                            'l': 'your right'
                        }
                    },
                    'f': 'The trailer'
                }
            }, {
                'PLACE': {
                    'r': 'at',
                    'l': {
                        'OBJECT': {
                            'r': [['of', [48, 50]]],
                            'l2': 'pallets',
                            'l': 'the row',
                            'f': 'the end'
                        }
                    }
                }
            }])
Ejemplo n.º 9
0
    def followCommand(self,
                      command=None,
                      state=None,
                      esdcs=None,
                      first_esdc_only=False,
                      verbose=True,
                      input_gggs=None):
        if command != None:
            self.updateCommand(command)

        if state != None:
            self.state = state

        command = str(self.commandEdit.toPlainText())

        if input_gggs != None:
            self.esdcs = input_gggs[0].esdcs
        elif esdcs != None and command == self.command:
            self.esdcs = esdcs
        else:

            class AnnotationStub:
                def __init__(self, command):
                    self.entireText = command

            self.esdcs = self.extractor(AnnotationStub(command))

            if len(self.esdcs) == 0:
                return [], []
            if first_esdc_only:
                self.esdcs = ExtendedSdcGroup([self.esdcs[0]])
                self.flattenedEsdcs = self.esdcs.flattenedEsdcs
        print "esdcs", esdcIo.toYaml(self.esdcs)
        #print "flattened esdcs", self.flattenedEsdcs

        self.esdcModel.setData(self.esdcs)
        self.esdcView.expandAll()

        #convert to a list of plans
        if input_gggs != None:
            gggs = input_gggs
        elif self.gggs != None:
            gggs = self.gggs
        else:
            if self.merging_mode == "merge_coreferences":
                from coreference.bag_of_words_resolver import BagOfWordsResolver
                from coreference.merge_coreferences import merge_coreferences
                resolver = BagOfWordsResolver(
                    "%s/tools/coreference/models/coref_1.5.pck" % SLU_HOME)
                gggs = merge_coreferences(self.esdcs, resolver)
                if len(gggs) > 1:
                    gggs = [gggs[0]]
            elif self.merging_mode == "merge_events":
                from coreference.event_resolver import EventResolver
                from coreference.merge_coreferences import merge_coreferences
                resolver = EventResolver()
                gggs = merge_coreferences(self.esdcs, resolver)
                gggs = gggs[0:1]
                for i, ggg in enumerate(gggs):
                    #ggg.to_latex("ggg_%d.pdf" % i)
                    pass

                # export parses, when saving ground truth parses from asr evaluation run
                # -- stefie10 7/19/2012
                #from esdcs.esdcIo import toYaml
                #import yaml
                #import os
                #with open("esdcs_%d.txt" % os.getpid(), "a") as f:
                #    f.write(yaml.dump([toYaml(self.esdcs)]))

                if len(self.esdcs) != 1:
                    raise ValueError("ESDCs didn't parse right: " +
                                     ` self.esdcs `)
                # print "merging events", self.esdcs.entireText
                # from coreference.merge_coreferences import merge_toplevel_events
                # #resolver = EventResolver()
                # #gggs = merge_coreferences(self.esdcs, resolver,
                # #                          verbose=False)
                # gggs = gggs_from_esdc_group(self.esdcs)
                # gggs = [merge_toplevel_events(gggs)]

                assert len(gggs) == 1, (len(gggs), self.esdcs.text)
            elif self.merging_mode == "merge_none":
                gggs = gggs_from_esdc_group(self.esdcs)
            else:
                raise ValueError("Bad merging mode: " + ` self.merging_mode `)

            if gggs[0].context == None:
                gggs[0].context = self.state.to_context()
            assert gggs[0].context.agent != None

        def run():
            try:
                assert gggs[0].context.agent != None
                self.plansList = self.taskPlanner.find_plan(
                    self.state,
                    gggs,
                    beam_width=self.beamWidthBox.value(),
                    beam_width_sequence=self.seqBeamWidthBox.value(),
                    search_depth_event=self.searchDepthBox.value(),
                    beam_width_event=self.beamWidthEventBox.value(),
                    save_state_tree=self.save_state_tree,
                    allow_null_action=self.allow_null_action)
            except:
                print "excption on gggs"
                #for i, ggg in enumerate(gggs):
                #    ggg.to_latex("ggg_%d.pdf" % i)
                raise

        start = time.time()
        cProfile.runctx("run()", globals(), locals(), "out.prof")
        end = time.time()

        if verbose:
            print "Cost Function Browser took", (end - start), "seconds."

        plansList = self.plansList
        if len(plansList) == 0:
            return [], []
        else:

            self.plans = plansModel.Plan.from_inference_result(
                self.taskPlanner, plansList)
            self.plansModel.setData(self.plans)
            self.nodeFeatureWeights.load(self.taskPlanner.cf_obj,
                                         gggs,
                                         self.plans,
                                         context=self.state.to_context())
            self.plansView.selectRow(0)
            self.gggWindow.load(plansList[0][2],
                                groundingSpace=self.state.objects)
            return self.esdcs, self.plans
Ejemplo n.º 10
0
    def testGoLeft(self):

        command = "go left"

        esdc_group = self.extractor.extractEsdcs(command)
        self.aeq(toYaml(esdc_group)[1], [{'EVENT': {'r': 'go left'}}])
Ejemplo n.º 11
0
    def testOrdering(self):

        esdcs = self.extractor.extractEsdcs(
            """Come out of the parking lot and turn left. You''ll come to Titus Avenue. Take
    a right, and then a quick left onto Hudson. Wegmans will be on the right."""
        )
        print esdcs.__class__
        self.aeq(
            toYaml(esdcs)[1], [{
                'EVENT': {
                    'r': 'Come',
                    'l': {
                        'PATH': {
                            'r': 'out of',
                            'l': 'the parking lot'
                        }
                    }
                }
            }, {
                'EVENT': {
                    'r': 'turn left'
                }
            }, {
                'EVENT': {
                    'r': [['You', [43, 46]], ['come', [51, 55]]],
                    'l': {
                        'PATH': {
                            'r': [['to', [56, 58]]],
                            'l': 'Titus Avenue'
                        }
                    },
                    'f': [['ll', [48, 50]]]
                }
            }, {
                'EVENT': {
                    'r': 'Take',
                    'l2': {
                        'PATH': {
                            'r': 'onto',
                            'l': 'Hudson'
                        }
                    },
                    'l': [['a', [78, 79]], ['right', [80, 85]],
                          ['then', [91, 95]]]
                }
            }, {
                'EVENT': {
                    'r': [['a', [78, 79]], ['right', [80, 85]],
                          ['then', [91, 95]]]
                }
            }, {
                'EVENT': {
                    'r': 'a quick left'
                }
            }, {
                'EVENT': {
                    'r': 'will be',
                    'l': {
                        'PLACE': {
                            'r': [['on', [138, 140]]],
                            'l': 'the right'
                        }
                    },
                    'f': 'Wegmans'
                }
            }])