예제 #1
0
def _dump_config(
    config: Dict[Text, Any],
    config_file_path: Text,
    missing_keys: Set[Text],
    auto_configured_keys: Set[Text],
    training_type: Optional[TrainingType] = TrainingType.BOTH,
) -> None:
    """Dump the automatically configured keys into the config file.

    The configuration provided in the file is kept as it is (preserving the order of
    keys and comments).
    For keys that were automatically configured, an explanatory comment is added and the
    automatically chosen configuration is added commented-out.
    If there are already blocks with comments from a previous auto configuration run,
    they are replaced with the new auto configuration.

    Args:
        config: The configuration including the automatically configured keys.
        config_file_path: The file into which the configuration should be dumped.
        missing_keys: Keys that need to be added to the config file.
        auto_configured_keys: Keys for which a commented out auto configuration section
                              needs to be added to the config file.
        training_type: NLU, CORE or BOTH depending on which is trained.
    """

    config_as_expected = _is_config_file_as_expected(config_file_path,
                                                     missing_keys,
                                                     auto_configured_keys,
                                                     training_type)
    if not config_as_expected:
        cli_utils.print_error(
            f"The configuration file at '{config_file_path}' has been removed or "
            f"modified while the automatic configuration was running. The current "
            f"configuration will therefore not be dumped to the file. If you want to "
            f"your model to use the configuration provided in '{config_file_path}', "
            f"you need to re-run training.")
        return

    _add_missing_config_keys_to_file(config_file_path, missing_keys)

    autoconfig_lines = _get_commented_out_autoconfig_lines(
        config, auto_configured_keys)

    with open(config_file_path,
              "r+",
              encoding=rasa.shared.utils.io.DEFAULT_ENCODING) as f:
        lines = f.readlines()
        updated_lines = _get_lines_including_autoconfig(
            lines, autoconfig_lines)
        f.seek(0)
        for line in updated_lines:
            f.write(line)

    auto_configured_keys = common_utils.transform_collection_to_sentence(
        auto_configured_keys)
    cli_utils.print_info(
        f"The configuration for {auto_configured_keys} was chosen automatically. It "
        f"was written into the config file at '{config_file_path}'.")
예제 #2
0
def test_transform_collection_to_sentence(
    collection: Collection, possible_outputs: List[Text]
):
    actual = transform_collection_to_sentence(collection)
    assert actual in possible_outputs