nemoguardrails.colang.v1_0.lang.comd_parser

View as Markdown

Module Contents

Functions

NameDescription
_get_param_typeHelper to return the type of a parameter.
_get_symbol_typeHelper to determine if a symbol is prefixed with its type.
_get_typed_symbol_nameReturns the symbol name prefixed with the type, if not already.
_record_utteranceHelper to record an utterance in the .md parsing result.
parse_md_fileParse a Markdown file for patterns.
parse_md_langReturns the language of the .md file.
parse_patternParses a pattern from the Markdown-friendly format to an internal format.

Data

SYMBOL_TYPES

__all__

API

nemoguardrails.colang.v1_0.lang.comd_parser._get_param_type(
type_str
)

Helper to return the type of a parameter.

For now we use a simple heuristic:

  1. If it’s a primitive type, we leave it as such.

  2. If it already has some sort of prefix with ”:”, we leave it as such.

  3. If it starts with lower case and it’s not one of the primitive types then we map it to “type:…”

  4. If it starts with an upper case, then we map it to an “object:…”

:param type_str: A string representing the type. :return: The actual type.

nemoguardrails.colang.v1_0.lang.comd_parser._get_symbol_type(
sym
)

Helper to determine if a symbol is prefixed with its type.

:param sym: The name of the symbol.

nemoguardrails.colang.v1_0.lang.comd_parser._get_typed_symbol_name(
sym: str,
symbol_type: str
)

Returns the symbol name prefixed with the type, if not already.

nemoguardrails.colang.v1_0.lang.comd_parser._record_utterance(
result: dict,
sym: str,
symbol_params: list,
symbol_context: typing.Optional[str],
symbol_meta: dict,
symbol_context_meta: dict,
data: typing.Union[str, dict]
)

Helper to record an utterance in the .md parsing result.

It supports both string utterances and rich utterances.

:param result: The result to append the utterance to. :param sym: The current symbol e.g. “utterance:welcome” :param symbol_params: Any additional symbol parameters. It is an array like [“$role=admin”, “channel.type=messenger”] :param symbol_context: An additional contextual expression that must be evaluated to True/False. :param symbol_meta: Meta information for the symbol in general. :param symbol_meta: Meta information for the symbol in this context. :param data: The data for the utterance, either string or something “rich” :return:

nemoguardrails.colang.v1_0.lang.comd_parser.parse_md_file(
file_name,
content = None
)

Parse a Markdown file for patterns.

The content can be also passed as a parameter to skip reading it.

:param file_name: A markdown file :param content: The content of the file. :return: A list of patterns.

nemoguardrails.colang.v1_0.lang.comd_parser.parse_md_lang(
file_name,
content = None
)

Returns the language of the .md file.

The content can be also passed as a parameter to skip reading it.

It searches for lang: XX in a yaml code block. By default it assumes the language is English.

nemoguardrails.colang.v1_0.lang.comd_parser.parse_pattern(
pattern
)

Parses a pattern from the Markdown-friendly format to an internal format. E.g. parse_pattern(“show me the deals I’ve won”) = (show me the deals I’ve {deal__status}, {‘user’: ‘CURRENT’}).

For patters with “assignment patterns” like “show me the deals” we transform it into: “show {user=CURRENT} the deals” with the mapping: { “user=CURRENT”: “me” }

:param pattern: The pattern in Markdown-friendly format. :return: A tuple (pattern, params) where pattern is a pattern containing only text and {capture} tokens, params is a dict of ‘implicit’ parameter values.

nemoguardrails.colang.v1_0.lang.comd_parser.SYMBOL_TYPES = ['intent', 'object', 'type', 'property', 'utterance', 'sym', 'lookup']
nemoguardrails.colang.v1_0.lang.comd_parser.__all__ = ['parse_md_file']