Grammar

class textworld.generator.text_generation.CountOrderedDict[source]

Bases: collections.OrderedDict

An OrderedDict whose empty items are 0

class textworld.generator.text_generation.MergeAction[source]

Bases: object

Group of actions merged into one.

This allows for blending consecutive instructions.

textworld.generator.text_generation.assign_description_to_object(obj, grammar, game_infos)[source]

Assign a descripton to an object.

textworld.generator.text_generation.assign_description_to_quest(quest, game, grammar)[source]
textworld.generator.text_generation.assign_description_to_room(room, game, grammar)[source]

Assign a descripton to a room.

textworld.generator.text_generation.assign_name_to_object(obj, grammar, game_infos)[source]

Assign a name to an object (if needed).

textworld.generator.text_generation.assign_new_matching_names(obj1_infos, obj2_infos, grammar, exclude=[])[source]
textworld.generator.text_generation.clean_replace_objs(grammar, desc, objs, game_infos)[source]

Return a cleaned/keyword replaced for a list of objects.

textworld.generator.text_generation.describe_event(event, game, grammar)[source]

Assign a descripton to a quest.

Return type:str
textworld.generator.text_generation.expand_clean_replace(symbol, grammar, obj, game_infos)[source]

Return a cleaned/keyword replaced symbol.

textworld.generator.text_generation.generate_instruction(action, grammar, game_infos, world, counts)[source]

Generate text instruction for a specific action.

textworld.generator.text_generation.generate_text_from_grammar(game, grammar)[source]
textworld.generator.text_generation.get_action_chains(actions, grammar, game_infos)[source]

Reduce the action list by combining similar actions.

textworld.generator.text_generation.is_seq(chain, game_infos)[source]

Check if we have a theoretical chain in actions.

textworld.generator.text_generation.list_to_string(lst, det, det_type='a')[source]

Convert a list to a natural language string.

textworld.generator.text_generation.obj_list_to_prop_string(objs, property, game_infos, det=True, det_type='a')[source]

Convert an object list to a nl string list of names.

textworld.generator.text_generation.repl_sing_plur(phrase, length)[source]

Alter a sentence depending on whether or not we are dealing with plural or singular objects (for counting)

textworld.generator.text_generation.replace_num(phrase, val)[source]

Add a numerical value to a string.

class textworld.generator.text_grammar.Grammar(options={}, rng=None)[source]

Bases: object

Context-Free Grammar for text generation.

Parameters:
  • options (Union[GrammarOptions, Mapping[str, Any]]) – For customizing text generation process (see textworld.generator.GrammarOptions for the list of available options).
  • rng (Optional[RandomState]) – Random generator used for sampling tag expansions.
check()[source]

Check if this grammar is valid.

TODO: use logging mechanism to report warnings and errors.

Return type:bool
expand(text, rng=None)[source]

Expand some text until there is no more tag to expand.

Parameters:
  • text (str) – Text potentially containing grammar tags to be expanded.
  • rng (optional) – Random generator used to chose an expansion when there is many. By default, it used the random generator of this grammar object.
Returns:

Resulting text in which there is no grammar tag left to be expanded.

Return type:

expanded_text

generate_name(obj_type, room_type='', include_adj=None, exclude=[])[source]

Generate a name given an object type and the type room it belongs to.

Parameters:
  • obj_type (str) – Type of the object for which we will generate a name.
  • room_type (optional) – Type of the room the object belongs to.
  • include_adj (optional) – If True, the name can contain a generated adjective. If False, any generated adjective will be discarded. Default: use value grammar.options.include_adj
  • exclude (optional) – List of names we should avoid generating.
Return type:

Tuple[str, str, str]

Returns:

  • name – The whole name, i.e. adj + " " + noun.
  • adj – The adjective part of the name.
  • noun – The noun part of the name.

get_all_adjective_for_type(type)[source]

Get all possible adjectives for a given object type.

Parameters:type (str) – Object type.
Returns:All possible adjectives sorted in alphabetical order.
Return type:adjectives
get_all_expansions_for_tag(tag, max_depth=500)[source]

Get all possible expansions for a grammar tag.

Parameters:
  • tag (str) – Grammar tag to be expanded.
  • max_depth (optional) – Maximum recursion depth when expanding tag.
Returns:

All possible expansions.

Return type:

expansions

get_all_expansions_for_type(type)[source]

Get all possible expansions for a given object type.

Parameters:type (str) – Object type.
Returns:All possible names.
Return type:names
get_all_names_for_type(type, include_adj)[source]

Get all possible names for a given object type.

Parameters:
  • type (str) – Object type.
  • include_adj (optional) – If True, names can contain generated adjectives. If False, any generated adjectives will be discarded.
Returns:

All possible names sorted in alphabetical order.

Return type:

names

get_all_nouns_for_type(type)[source]

Get all possible nouns for a given object type.

Parameters:type (str) – Object type.
Returns:All possible nouns sorted in alphabetical order.
Return type:nouns
get_random_expansion(tag, rng=None)[source]

Return a randomly chosen expansion for the given tag.

Parameters:
  • tag (str) – Grammar tag to be expanded.
  • rng (optional) – Random generator used to chose an expansion when there is many. By default, it used the random generator of this grammar object.
Returns:

An expansion chosen randomly for the provided tag.

Return type:

expansion

get_vocabulary()[source]
Return type:List[str]
has_tag(tag)[source]

Check if the grammar has a given tag.

Return type:bool
split_name_adj_noun(candidate, include_adj)[source]

Extract the full name, the adjective and the noun from a string.

Parameters:
  • candidate (str) – String that may contain one adjective-noun sperator ‘|’.
  • include_adj (optional) – If True, the name can contain a generated adjective. If False, any generated adjective will be discarded.
Return type:

Optional[Tuple[str, str, str]]

Returns:

  • name – The whole name, i.e. adj + " " + noun.
  • adj – The adjective part of the name.
  • noun – The noun part of the name.

class textworld.generator.text_grammar.GrammarOptions(options=None, **kwargs)[source]

Bases: object

classmethod deserialize(data)[source]

Creates a GrammarOptions from serialized data.

Parameters:data (Mapping[~KT, +VT_co]) – Serialized data with the needed information to build a GrammarOptions object.
Return type:GrammarOptions
serialize()[source]

Serialize this object.

Results:
GrammarOptions’s data serialized to be JSON compatible.
Return type:Mapping[~KT, +VT_co]
allowed_variables_numbering

bool – Append numbers after an object name if there is not enough variation for it.

ambiguous_instructions

bool – When True, in the game objective, objects of interest might be refer to by their type or adjective rather than full name.

blend_descriptions

bool – When True, objects sharing some properties might be described in a single sentence rather than separate consecutive ones.

blend_instructions

bool – When True, consecutive actions to be accomplished might be described in a single sentence rather than separate ones.

include_adj

bool – When True, object names can be preceeded by an adjective.

names_to_exclude

List[str] – List of names the text generation should not use.

only_last_action

bool – When True, only the last action of a quest will be described in the generated objective.

theme

str – Grammar theme’s name. All *.twg files starting with that name will be loaded.

unique_expansion

bool – When True, #symbol# are force to be expanded to unique text.

uuid

Generate UUID for this set of grammar options.

Return type:str
textworld.generator.text_grammar.fix_determinant(var)[source]