def load(cls, target_dir: Optional[str] = None): if target_dir is None: if os.path.isdir("./textworld_data"): target_dir = "./textworld_data" else: target_dir = BUILTIN_DATA_PATH # Load knowledge base related files. paths = glob.glob(pjoin(target_dir, "logic", "*")) logic = GameLogic.load(paths) # Load text generation related files. text_grammars_path = pjoin(target_dir, "text_grammars") return cls(logic, text_grammars_path)
def load(cls, target_dir: Optional[str] = None, logic_path: Optional[str] = None, grammar_path: Optional[str] = None) -> "KnowledgeBase": """ Build a KnowledgeBase from several files (logic and text grammar). Args: target_dir: Folder containing two subfolders: `logic` and `text_grammars`. If provided, both `logic_path` and `grammar_path` are ignored. logic_path: Folder containing `*.twl` files that describe the logic of a game. grammar_path: Folder containing `*.twg` files that describe the grammar used for text generation. Returns: KnowledgeBase object. """ if target_dir: logic_path = pjoin(target_dir, "logic") grammar_path = pjoin(target_dir, "text_grammars") if logic_path is None: logic_path = pjoin(".", "textworld_data", "logic") # Check within working dir. if not os.path.isdir(logic_path): logic_path = LOGIC_DATA_PATH # Default to built-in data. # Load knowledge base related files. paths = glob.glob(pjoin(logic_path, "*.twl")) logic = GameLogic.load(paths) if grammar_path is None: grammar_path = pjoin(".", "textworld_data", "text_grammars") # Check within working dir. if not os.path.isdir(grammar_path): grammar_path = TEXT_GRAMMARS_PATH # Default to built-in data. # Load text generation related files. kb = cls(logic, grammar_path) kb.logic_path = logic_path return kb
def load_logic(target_dir: str): global _LOGIC if _LOGIC: return paths = [pjoin(target_dir, path) for path in os.listdir(target_dir)] logic = GameLogic.load(paths) global _TYPES _TYPES = _to_type_tree(logic.types) global _RULES _RULES = _to_regex_dict(logic.rules.values()) global _REVERSE_RULES _REVERSE_RULES = _to_reverse_mapper(_RULES, logic.reverse_rules) global _CONSTRAINTS _CONSTRAINTS = _to_regex_dict(logic.constraints.values()) global INFORM7_COMMANDS INFORM7_COMMANDS = {i7cmd.rule: i7cmd.command for i7cmd in logic.inform7.commands.values()} global INFORM7_EVENTS INFORM7_EVENTS = {i7cmd.rule: i7cmd.event for i7cmd in logic.inform7.commands.values()} global INFORM7_PREDICATES INFORM7_PREDICATES = {i7pred.predicate.signature: (i7pred.predicate, i7pred.source) for i7pred in logic.inform7.predicates.values()} global INFORM7_VARIABLES INFORM7_VARIABLES = {i7type.name: i7type.kind for i7type in logic.inform7.types.values()} global INFORM7_VARIABLES_DESCRIPTION INFORM7_VARIABLES_DESCRIPTION = {i7type.name: i7type.definition for i7type in logic.inform7.types.values()} global INFORM7_ADDONS_CODE INFORM7_ADDONS_CODE = logic.inform7.code _LOGIC = logic