PythonKnowledgeBase

KnowledgeBase

Share immutable knowledge across parsers, transform engines, and documents.

KnowledgeBaseclass#

Immutable knowledge shared by parsers, engines, and documents.

A knowledge base defines the known commands, environments, characters, and delimiters that parsing, transforms, and Document construction and edits check against. Share one instance across a Parser, TransformEngine, and their documents.

Equality and hashing use instance identity: separately constructed knowledge bases are distinct even with identical recipes. Query results are copies. packages=None loads the default runtime packages; [] loads none.

Python
kb = texform.KnowledgeBase(["ams"])
doc = texform.Parser(kb).parse(r"\frac{a}{b}")["document"]

KnowledgeBase#

KnowledgeBase(    packages: list[str] | None = None,    items: list[ContextItem] | None = None,    remove_commands: list[str] | None = None,    remove_environments: list[str] | None = None,    remove_delimiter_controls: list[str] | None = None,) -> None

Build the knowledge base once.

ParameterTypeDefaultDescription
packageslist[str] | NoneNone

Package names to load, or None for the defaults.

itemslist[ContextItem] | NoneNone

Extra command, environment, or delimiter items to add.

remove_commandslist[str] | NoneNone

Command names to remove after loading.

remove_environmentslist[str] | NoneNone

Environment names to remove after loading.

remove_delimiter_controlslist[str] | NoneNone

Delimiter control names to remove after loading.

Raises
  • ConfigError —

    If a package name or an item is invalid.

__eq__#

__eq__(other: object) -> bool
ParameterType
otherobject
Returns
bool

__hash__#

__hash__() -> int
Returns
int

packages#

packages() -> list[str]

Return the loaded package names in canonical order.

Returns
list[str]

commands#

commands(mode: Literal['math', 'text']) -> list[dict[str, Any]]

Return the command records for mode, sorted by name.

ParameterType
modeLiteral['math', 'text']
Returns
list[dict[str, Any]]

environments#

environments(mode: Literal['math', 'text']) -> list[dict[str, Any]]

Return the environment records for mode, sorted by name.

ParameterType
modeLiteral['math', 'text']
Returns
list[dict[str, Any]]

characters#

characters(mode: Literal['math', 'text']) -> list[dict[str, Any]]

Return the character records for mode, sorted by name.

ParameterType
modeLiteral['math', 'text']
Returns
list[dict[str, Any]]

delimiters#

delimiters() -> list[dict[str, Any]]

Return delimiter records sorted by name.

Returns
list[dict[str, Any]]

lookup_command#

lookup_command(    name: str,    mode: Literal['math', 'text'],) -> dict[str, Any] | None

Look up the full knowledge entry for a command in a given mode.

ParameterTypeDescription
namestr

The command name without the leading backslash.

modeLiteral['math', 'text']

"math" or "text".

Returns
dict[str, Any] | None —

A dict describing the command, including its parsed args slots, or None if the command is unknown in that mode.

lookup_explicit_command#

lookup_explicit_command(    name: str,    mode: Literal['math', 'text'],) -> dict[str, Any] | None

Look up the knowledge entry for an explicit command in a given mode.

ParameterTypeDescription
namestr

The command name without the leading backslash.

modeLiteral['math', 'text']

"math" or "text".

Returns
dict[str, Any] | None —

A knowledge-entry dict, or None if unknown in that mode.

lookup_character#

lookup_character(    name: str,    mode: Literal['math', 'text'],) -> dict[str, Any] | None

Look up the knowledge entry for a character in a given mode.

ParameterTypeDescription
namestr

The character name without the leading backslash.

modeLiteral['math', 'text']

"math" or "text".

Returns
dict[str, Any] | None —

A knowledge-entry dict, or None if unknown in that mode.

lookup_env#

lookup_env(    name: str,    mode: Literal['math', 'text'],) -> dict[str, Any] | None

Look up the knowledge entry for an environment in a given mode.

ParameterTypeDescription
namestr

The environment name.

modeLiteral['math', 'text']

"math" or "text".

Returns
dict[str, Any] | None —

A knowledge-entry dict, or None if unknown in that mode.

is_delimiter_control#

is_delimiter_control(name: str) -> bool

Report whether a name is a delimiter control such as langle.

ParameterTypeDescription
namestr

The control name without the leading backslash.

Returns
bool —

True if the name is a known delimiter control.

Python
texform.KnowledgeBase().is_delimiter_control("langle")  # True

knows_command_name#

knows_command_name(name: str) -> bool

Report whether a command name is known, ignoring mode.

ParameterTypeDescription
namestr

The command name without the leading backslash.

Returns
bool —

True if the command is known in any mode.

knows_env_name#

knows_env_name(name: str) -> bool

Report whether an environment name is known, ignoring mode.

ParameterTypeDescription
namestr

The environment name.

Returns
bool —

True if the environment is known in any mode.

knows_character_name#

knows_character_name(name: str) -> bool

Report whether a character name is known, ignoring mode.

ParameterTypeDescription
namestr

The character name without the leading backslash.

Returns
bool —

True if the character is known in any mode.

CommandItemdict type#

A custom command injected into parser knowledge.

FieldTypeDescription
targetLiteral['command']

Discriminator; always "command".

namestr

Command name without the leading backslash.

kindLiteral['prefix', 'infix', 'declarative']

Syntactic role: "prefix", "infix", or "declarative".

allowed_modeLiteral['math', 'text', 'both']

Modes in which the command is valid: "math", "text", or "both".

argspecstr

xparse-style argument specification, such as "m m" or "o m".

tagsNotRequired[list[str]]

Optional free-form classification tags carried into the knowledge entry.

EnvironmentItemdict type#

A custom environment injected into parser knowledge.

FieldTypeDescription
targetLiteral['environment']

Discriminator; always "environment".

namestr

Environment name as written in \begin{...}.

allowed_modeLiteral['math', 'text', 'both']

Modes in which the environment may appear.

body_modeLiteral['math', 'text']

Content mode of the environment body ("math" or "text").

argspecstr

xparse-style argument specification for the environment header.

tagsNotRequired[list[str]]

Optional free-form classification tags.

DelimiterItemdict type#

A custom delimiter control injected into parser knowledge.

FieldTypeDescription
targetLiteral['delimiter']

Discriminator; always "delimiter".

namestr

Delimiter-control name without the leading backslash (for example "langle").