pyaiml21.graphmaster.graphmaster.GraphMaster¶
- class pyaiml21.graphmaster.graphmaster.GraphMaster(bot_config: pyaiml21.botconfig.config.BotConfig, policy: Union[str, Callable[[Any, Any], Any]] = 'keep_last')[source][source]¶
GraphMaster is a pattern matching data structure.
It stores AIML categories in the tree-like structure (directed word graph/tree) and uses backtracking to find the first match.
Each AIML category is uniquely identified by its pattern path, which consists of the pattern, that and topic elements. The GraphMaster stores the corresponding template node in such a way, that it can be retrieved by the match method iff the input path matches the pattern path.
- Typical order of operations over graphmaster consists of:
- storing categories:
- validate - make sure the category conforms the AIML
specification and/or the available tag
save(category)
retrieving: - realized via match()
Methods
Add pattern tag to the known tags of this graphmaster.
Store the graphmaster into the specified file.
Store graphmaster to html file.
Load the graphmaster from the file, after it was saved using dump.
Load the object given its stored_representation.
Find the matching Pattern Path.
Save the category into the graphmaster.
Store obj and return its stored representation.
Return the number of AIML categories.
Validate the category, check its syntax.
Implement <vocabulary> tag.
Attributes
THATTOPIC- add_pattern_tag(tag: pyaiml21.graphmaster.pattern.Pattern)[source][source]¶
Add pattern tag to the known tags of this graphmaster.
If the same priority tag is present, this tag will be matched first.
- generate_html(filename: str, *args, **kwargs)[source][source]¶
Store graphmaster to html file.
This method can be used for debugging purposes.
Opens the browser with the generated html. It is necessary that pyvis library is also installed.
By default, the graph is directed, 1000 x 1000 px.
- Parameters
filename – name of the file to store generated html into
args – positional arguments passed to pyvis.network.Network
kwargs – keyword arguments passed to pyvis.network.Network
- Returns
the generated graph
- static load(filename: str) pyaiml21.graphmaster.graphmaster.GraphMaster[source][source]¶
Load the graphmaster from the file, after it was saved using dump.
- Parameters
filename – name of the file to load graphmaster from
- Returns
loaded graphmaster
- load_obj(stored_representation: Any) Any[source][source]¶
Load the object given its stored_representation.
See
save_objfor more info.The following should hold for all x:
self.save_obj(self.load_obj(x)) = x self.load_obj(self.save_obj(x)) = x
- Parameters
stored_representation – representation of object stored in the nodemappers
- Returns
the real object represented by stored_representation
- match(input_: str, that_: str, topic_: str) Optional[Tuple[Any, pyaiml21.graphmaster.stars.StarBindings, List[float]]][source][source]¶
Find the matching Pattern Path.
Given input, that (last bot’s response) and topic, construct input path and find the matching category, retrieving the stored object with the matched stars and a sequence of priorities of matching.
All the arguments should be already normalized.
- Parameters
input – user’s input
that – last bot’s response
topic – conversation topic
- Returns
None if no matching category was found, else a tuple with - [0] matched object saved with the best-matching category - [1] star bindings for the matching - [2] a sequence of priorities used to find the matching, can be used to compare different matching-s (the bigger value and sooner, the better match)
- save(category: pyaiml21.ast.category.Category)[source][source]¶
Save the category into the graphmaster.
Traverse the graphmaster according to the pattern path specified by the category, creating new nodes if necessary, and store the template on the specified place, so it can be later retrieved by match.
Successfully calling self.validate is a precondition of correct behaviour of this method.
- Parameters
category – a category used to build a pattern path
- save_obj(obj: Any) Any[source][source]¶
Store obj and return its stored representation.
For example id which will be stored in the memory.
- Parameters
obj – the object that should be stored in the graphmaster
- Returns
representation of the object saved in-memory
- validate(category: pyaiml21.ast.category.Category, logger: pyaiml21.parser.logger.Logger) None[source][source]¶
Validate the category, check its syntax.
Make sure the pattern, that and topic tags contain valid elements. The validation results are stored in the logger instance.
Calling this function successfully (without errors in the logger) is a precondition of storing objects based on this category into this graphmaster.
- Parameters
category – category to validate
logger – logger to log errors or warning
- Returns
nothing, use logger to check for errors
- vocabulary() Set[str][source][source]¶
Implement <vocabulary> tag.
Return a set of words this graphmaster understands (including stars, excluding any word containing underscores).
Approximates <vocabulary/> tag in the sense that it does not count the words in <set>, as it is undefined how many words infinite sets (:number:) have.