pyaiml21.interpreter.walker.Interpreter

class pyaiml21.interpreter.walker.Interpreter(bot: pyaiml21.bot._bot.Bot, session: pyaiml21.session.session.Session)[source][source]

A tree-walking interpreter used to evaluate the AIML <template> tags.

Recursively evaluate the bot’s reply, by using defined evaluation rules given in known_tags. To update these, feel free to use method register.

A new interpreter instance should be created for each user-chatbot message.

If there is no answer found for the given input, the interpreter returns the value of bot’s _predicate “default”.

Methods

add_frame

Add stack frame for current <template>.

call

Find the response for the input given by input_.

enter

Call when starting the evaluation of node.

eval

Evaluate node and return the result.

eval_seq

Evaluate a sequence of nodes, concatenating the results.

eval_unknown

Evaluate unknown tag by keeping the xml in the result.

fail

Interrupt the evaluation of the response, returning result.

fail_node

Announce the interpreter that evaluation of node failed.

leave

Call when the evaluation of node finished with result.

pop_frame

Pop stack frame when leaving current <template>.

register

Register a function used to interpret AIML, possibly overriding.

Attributes

stars

Access stars for current category.

vars

Access local variables for current category.

add_frame(_pattern: str, stars: pyaiml21.graphmaster.stars.StarBindings)[source][source]

Add stack frame for current <template>.

call(input_: str) str[source][source]

Find the response for the input given by input_.

enter(_node: pyaiml21.ast.node.Node)[source][source]

Call when starting the evaluation of node.

eval(node: pyaiml21.ast.node.Node) str[source][source]

Evaluate node and return the result.

Use self.known_tags to find the proper evaluation function.

Parameters

node – AST node to be evaluated

Returns

evaluated string

eval_seq(seq: List[pyaiml21.ast.node.Node]) str[source][source]

Evaluate a sequence of nodes, concatenating the results.

eval_unknown(node: pyaiml21.ast.node.Node) str[source][source]

Evaluate unknown tag by keeping the xml in the result.

fail(result: str = '')[source][source]

Interrupt the evaluation of the response, returning result.

fail_node(node: pyaiml21.ast.node.Node) str[source][source]

Announce the interpreter that evaluation of node failed.

During evaluation, this method is called whenever an assertion should fail (index is not decimal, e.g.). To resolve the situation, it first tries to call {tag}FAILED, if not successful, the interpreter returns predefined constant.

leave(_node: pyaiml21.ast.node.Node, _result: str)[source][source]

Call when the evaluation of node finished with result.

pop_frame()[source][source]

Pop stack frame when leaving current <template>.

register(tag: str, fn: Callable[[pyaiml21.interpreter.walker.Interpreter, pyaiml21.ast.node.Node], str])[source][source]

Register a function used to interpret AIML, possibly overriding.

property stars: pyaiml21.graphmaster.stars.StarBindings[source]

Access stars for current category.

property vars: pyaiml21.interpreter.symbol_table.SymbolTable[source]

Access local variables for current category.