Beispiel #1
0
def test_lookup_is_parsed():

    parser = RasaYAMLReader()
    training_data = parser.reads(LOOKUP_EXAMPLE)

    assert training_data.lookup_tables[0]["name"] == LOOKUP_ITEM_NAME
    assert len(training_data.lookup_tables[0]["elements"]) == 3
Beispiel #2
0
def test_synonyms_are_parsed():
    parser = RasaYAMLReader()
    training_data = parser.reads(SYNONYM_EXAMPLE)

    assert len(training_data.entity_synonyms) == 2
    assert training_data.entity_synonyms["pink pig"] == "savings"
    assert training_data.entity_synonyms["savings account"] == "savings"
Beispiel #3
0
def test_regex_is_parsed():

    regex_name = "zipcode"
    pattern_1 = "[0-9]{5}"
    pattern_2 = "[0-9]{4}"

    regex_example = f"""
    nlu:
    - regex: {regex_name}
      examples: |
        - {pattern_1}
        - {pattern_2}
    """

    parser = RasaYAMLReader()
    training_data = parser.reads(regex_example)

    assert len(training_data.regex_features) == 2
    assert {
        "name": regex_name,
        "pattern": pattern_1
    } in training_data.regex_features
    assert {
        "name": regex_name,
        "pattern": pattern_2
    } in training_data.regex_features
Beispiel #4
0
def test_minimal_valid_example():
    parser = RasaYAMLReader()

    with pytest.warns(None) as record:
        parser.reads(MINIMAL_VALID_EXAMPLE)

    assert not len(record)
Beispiel #5
0
def test_no_version_specified_raises_warning():
    parser = RasaYAMLReader()

    with pytest.warns(None) as record:
        parser.reads(EXAMPLE_NO_VERSION_SPECIFIED)

    # warning for the missing version string
    assert len(record) == 1
Beispiel #6
0
def test_regex_is_parsed():

    parser = RasaYAMLReader()
    training_data = parser.reads(REGEX_EXAMPLE)

    assert len(training_data.regex_features) == 2
    assert {"name": REGEX_NAME, "pattern": PATTERN_1} in training_data.regex_features
    assert {"name": REGEX_NAME, "pattern": PATTERN_2} in training_data.regex_features
Beispiel #7
0
def test_nlg_fails_to_read_empty():
    responses_yml = textwrap.dedent("""
      responses:
    """)

    reader = RasaYAMLReader()

    with pytest.raises(ValueError):
        reader.reads(responses_yml)
Beispiel #8
0
def test_wrong_format_raises():

    wrong_yaml_nlu_content = """
    !!
    """

    parser = RasaYAMLReader()
    with pytest.raises(ValueError):
        parser.reads(wrong_yaml_nlu_content)
Beispiel #9
0
def test_multiline_intent_example_is_skipped_when_no_leading_symbol():
    parser = RasaYAMLReader()

    with pytest.warns(None) as record:
        training_data = parser.reads(MULTILINE_INTENT_EXAMPLES_NO_LEADING_SYMBOL)

    assert len(record)

    assert len(training_data.training_examples) == 1
Beispiel #10
0
def test_nlg_fails_on_empty_response():
    responses_yml = textwrap.dedent("""
      responses:
        chitchat/ask_weather:
    """)

    reader = RasaYAMLReader()

    with pytest.raises(InvalidDomain):
        reader.reads(responses_yml)
Beispiel #11
0
def test_multiline_intent_is_parsed(example: Text):
    parser = RasaYAMLReader()

    with pytest.warns(None) as record:
        training_data = parser.reads(example)

    assert not len(record)

    assert len(training_data.training_examples) == 2
    assert training_data.training_examples[0].get(
        INTENT) == training_data.training_examples[1].get(INTENT)
