def describe_event()

in textworld/generator/text_generation.py [0:0]


def describe_event(event: Event, game: Game, grammar: Grammar) -> str:
    """
    Assign a descripton to a quest.
    """
    # We have to "count" all the adj/noun/types in the world
    # This is important for using "unique" but abstracted references to objects
    counts = OrderedDict()
    counts["adj"] = CountOrderedDict()
    counts["noun"] = CountOrderedDict()
    counts["type"] = CountOrderedDict()

    # Assign name and description to objects.
    for obj in game.world.objects:
        if obj.type in ["I", "P"]:
            continue

        obj_infos = game.infos[obj.id]
        counts['adj'][obj_infos.adj] += 1
        counts['noun'][obj_infos.noun] += 1
        counts['type'][obj.type] += 1

    if len(event.actions) == 0:
        # We don't need to say anything if the quest is empty
        event_desc = ""
    else:
        # Generate a description for either the last, or all commands
        if grammar.options.only_last_action:
            actions_desc, _ = generate_instruction(event.actions[-1], grammar, game, counts)
            only_one_action = True
        else:
            actions_desc_list = []
            # Decide if we blend instructions together or not
            if grammar.options.blend_instructions:
                instructions = get_action_chains(event.actions, grammar, game)
            else:
                instructions = event.actions

            only_one_action = len(instructions) < 2
            for c in instructions:
                desc, separator = generate_instruction(c, grammar, game, counts)
                actions_desc_list.append(desc)
                if c != instructions[-1] and len(separator) > 0:
                    actions_desc_list.append(separator)
            actions_desc = " ".join(actions_desc_list)

        if only_one_action:
            quest_tag = grammar.get_random_expansion("#quest_one_action#")
            quest_tag = quest_tag.replace("(action)", actions_desc.strip())

        else:
            quest_tag = grammar.get_random_expansion("#quest#")
            quest_tag = quest_tag.replace("(list_of_actions)", actions_desc.strip())

        event_desc = grammar.expand(quest_tag)
        event_desc = re.sub(r"(^|(?<=[?!.]))\s*([a-z])",
                            lambda pat: pat.group(1) + ' ' + pat.group(2).upper(),
                            event_desc)

    return event_desc