in textworld/generator/text_grammar.py [0:0]
def generate_name(self, obj_type: str, room_type: str = "",
include_adj: Optional[bool] = None, exclude: Container[str] = []) -> Tuple[str, str, str]:
"""
Generate a name given an object type and the type room it belongs to.
Parameters
----------
obj_type :
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.
Returns
-------
name :
The whole name, i.e. `adj + " " + noun`.
adj :
The adjective part of the name.
noun :
The noun part of the name.
"""
if include_adj is None:
include_adj = self.options.include_adj
# Get room-specialized name, if possible.
symbol = "#{}_({})#".format(room_type, obj_type)
if not self.has_tag(symbol):
# Otherwise, fallback on the generic object names.
symbol = "#({})#".format(obj_type)
# We don't want to generate a name that is in `exclude`.
found_candidate = False
for i in range(50): # We default to fifty attempts
candidate = self.expand(symbol)
name, adj, noun = self.split_name_adj_noun(candidate, include_adj)
if name not in exclude:
found_candidate = True
break
if not found_candidate:
# Not enough variation for the object we want to name.
# Warn the user and fall back on adding an adjective if we can.
if not include_adj:
name, adj, noun = self.generate_name(obj_type, room_type, include_adj=True, exclude=exclude)
msg = ("Not enough variation for '{}'. Falling back on using adjective '{}'."
" To avoid this message you can add more variation in the '{}'"
" related grammar files located in '{}'.")
msg = msg.format(symbol, adj, self.theme, KnowledgeBase.default().text_grammars_path)
warnings.warn(msg, textworld.GenerationWarning)
return name, adj, noun
# Still not enough variation for the object we want to name.
if not self.allowed_variables_numbering:
msg = ("Not enough variation for '{}'. You can add more variation"
" in the '{}' related grammar files located in '{}'"
" or turn on the 'include_adj=True' grammar flag."
" In last resort, you could always turn on the"
" 'allowed_variables_numbering=True' grammar flag"
" to append unique number to object name.")
msg = msg.format(symbol, self.theme, KnowledgeBase.default().text_grammars_path)
raise ValueError(msg)
if obj_type not in self.overflow_dict:
self.overflow_dict[obj_type] = []
# Append unique (per type) number to the noun.
suffix = " {}".format(len(self.overflow_dict[obj_type]))
noun += suffix
name += suffix
self.overflow_dict[obj_type].append(name)
return name, adj, noun