Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix comment exploding autoconfig #6952

Merged
merged 6 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions data/test_config/config_with_comment_between_suggestions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
language: en
pipeline:
# # No configuration for the NLU pipeline was provided. The following default pipeline was used to train your model.
# # If you'd like to customize it, uncomment and adjust the pipeline.
# # See https://rasa.com/docs/rasa/tuning-your-model for more information.
# - name: WhitespaceTokenizer
# - name: RegexFeaturizer
# - name: LexicalSyntacticFeaturizer
# - name: CountVectorsFeaturizer
# - name: CountVectorsFeaturizer
# analyzer: char_wb
# min_ngram: 1
# max_ngram: 4
# - name: DIETClassifier
# epochs: 100
# - name: EntitySynonymMapper
# - name: ResponseSelector
# epochs: 100
# - name: FallbackClassifier
# threshold: 0.3
# ambiguity_threshold: 0.1
# some other comment in here
policies:
# # No configuration for policies was provided. The following default policies were used to train your model.
# # If you'd like to customize them, uncomment and adjust the policies.
# # See https://rasa.com/docs/rasa/policies for more information.
# - name: MemoizationPolicy
# - name: TEDPolicy
# max_history: 5
# epochs: 100
# - name: RulePolicy
17 changes: 9 additions & 8 deletions rasa/shared/importers/autoconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
from enum import Enum
from pathlib import Path
from typing import Text, Dict, Any, List, Set, Optional

import rasa.shared.constants
Expand Down Expand Up @@ -165,14 +166,14 @@ def _dump_config(

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)
current_config_content = rasa.shared.utils.io.read_file(config_file_path)
current_config_lines = current_config_content.splitlines(keepends=True)

updated_lines = _get_lines_including_autoconfig(
current_config_lines, autoconfig_lines
)

rasa.shared.utils.io.write_text_file("".join(updated_lines), config_file_path)

auto_configured_keys = rasa.shared.utils.common.transform_collection_to_sentence(
auto_configured_keys
Expand Down
16 changes: 16 additions & 0 deletions tests/shared/importers/test_autoconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,19 @@ def test_get_configuration_for_different_training_types(
expected = rasa.shared.utils.io.read_file(str(CONFIG_FOLDER / expected_file))

assert actual == expected


def test_comment_causing_invalid_autoconfig(tmp_path: Path):
"""Regression test for https://github.com/RasaHQ/rasa/issues/6948."""

config_file = tmp_path / "config.yml"
shutil.copyfile(
str(CONFIG_FOLDER / "config_with_comment_between_suggestions.yml"), config_file
)

_ = autoconfig.get_configuration(str(config_file))

# This should not throw
dumped = rasa.shared.utils.io.read_yaml_file(config_file)

assert dumped