pyaiml21.chatbot.Bot

class pyaiml21.chatbot.Bot(gm_cls: typing.Type[pyaiml21.graphmaster.graphmaster.GraphMaster] = <class 'pyaiml21.graphmaster.graphmaster.GraphMaster'>, *args, **kwargs)[source][source]

Represents an AIML chatbot.

The Bot is the main entity and is capable of responding to the user’s inputs, managing user sessions and loading AIML contents.

This class represents the main entrypoint to work with pyaiml21 interpreter.

Methods

add_properties

Add properties to the bot.

add_substitutions

Update the substitutions of the bot.

brain_size

Find the number of categories stored in the brain.

get_predicate

Find and return the value of a bot's _predicate.

get_property

Return the bot's property value.

get_session

Access arbitrary bot-user session.

init_new_user

Create a chat session for a new user, user must be a new user.

learn_aiml

Load .aiml file and store its contents in the (global) graphmaster.

learn_aimlif

Load .aimlif file and store its contents in the (global) graphmaster.

learn_category

Learn category online, during the evaluation of the response.

load_map

Learn and store AIML map m.

load_patterns

Learn pattern tags.

load_set

Learn and store AIML set s.

load_tags

Learn <template> tags.

preprocess

Normalize the text fed to the graphmaster.

respond

Search graphmaster and return the reply to input_.

search_brain

Search the bot's brain and return the appropriate template node.

vocabulary

Find the number of distinct words stored as nodes in the bot's brain.

Attributes

gm

Bot's 'brain' - the graphmaster global for all users.

sessions

All bot-user sessions of this bot.

normalize_fn

Remove punctuation and split into sentences and each to words.

add_properties(props: Mapping[str, str]) None[source][source]

Add properties to the bot.

Parameters

props – dictionary with bot’s properties

add_substitutions(tag: str, subs: Mapping[str, str]) None[source][source]

Update the substitutions of the bot.

Parameters
  • tag – name of the tag fot substitutions. e.g. normalize, denormalize, gender, person, person2

  • subs – the substitutions

brain_size(user_id: str) int[source][source]

Find the number of categories stored in the brain.

Combines the results with global brain and brain for the specified user. Implements the <size> tag.

Parameters

user_id – the user for whom we should consider the local brain

Returns

number of categories in the brain

get_predicate(name: str, user_id: str) str[source][source]

Find and return the value of a bot’s _predicate.

Parameters
  • name – name of the _predicate

  • user_id – current user we are chatting with

Returns

the _predicate value or self.config.default_predicate

get_session(user_id: str) Optional[pyaiml21.session.session.Session][source][source]

Access arbitrary bot-user session.

gm[source]

Bot’s ‘brain’ - the graphmaster global for all users.

init_new_user(user_id: str) pyaiml21.session.session.Session[source][source]

Create a chat session for a new user, user must be a new user.

learn_aiml(aiml_file: str) pyaiml21.parser.logger.Logger[source][source]

Load .aiml file and store its contents in the (global) graphmaster.

Parameters

aiml_file – name of the .aiml file

Returns

Logger instance with errors or warnings, if the logger has any errors, the filename file was not loaded

learn_aimlif(aimlif_file: str) pyaiml21.parser.logger.Logger[source][source]

Load .aimlif file and store its contents in the (global) graphmaster.

Parameters

aimlif_file – name of the .aimlif file

Returns

Logger instance with errors or warnings, if the logger has any errors, the filename file was not loaded

learn_category(category: str, global_: bool, user_id: str) pyaiml21.parser.logger.Logger[source][source]

Learn category online, during the evaluation of the response.

Parameters
  • category – category to learn, root element is <category>

  • global – if True, learn and save to file global for all users, otherwise only for user with user_id

  • user_id – id of the user we are chatting with

Returns

True on success (parsing reported 0 errors, user exists)

load_map(name: str, m: Mapping[str, str]) None[source][source]

Learn and store AIML map m.

The keys of the map m are expected to be in the uppercase.

load_patterns(patterns: Iterable[pyaiml21.graphmaster.pattern.Pattern]) None[source][source]

Learn pattern tags.

After running this method, the bot will understand and be able to parse and match given pattern within its graphmasters.

Parameters

patterns – a list of objects representing <pattern> nodes

load_set(name: str, s: Set[str]) None[source][source]

Learn and store AIML set s.

The elements of the set s are expected to be in the uppercase.

load_tags(parse_tags: Dict[str, Callable[[pyaiml21.ast.node.Node, pyaiml21.parser.logger.Logger], None]], eval_tags: Dict[str, Callable[[pyaiml21.interpreter.walker.Interpreter, pyaiml21.ast.node.Node], str]], allow_different: bool = False) None[source][source]

Learn <template> tags.

By default, check that the same tags are in both dictionaries, you can override this behaviour by setting allow_different flag.

Parameters
  • parse_tags – dictionary mapping from tag name to the function that parses the tag

  • eval_tags – mapping from name of tag to the function that evaluates the tag

  • allow_different – skip the check that both dictionaries contains the same tags

normalize_fn[source]

Remove punctuation and split into sentences and each to words.

preprocess(text) List[str][source][source]

Normalize the text fed to the graphmaster.

Expand the abbreviations, remove punctuation and split the text into sentences.

Parameters

text – text to be normalized

Returns

normalized sentences

respond(input_: str, user_id: str, debug: bool = False) str[source][source]

Search graphmaster and return the reply to input_.

Parameters
  • user_id – user id for whom to search

  • input – user input to match

  • debug – if true, use interactive debugger Debugger to find the answer

Returns

the bot’s reply

search_brain(user_id: str, sentence: str) Optional[Tuple[pyaiml21.ast.node.Node, pyaiml21.graphmaster.stars.StarBindings, bool]][source][source]

Search the bot’s brain and return the appropriate template node.

Parameters
  • user_id – user from the bot-client conversation

  • sentence – normalized sentence for which to find the template

Returns

None if no match was found, else triple with template node, matched stars and flag signalizing whether the match was exact using $WORD

sessions: Dict[str, pyaiml21.session.session.Session][source]

All bot-user sessions of this bot.

vocabulary(user_id: str) int[source][source]

Find the number of distinct words stored as nodes in the bot’s brain.

The results are combined with global brain and brain for the specified user. Implements the <vocabulary> tag.

Parameters

user_id – the user for whom we should consider the local brain

Returns

number of distinct words