pyaiml21.parser.parser.AIMLParser¶
- class pyaiml21.parser.parser.AIMLParser[source][source]¶
Allows parsing XML files into tree-like objects.
AIMLParser represents a parsing phase when loading AIML categories into the interpreter. Loads file, parses it as a XML file (ignoring all AIML syntax rules), and finds and returns the basic unit of knowledge, category elements, which are then validated.
To enable the parser to validate other tags, use add_tag to pass the name of the tag and parsing function.
Note: As category elements might be wrapped with <topic>, AIMLParser tries to correctly assign child node <topic> to all categories wrapped, i.e.
<topic name="..."> <category>..</category> </topic>
is transformed, internally, into
<category> ... <topic>topic_name</topic> </category>
Methods
Enable the parser to correctly validate the <name> tag.
Parse .aiml file given by the filename.
Parse AIML content from the text.
Parse .aimlif file given by the filename.
Parse one <category> from the text.
- add_tag(name: str, fn: Callable[[pyaiml21.ast.node.Node, pyaiml21.parser.logger.Logger], None]) None[source][source]¶
Enable the parser to correctly validate the <name> tag.
- parse_aiml_file(filename: str) Tuple[Optional[List[pyaiml21.ast.category.Category]], pyaiml21.parser.logger.Logger][source][source]¶
Parse .aiml file given by the filename.
.aiml file is an XML file containing AIML elements, and <aiml> as the root element. Any syntactic errors are reported via the returned logger.
- Parameters
filename – name of file to parse (.aiml)
- Returns
list of categories and the logger with error messages; optionally the list of categories might be None, in this case the logger contains at least one error
- parse_aiml_text(text: str) Tuple[Optional[List[pyaiml21.ast.category.Category]], pyaiml21.parser.logger.Logger][source][source]¶
Parse AIML content from the text.
The requirements on the text content are similar to the ones presented in the parse_aiml_text method.
- Parameters
text – string with AIML content
- Returns
list of categories and the logger with error messages; optionally the list of categories might be None, in this case the logger contains at least one error
- parse_aimlif_file(filename: str) Tuple[Optional[List[pyaiml21.ast.category.Category]], pyaiml21.parser.logger.Logger][source][source]¶
Parse .aimlif file given by the filename.
The AIMLIF format is an alternative format to represent AIML files, where each category is stored on a single line. The elements are separated with a comma, in this order:
activation count, pattern, that, topic, template, filename
- Notes:
this method completely ignores activation count as it does not seem to have any purpose;
the filename present on the line is also ignored;
- Parameters
filename – .aimlif file to parse
- Returns
list of categories and the logger with error messages; optionally the list of categories might be None, in this case the logger contains at least one error
- parse_category_aiml(text: str) Tuple[Optional[pyaiml21.ast.category.Category], pyaiml21.parser.logger.Logger][source][source]¶
Parse one <category> from the text.
The only requirement is that <category> is the root element and text is well-formed xml document.
- Parameters
text – string with <category> element
- Returns
parsed category and the logger with reported errors, optionally the category might be None, in this case the logger contains at least one error