def interpret_named_schematic( interpreter, speaker, d) -> Tuple[List[Block], Optional[str], List[Tuple[str, str]]]: """Return a tuple of 3 values: - the schematic blocks, list[(xyz, idm)] - a SchematicNode memid, or None - a list of (pred, val) tags """ if "has_name" not in d: raise ErrorWithResponse("I don't know what you want me to build.") name = d["has_name"] stemmed_name = stemmer.stemWord(name) shapename = SPECIAL_SHAPES_CANONICALIZE.get( name) or SPECIAL_SHAPES_CANONICALIZE.get(stemmed_name) if shapename: blocks, tags = interpret_shape_schematic(speaker, d, shapename=shapename) return blocks, None, tags schematic = interpreter.memory.get_schematic_by_name(name) if schematic is None: schematic = interpreter.memory.get_schematic_by_name(stemmed_name) if schematic is None: raise ErrorWithResponse("I don't know what you want me to build.") tags = [(p, v) for (_, p, v) in interpreter.memory.get_triples(subj=schematic.memid)] return list(schematic.blocks.items()), schematic.memid, tags
def interpret_named_schematic( interpreter, speaker, d ) -> Tuple[List[Block], Optional[str], List[Tuple[str, str]]]: """Return a tuple of 3 values: - the schematic blocks, list[(xyz, idm)] - a SchematicNode memid, or None - a list of (pred, val) tags warning: if multiple possibilities are given for the same tag, current heursitic just picks one. e.g. if the lf is "triples" : [{"pred_text": "has_colour", "obj_text": "red"}, {"pred_text": "has_colour", "obj_text": "blue"}] will currently just pick red. Same for other properties encoded in triples """ # FIXME! this is not compositional, and is not using full FILTERS handlers filters_d = d.get("filters", {}) triples = filters_d.get("triples", []) names = get_properties_from_triples(triples, "has_name") if not any(names): raise ErrorWithResponse("I don't know what you want me to build.") name = names[0] stemmed_name = name.strip("s") # why aren't we using stemmer anymore? shapename = SPECIAL_SHAPES_CANONICALIZE.get(name) or SPECIAL_SHAPES_CANONICALIZE.get( stemmed_name ) if shapename: shape_blocks, tags = interpret_shape_schematic( interpreter, speaker, d, shapename=shapename ) return shape_blocks, None, tags schematic = interpreter.memory.get_schematic_by_name(name) if schematic is None: schematic = interpreter.memory.get_schematic_by_name(stemmed_name) if schematic is None: raise ErrorWithResponse("I don't know what you want me to build.") tags = [(p, v) for (_, p, v) in interpreter.memory.get_triples(subj=schematic.memid)] blocks = schematic.blocks # TODO generalize to more general block properties # Longer term: remove and put a call to the modify model here colours = get_properties_from_triples(triples, "has_colour") if any(colours): colour = colours[0] old_idm = most_common_idm(blocks.values()) c = block_data.COLOR_BID_MAP.get(colour) if c is not None: new_idm = random.choice(c) for l in blocks: if blocks[l] == old_idm: blocks[l] = new_idm return list(blocks.items()), schematic.memid, tags
def interpret_named_schematic( interpreter, speaker, d) -> Tuple[List[Block], Optional[str], List[Tuple[str, str]]]: """Return a tuple of 3 values: - the schematic blocks, list[(xyz, idm)] - a SchematicNode memid, or None - a list of (pred, val) tags """ if "has_name" not in d: raise ErrorWithResponse("I don't know what you want me to build.") name = d["has_name"] stemmed_name = name shapename = SPECIAL_SHAPES_CANONICALIZE.get( name) or SPECIAL_SHAPES_CANONICALIZE.get(stemmed_name) if shapename: shape_blocks, tags = interpret_shape_schematic(interpreter, speaker, d, shapename=shapename) return shape_blocks, None, tags schematic = interpreter.memory.get_schematic_by_name(name) if schematic is None: schematic = interpreter.memory.get_schematic_by_name(stemmed_name) if schematic is None: raise ErrorWithResponse("I don't know what you want me to build.") tags = [(p, v) for (_, p, v) in interpreter.memory.get_triples(subj=schematic.memid)] blocks = schematic.blocks # TODO generalize to more general block properties # Longer term: remove and put a call to the modify model here if d.get("has_colour"): old_idm = most_common_idm(blocks.values()) c = block_data.COLOR_BID_MAP.get(d["has_colour"]) if c is not None: new_idm = random.choice(c) for l in blocks: if blocks[l] == old_idm: blocks[l] = new_idm return list(blocks.items()), schematic.memid, tags
def specify_object(ref_obj_dict, sl=32, flat=False): shape_keys = [ "has_size", "has_thickness", "has_radius", "has_depth", "has_width", "has_height", "has_length", "has_slope", "has_orientation", "has_distance", "has_base", "has_colour", ] ############################################## # hack to deal with size ranges in size_words ############################################## fake_interpreter = Dummy() fake_interpreter.agent = Dummy() def ssti(s): return size_str_to_int(s, ranges=RANGES) fake_interpreter.agent.size_str_to_int = ssti name = ref_obj_dict.get("has_name") if name is not None: stemmed_name = stemmer.stemWord(name) shapename = SPECIAL_SHAPES_CANONICALIZE.get(name) or SPECIAL_SHAPES_CANONICALIZE.get( stemmed_name ) if SPAWN_OBJECTS.get(name): return name elif SPAWN_OBJECTS.get(stemmed_name): return stemmed_name if shapename: regularize_ref_obj_dict(ref_obj_dict, flat=flat) blocks, _ = interpret_shape_schematic( fake_interpreter, None, ref_obj_dict, shapename=shapename ) return blocks else: # somethings wrong, abort return None else: if ref_obj_dict.get("has_shape"): regularize_ref_obj_dict(ref_obj_dict, flat=flat) blocks, _ = interpret_shape_schematic(fake_interpreter, None, ref_obj_dict) return blocks elif any(k in shape_keys for k in ref_obj_dict.keys()): regularize_ref_obj_dict(ref_obj_dict, flat=flat) if flat: shapename = random.choice(["TRIANGLE", "CIRCLE", "DISK", "RECTANGLE"]) else: shapename = random.choice(list(SPECIAL_SHAPE_FNS.keys())) blocks, _ = interpret_shape_schematic( fake_interpreter, None, ref_obj_dict, shapename=shapename ) return blocks else: # somethings wrong, abort return None
S["id_to_obj"] = {} for m in S["mobs"]: S["id_to_obj"][m["dict_id"]] = m for m in S["block_obj"]: S["id_to_obj"][m["dict_id"]] = m if flat: for b in S["block_obj"]: if b["blocks"] is not None: b["blocks"] = get_slice(b["blocks"], axis=1, coord=0) return S if __name__ == "__main__": template_attributes = {"count": range(1, 5)} template_attributes["step"] = range(1, 10) template_attributes["non_shape_names"] = list(SPECIAL_SHAPES_CANONICALIZE.keys()) template_attributes["mob_names"] = ["pig", "sheep", "cow", "chicken", "rabbit"] for i in range(1000): print(i) S = build_scene(template_attributes) # import json # x = {'a':{'at':'d', 'R':{'hs':'h', 'ha':'h', 'l':{'rd':'right', 'lt':'RO','R':{'hn':'donk'}}}}} # print(json.dumps(x, sort_keys=True, indent=4)) # u = get_fields_by_key((None, x) , 'R') # print(len(u))
S["id_to_obj"][m["dict_id"]] = m for m in S["block_obj"]: S["id_to_obj"][m["dict_id"]] = m if flat: for b in S["block_obj"]: if b["blocks"] is not None: b["blocks"] = get_slice(b["blocks"], axis=1, coord=0) return S if __name__ == "__main__": template_attributes = {"count": range(1, 5)} template_attributes["step"] = range(1, 10) template_attributes["non_shape_names"] = list( SPECIAL_SHAPES_CANONICALIZE.keys()) template_attributes["mob_names"] = [ "pig", "sheep", "cow", "chicken", "rabbit" ] for i in range(1000): print(i) S = build_scene(template_attributes) # import json # x = {'a':{'at':'d', 'R':{'hs':'h', 'ha':'h', 'l':{'rd':'right', 'lt':'RO','R':{'hn':'donk'}}}}} # print(json.dumps(x, sort_keys=True, indent=4)) # u = get_fields_by_key((None, x) , 'R') # print(len(u))