예제 #1
0
  def example():
    """Example of using the FullBrevity class."""
    facts = [[Type, "cube", "obj1"], ["color", "red", "obj1"], ["size", "big", "obj1"],
             [Type, "ball", "obj2"], ["color", "blue", "obj2"], ["size", "big", "obj2"],
             [Type, "ball", "obj3"], ["color", "red", "obj3"], ["size", "small", "obj3"]]

    fb = FullBrevity(facts)

    # Print English description for each object
    for obj_id in ["obj1", "obj2", "obj3"]:
      obj_type = [f for f in facts if f[0] == Type and f[2] == obj_id] # Include type for clarity
      print "%s: %s" % (obj_id, generate_phrase(fb.describe(obj_id) + obj_type, ["color", "size"]))
예제 #2
0
    def example():
        """Example of using the Incremental class."""
        facts = [[Type, "cube", "obj1"], ["color", "brick", "obj1"],
                 ["size", "big", "obj1"], [Type, "ball", "obj2"],
                 ["color", "navy", "obj2"], ["size", "big", "obj2"],
                 [Type, "ball", "obj3"], ["color", "scarlet", "obj3"],
                 ["size", "small", "obj3"]]

        ranked_attrs = ["color", "size", Type]
        tax = {
            "blue": {
                "parent": None,
                "children": ["navy", "cerulean"]
            },
            "red": {
                "parent": None,
                "children": ["scarlet", "brick"]
            },
            "navy": {
                "parent": "blue",
                "children": []
            },
            "cerulean": {
                "parent": "blue",
                "children": []
            },
            "scarlet": {
                "parent": "red",
                "children": []
            },
            "brick": {
                "parent": "red",
                "children": []
            }
        }

        taxonomy = Taxonomy(tax)
        incr = Incremental(facts, ranked_attrs, taxonomy)

        # Print English description for each object
        for obj_id in ["obj1", "obj2", "obj3"]:
            obj_type = [f for f in facts if f[0] == Type and f[2] == obj_id
                        ]  # Include type for clarity
            print "%s: %s" % (obj_id,
                              generate_phrase(
                                  incr.describe(obj_id) + obj_type,
                                  ["color", "size"]))
예제 #3
0
    def example():
        """Example of using the Incremental class."""
        facts = [[Type, "cube", "obj1"], ["color", "brick", "obj1"], ["size", "big", "obj1"],
                 [Type, "ball", "obj2"], ["color", "navy", "obj2"], ["size", "big", "obj2"],
                 [Type, "ball", "obj3"], ["color", "scarlet", "obj3"], ["size", "small", "obj3"]]

        ranked_attrs = ["color", "size", Type]
        tax = {
            "blue" : {"parent" : None, "children" : ["navy", "cerulean"]},
            "red"  : {"parent" : None, "children" : ["scarlet", "brick"]},
            "navy" : {"parent" : "blue", "children" : []},
            "cerulean" : {"parent" : "blue", "children" : []},
            "scarlet" : {"parent" : "red", "children" : []},
            "brick" : {"parent" : "red", "children" : []}
        }

        taxonomy = Taxonomy(tax)
        incr = Incremental(facts, ranked_attrs, taxonomy)

        # Print English description for each object
        for obj_id in ["obj1", "obj2", "obj3"]:
            obj_type = [f for f in facts if f[0] == Type and f[2] == obj_id] # Include type for clarity
            print "%s: %s" % (obj_id, generate_phrase(incr.describe(obj_id) + obj_type, ["color", "size"]))
예제 #4
0
  shuffle(facts, lambda: 0.0)

  fb = FullBrevity(filter(lambda f: f[0] != Rel, facts))
  rel = Relational(facts)
  #The ordered priority for using attributes, important for incremental algorithm
  ranked_attrs = ["color", "row", "col", "corner"]
  #Taxonomy used in incremental algorithm to pick out a more common name when appropriate
  # For instance dog instead of Chihuahua when there is a referent dog amongst other animals (which are not dogs)
  taxonomy = Taxonomy({})
  incr = Incremental(facts, ranked_attrs, taxonomy)

  #defines how to turn these rules into English phrases
  handlers = {
    "col": lambda(desc): "column %s" % desc,
    "row": lambda(desc): "row %s" % desc,
    "corner": lambda(desc): "corner",
    "above": lambda(lr): "above" if lr else "below",
    "below": lambda(lr): "below" if lr else "above",
    "right": lambda(lr): "to the right of" if lr else "to the left of",
    "left": lambda(lr): "to the left of" if lr else "to the right of"
  }
  
  #Generate phrases with each algorithm and print to screen
  for i in range(1, 17):
    obj_id = "d%s" % i
    print "%#02d,\"Full Brevity\",\"%s\"" % (i, generate_phrase(fb.describe(obj_id), ranked_attrs, handlers))
    print "%#02d,\"Relational\",\"%s\"" % (i, generate_phrase_rel(rel.describe(obj_id), ranked_attrs, obj_id, handlers))
    print "%#02d,\"Incremental\",\"%s\"" % (i, generate_phrase(incr.describe(obj_id), ranked_attrs, handlers))

예제 #5
0
if __name__ == '__main__':
    facts = getFacts()

    ranked_attrs = ["color", "size", Type]
    taxonomy = Taxonomy({})

    handlers = {
        "in_front_of": lambda (lr): "in front of",
        "left_of": lambda (lr): "to the left of",
        "right_of": lambda (lr): "to the right of"
    }

    #Print out the referring expressions generated by each algorithm for each scene
    for i in range(1, 21):
        fb = FullBrevity(facts[i])
        desc_fb = fb.describe("r1")

        incr = Incremental(facts[i], ranked_attrs, taxonomy)
        desc_incr = incr.describe("r1")

        rel = Relational(facts[i])
        desc_rel = rel.describe("r1")

        print "%#02d,\"Full Brevity\",\"%s\"" % (
            i, util.generate_phrase(desc_fb, ranked_attrs))
        print "%#02d,\"Incremental\",\"%s\"" % (
            i, util.generate_phrase(desc_incr, ranked_attrs))
        print "%#02d,\"Relational\",\"%s\"" % (
            i, util.generate_phrase_rel(desc_rel, ranked_attrs, "r1",
                                        handlers))
예제 #6
0
    return facts


if __name__ == "__main__":
    facts = getFacts()

    ranked_attrs = ["color", "size", Type]
    taxonomy = Taxonomy({})

    handlers = {
        "in_front_of": lambda (lr): "in front of",
        "left_of": lambda (lr): "to the left of",
        "right_of": lambda (lr): "to the right of",
    }

    # Print out the referring expressions generated by each algorithm for each scene
    for i in range(1, 21):
        fb = FullBrevity(facts[i])
        desc_fb = fb.describe("r1")

        incr = Incremental(facts[i], ranked_attrs, taxonomy)
        desc_incr = incr.describe("r1")

        rel = Relational(facts[i])
        desc_rel = rel.describe("r1")

        print '%#02d,"Full Brevity","%s"' % (i, util.generate_phrase(desc_fb, ranked_attrs))
        print '%#02d,"Incremental","%s"' % (i, util.generate_phrase(desc_incr, ranked_attrs))
        print '%#02d,"Relational","%s"' % (i, util.generate_phrase_rel(desc_rel, ranked_attrs, "r1", handlers))