Skip to content

rephrasal_parser

obtain_parser_functions

obtain_parser_functions(
    parser: str | list[str], parser_functions_dict: dict[str, Callable]
) -> list[Callable]

Check if the parser(s) provided are in the parser_functions_dict.

Parameters:

Name Type Description Default
parser str | list[str]

A single parser or a list of parsers to check if they are keys in the parser_functions_dict dictionary

required
parser_functions_dict dict[str, Callable]

A dictionary of parser functions with the keys as the parser names and the values as the parser functions

required

Returns:

Type Description
list[Callable]

List of parser functions that correspond to the parsers

Source code in src/prompto/rephrasal_parser.py
def obtain_parser_functions(
    parser: str | list[str], parser_functions_dict: dict[str, Callable]
) -> list[Callable]:
    """
    Check if the parser(s) provided are in the parser_functions_dict.

    Parameters
    ----------
    parser : str | list[str]
        A single parser or a list of parsers to check if they
        are keys in the parser_functions_dict dictionary
    parser_functions_dict : dict[str, Callable]
        A dictionary of parser functions with the keys as the
        parser names and the values as the parser functions

    Returns
    -------
    list[Callable]
        List of parser functions that correspond to the parsers
    """
    if isinstance(parser, str):
        parser = [parser]

    functions = []
    for p in parser:
        if not isinstance(p, str):
            raise TypeError("If parser is a list, each element must be a string")
        if p not in parser_functions_dict.keys():
            raise KeyError(
                f"Parser '{p}' is not a key in parser_functions_dict. "
                f"Available parsers are: {list(parser_functions_dict.keys())}"
            )

        functions.append(parser_functions_dict[p])

    logging.info(f"parser functions to be used: {parser}")
    return functions