in textworld/generator/text_generation.py [0:0]
def assign_description_to_room(room, game, grammar):
"""
Assign a descripton to a room.
"""
# Add the decorative text
room_desc = expand_clean_replace("#dec#\n\n", grammar, room, game)
# Convert the objects into groupings based on adj/noun/type
objs = [o for o in room.content if game.kb.types.is_descendant_of(o.type, game.kb.types.CLASS_HOLDER)]
groups = OrderedDict()
groups["adj"] = OrderedDict()
groups["noun"] = OrderedDict()
for obj in objs:
obj_infos = game.infos[obj.id]
adj, noun = obj_infos.adj, obj_infos.noun
# get all grouped adjectives and nouns
groups['adj'][adj] = list(filter(lambda x: game.infos[x.id].adj == adj, objs))
groups['noun'][noun] = list(filter(lambda x: game.infos[x.id].noun == noun, objs))
# Generate the room description, prioritizing group descriptions where possible
ignore = []
for obj in objs:
if obj.id in ignore:
continue # Skip that object.
obj_infos = game.infos[obj.id]
adj, noun = obj_infos.adj, obj_infos.noun
if grammar.options.blend_descriptions:
found = False
for type in ["noun", "adj"]:
group_filt = []
if getattr(obj_infos, type) != "":
group_filt = list(filter(lambda x: x.id not in ignore, groups[type][getattr(obj_infos, type)]))
if len(group_filt) > 1:
found = True
desc = replace_num(grammar.expand("#room_desc_group#"), len(group_filt))
if type == "noun":
desc = desc.replace("(val)", "{}s".format(getattr(obj_infos, type)))
desc = desc.replace("(name)", obj_list_to_prop_string(group_filt, "adj", game, det_type="one"))
elif type == "adj":
_adj = getattr(obj_infos, type) if getattr(obj_infos, type) is not None else ""
desc = desc.replace("(val)", "{}things".format(_adj))
desc = desc.replace("(name)", obj_list_to_prop_string(group_filt, "noun", game))
for o2 in group_filt:
ignore.append(o2.id)
if game.kb.types.is_descendant_of(o2.type, game.kb.types.CLASS_HOLDER):
for vtype in [o2.type] + game.kb.types.get_ancestors(o2.type):
tag = "#room_desc_({})_multi_{}#".format(vtype, "adj" if type == "noun" else "noun")
if grammar.has_tag(tag):
desc += expand_clean_replace(" " + tag, grammar, o2, game)
break
room_desc += " {}".format(fix_determinant(desc))
break
if found:
continue
if obj.type not in ["P", "I", "d"]:
for vtype in [obj.type] + game.kb.types.get_ancestors(obj.type):
tag = "#room_desc_({})#".format(vtype)
if grammar.has_tag(tag):
room_desc += expand_clean_replace(" " + tag, grammar, obj, game)
break
room_desc += "\n\n"
# Look for potential exit directions.
exits_with_open_door = []
exits_with_closed_door = []
exits_without_door = []
for dir_ in sorted(room.exits.keys()):
if dir_ in room.doors:
door_obj = room.doors[dir_]
attributes_names = [attr.name for attr in door_obj.get_attributes()]
if "open" in attributes_names:
exits_with_open_door.append((dir_, door_obj))
else:
exits_with_closed_door.append((dir_, door_obj))
else:
exits_without_door.append(dir_)
exits_desc = []
# Describing exits with door.
if grammar.options.blend_descriptions and len(exits_with_closed_door) > 1:
dirs, door_objs = zip(*exits_with_closed_door)
e_desc = grammar.expand("#room_desc_doors_closed#")
e_desc = replace_num(e_desc, len(door_objs))
e_desc = e_desc.replace("(dir)", list_to_string(dirs, False))
e_desc = clean_replace_objs(grammar, e_desc, door_objs, game.infos)
e_desc = repl_sing_plur(e_desc, len(door_objs))
exits_desc.append(e_desc)
else:
for dir_, door_obj in exits_with_closed_door:
d_desc = expand_clean_replace(" #room_desc_(d)#", grammar, door_obj, game)
d_desc = d_desc.replace("(dir)", dir_)
exits_desc.append(d_desc)
if grammar.options.blend_descriptions and len(exits_with_open_door) > 1:
dirs, door_objs = zip(*exits_with_open_door)
e_desc = grammar.expand("#room_desc_doors_open#")
e_desc = replace_num(e_desc, len(door_objs))
e_desc = e_desc.replace("(dir)", list_to_string(dirs, False))
e_desc = clean_replace_objs(grammar, e_desc, door_objs, game.infos)
e_desc = repl_sing_plur(e_desc, len(door_objs))
exits_desc.append(e_desc)
else:
for dir_, door_obj in exits_with_open_door:
d_desc = expand_clean_replace(" #room_desc_(d)#", grammar, door_obj, game)
d_desc = d_desc.replace("(dir)", dir_)
exits_desc.append(d_desc)
# Describing exits without door.
if grammar.options.blend_descriptions and len(exits_without_door) > 1:
e_desc = grammar.expand("#room_desc_exits#").replace("(dir)", list_to_string(exits_without_door, False))
e_desc = repl_sing_plur(e_desc, len(exits_without_door))
exits_desc.append(e_desc)
else:
for dir_ in exits_without_door:
e_desc = grammar.expand("#room_desc_(dir)#").replace("(dir)", dir_)
exits_desc.append(e_desc)
room_desc += " ".join(exits_desc)
# Finally, set the description
return fix_determinant(room_desc)