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: fix JSON to YAML conversion #10991

Merged
merged 1 commit into from
Mar 15, 2022
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
1 change: 1 addition & 0 deletions changelog/10675.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix broken conversion from Rasa JSON NLU data to Rasa YAML NLU data.
2 changes: 1 addition & 1 deletion rasa/cli/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def _validate_story_structure(validator: "Validator", args: argparse.Namespace)
def _convert_nlu_data(args: argparse.Namespace) -> None:
import rasa.nlu.convert

if args.format == "json":
if args.format in ["json", "yaml"]:
rasa.nlu.convert.convert_training_data(
args.data, args.out, args.format, args.language
)
Expand Down
14 changes: 3 additions & 11 deletions rasa/nlu/convert.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import argparse
import os
from typing import Text, Union

from rasa.shared.nlu.training_data.formats.rasa_yaml import RasaYAMLWriter
from rasa.shared.utils.cli import print_error
import rasa.shared.nlu.training_data.loading
from rasa.nlu.utils import write_to_file
Expand Down Expand Up @@ -31,18 +31,10 @@ def convert_training_data(
)
return

td = rasa.shared.nlu.training_data.loading.load_data(data_file, language)
if output_format == "json":
td = rasa.shared.nlu.training_data.loading.load_data(data_file, language)
output = td.nlu_as_json(indent=2)
else:
print_error(
"Did not recognize output format. Supported output formats: 'json' and "
"'md'. Specify the desired output format with '--format'."
)
return
output = RasaYAMLWriter().dumps(td)

write_to_file(out_file, output)


def main(args: argparse.Namespace) -> None:
convert_training_data(args.data, args.out, args.format, args.language)
39 changes: 38 additions & 1 deletion tests/cli/test_rasa_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from collections import namedtuple
from typing import Callable, Text

from _pytest.fixtures import FixtureRequest
from _pytest.monkeypatch import MonkeyPatch
from _pytest.pytester import RunResult
from rasa.cli import data
from rasa.shared.importers.importer import TrainingDataImporter
from rasa.shared.nlu.training_data.formats import RasaYAMLReader
from rasa.validator import Validator
import rasa.shared.utils.io

Expand Down Expand Up @@ -53,7 +55,7 @@ def test_data_split_nlu(run_in_simple_project: Callable[..., RunResult]):
assert yml_file.exists(), f"{yml_file} file does not exist"


def test_data_convert_nlu(run_in_simple_project: Callable[..., RunResult]):
def test_data_convert_nlu_json(run_in_simple_project: Callable[..., RunResult]):
run_in_simple_project(
"data",
"convert",
Expand All @@ -69,6 +71,41 @@ def test_data_convert_nlu(run_in_simple_project: Callable[..., RunResult]):
assert os.path.exists("out_nlu_data.json")


def test_data_convert_nlu_yml(
run: Callable[..., RunResult], tmp_path: Path, request: FixtureRequest
):

target_file = tmp_path / "out.yml"

# The request rootdir is required as the `testdir` fixture in `run` changes the
# working directory
test_data_dir = Path(request.config.rootdir, "data", "examples", "rasa")
source_file = (test_data_dir / "demo-rasa.json").absolute()
result = run(
"data",
"convert",
"nlu",
"--data",
str(source_file),
"--out",
str(target_file),
"-f",
"yaml",
)

assert result.ret == 0
assert target_file.exists()

actual_data = RasaYAMLReader().read(target_file)
expected = RasaYAMLReader().read(test_data_dir / "demo-rasa.yml")

assert len(actual_data.training_examples) == len(expected.training_examples)
assert len(actual_data.entity_synonyms) == len(expected.entity_synonyms)
assert len(actual_data.regex_features) == len(expected.regex_features)
assert len(actual_data.lookup_tables) == len(expected.lookup_tables)
assert actual_data.entities == expected.entities


def test_data_split_help(run: Callable[..., RunResult]):
output = run("data", "split", "nlu", "--help")

Expand Down