Example #1
0
  def example():
    """Example of using the Relational class."""
    facts = [[Type, "cup", "c1"], [Type, "cup", "c2"], [Type, "cup", "c3"],
             [Type, "bowl", "b1"], [Type, "bowl", "b2"],
             [Type, "table", "t1"], [Type, "table", "t2"], ["color", "red", "t2"],
             [Type, "floor", "f1"],
             [Rel, "in", "c1", "b1"], [Rel, "in", "c2", "b2"],
             [Rel, "on", "c3", "f1"], [Rel, "on", "b1", "f1"], [Rel, "on", "b2", "t1"],
             [Rel, "on", "t1", "f1"], [Rel, "on", "t2", "f1"]]

    rel = Relational(facts)
    obj_types = [f for f in facts if f[0] == Type] # Include types in the description for clarity
    handlers = {
      "on" : lambda(lr): "on" if lr else "on which lies",
      "in" : lambda(lr): "in" if lr else "in which lies"
    }

    # Generate an English description for each object
    for obj_id in ["c1", "c2", "c3", "b1", "b2", "t1", "t2", "f1"]:
      print "%s: %s" % (obj_id, generate_phrase_rel(rel.describe(obj_id) + obj_types, ["color"], obj_id, handlers))
Example #2
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))

Example #3
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))
Example #4
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))