Beispiel #12
0
def test_synonyms_are_parsed():
    synonym_example = """
    nlu:
    - synonym: savings
      examples: |
        - pink pig
        - savings account
    """

    parser = RasaYAMLReader()
    training_data = parser.reads(synonym_example)

    assert len(training_data.entity_synonyms) == 2
    assert training_data.entity_synonyms["pink pig"] == "savings"
    assert training_data.entity_synonyms["savings account"] == "savings"
Beispiel #13
0
def test_nlg_reads_text():
    responses_yml = textwrap.dedent(
        """
      responses:
        chitchat/ask_weather:
        - text: Where do you want to check the weather?
    """
    )

    reader = RasaYAMLReader()
    result = reader.reads(responses_yml)

    assert result.responses == {
        "chitchat/ask_weather": [{"text": "Where do you want to check the weather?"}]
    }
Beispiel #14
0
def test_entity_is_extracted(example: Text, expected_num_entities: int):
    reader = RasaYAMLReader()

    intent_name = "test-intent"

    yaml_string = f"""
nlu:
- intent: {intent_name}
  examples: |
    - {example}
"""

    result = reader.reads(yaml_string)

    assert len(result.training_examples) == 1
    actual_example = result.training_examples[0]
    assert actual_example.data["intent"] == intent_name
    assert len(actual_example.data.get("entities", [])) == expected_num_entities
Beispiel #15
0
def test_lookup_is_parsed():

    lookup_item_name = "additional_currencies"

    lookup_example = f"""
    nlu:
    - lookup: {lookup_item_name}
      examples: |
        - Peso
        - Euro
        - Dollar
    """

    parser = RasaYAMLReader()
    training_data = parser.reads(lookup_example)

    assert training_data.lookup_tables[0]["name"] == lookup_item_name
    assert len(training_data.lookup_tables[0]["elements"]) == 3
Beispiel #16
0
def test_nlg_reads_any_multimedia():
    responses_yml = textwrap.dedent("""
      responses:
        chitchat/ask_weather:
        - text: Where do you want to check the weather?
          image: https://example.com/weather.jpg
          temperature: 25°C
    """)

    reader = RasaYAMLReader()
    result = reader.reads(responses_yml)

    assert result.responses == {
        "chitchat/ask_weather": [{
            "text": "Where do you want to check the weather?",
            "image": "https://example.com/weather.jpg",
            "temperature": "25°C",
        }]
    }
Beispiel #17
0
def test_nlg_multimedia_load_dump_roundtrip():
    responses_yml = textwrap.dedent(
        """
      responses:
        chitchat/ask_weather:
        - text: Where do you want to check the weather?
          image: https://example.com/weather.jpg

        chitchat/ask_name:
        - text: My name is Sara.
    """
    )

    reader = RasaYAMLReader()
    result = reader.reads(responses_yml)

    dumped = RasaYAMLWriter().dumps(result)

    validation_reader = RasaYAMLReader()
    dumped_result = validation_reader.reads(dumped)

    assert dumped_result.responses == result.responses

    # dumping again should also not change the format
    assert dumped == RasaYAMLWriter().dumps(dumped_result)
Beispiel #18
0
def test_minimal_yaml_nlu_file(tmp_path):
    target_file = tmp_path / "test_nlu_file.yaml"
    io_utils.write_yaml(MINIMAL_VALID_EXAMPLE, target_file, True)
    assert RasaYAMLReader.is_yaml_nlu_file(target_file)
Beispiel #19
0
def test_synonyms_are_extracted_from_entities():
    parser = RasaYAMLReader()
    training_data = parser.reads(MULTILINE_INTENT_EXAMPLE_WITH_SYNONYM)

    assert len(training_data.entity_synonyms) == 1
Beispiel #20
0
def test_yaml_examples_are_written(example: Text):
    parser = RasaYAMLReader()
    writer = RasaYAMLWriter()

    training_data = parser.reads(example)
    assert example.strip() == writer.dumps(training_data).strip()
Beispiel #21
0
def test_wrong_schema_raises(example: Text):

    parser = RasaYAMLReader()
    with pytest.raises(ValueError):
        parser.reads(example)