pyaiml21.botconfig.substitutions.Substitutions

class pyaiml21.botconfig.substitutions.Substitutions[source][source]

A dict-like object that enables doing fast substitutions.

It represents a mapping from keys to values done at once (without having to care about overlapping words).

The implementation was adopted from PyAIML, the original AIML 1.x interpreter written in Python by Cort Stratton:

https://github.com/cdwfs/pyaiml/blob/master/aiml/WordSub.py

Methods

sub

Perform the substitutions.

sub(text: str) str[source][source]

Perform the substitutions.

Replace any substring of text equal to any key from the dictionary with the corresponding value. The replacement is done case-insensitive.

Parameters

text – text to be substituted

Returns

text with replaced substrings

Example:
>>> s = Substitutions()
>>> s.sub("Nothing happens") == "Nothing happens"
True
>>> s["dog"] = "cat"
>>> s["cat"] = "dog"
>>> s["lama"] = "spaRRow"
>>> s.sub("A dog met a lama") == "A cat met a spaRRow"
True
>>> s[" car "] = "bike"
>>> s.sub("I have a car you dont") == "I have abikeyou dont"
True