dc6079821b
Docs Tests / Check for file changes (push) Has been cancelled
Docs Tests / Test Documentation (push) Has been cancelled
Docs Tests / Documentation Linting Checks (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.10, test-performance) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.10, test-policies) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.8, test-cli) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.8, test-core-featurizers) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.8, test-full-model-training) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.8, test-nlu-featurizers) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.8, test-nlu-predictors) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.8, test-other-unit-tests) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.8, test-performance) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.8, test-policies) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.9, test-cli) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.9, test-core-featurizers) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.9, test-full-model-training) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.9, test-nlu-featurizers) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.9, test-nlu-predictors) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.9, test-other-unit-tests) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.9, test-performance) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.9, test-policies) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.10, test-cli) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.10, test-core-featurizers) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.10, test-full-model-training) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.10, test-nlu-featurizers) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.10, test-nlu-predictors) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.10, test-other-unit-tests) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.10, test-performance) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.10, test-policies) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.8, test-cli) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.8, test-core-featurizers) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.8, test-full-model-training) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.8, test-nlu-featurizers) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.8, test-nlu-predictors) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.8, test-other-unit-tests) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.8, test-performance) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.8, test-policies) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.9, test-cli) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.9, test-core-featurizers) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.9, test-full-model-training) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.9, test-nlu-featurizers) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.9, test-nlu-predictors) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.9, test-other-unit-tests) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.9, test-performance) (push) Has been cancelled
Continuous Integration / Run Tests (windows-2022, 3.9, test-policies) (push) Has been cancelled
Continuous Integration / Run Flaky Tests (ubuntu-24.04, 3.10) (push) Has been cancelled
Continuous Integration / Run Flaky Tests (ubuntu-24.04, 3.8) (push) Has been cancelled
Continuous Integration / Run Flaky Tests (ubuntu-24.04, 3.9) (push) Has been cancelled
Continuous Integration / Run Flaky Tests (windows-2022, 3.10) (push) Has been cancelled
Continuous Integration / Run Flaky Tests (windows-2022, 3.8) (push) Has been cancelled
Continuous Integration / Run Flaky Tests (windows-2022, 3.9) (push) Has been cancelled
Continuous Integration / Check for file changes (push) Has been cancelled
Continuous Integration / Wait for docs tests (push) Has been cancelled
Continuous Integration / Code Quality (push) Has been cancelled
Continuous Integration / Check for changelog (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.10, test-cli) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.10, test-core-featurizers) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.10, test-full-model-training) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.10, test-nlu-featurizers) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.10, test-nlu-predictors) (push) Has been cancelled
Continuous Integration / Run Tests (ubuntu-24.04, 3.10, test-other-unit-tests) (push) Has been cancelled
Continuous Integration / Upload coverage reports to codeclimate (push) Has been cancelled
Continuous Integration / Run Non-Sequential Integration Tests (push) Has been cancelled
Continuous Integration / Run Broker Integration Tests (push) Has been cancelled
Continuous Integration / Run Sequential Integration Tests (push) Has been cancelled
Continuous Integration / Build Docker base images and setup environment (push) Has been cancelled
Continuous Integration / Build Docker (default) (push) Has been cancelled
Continuous Integration / Build Docker (full) (push) Has been cancelled
Continuous Integration / Build Docker (mitie-en) (push) Has been cancelled
Continuous Integration / Build Docker (spacy-de) (push) Has been cancelled
Continuous Integration / Build Docker (spacy-en) (push) Has been cancelled
Continuous Integration / Build Docker (spacy-it) (push) Has been cancelled
Continuous Integration / Deploy to PyPI (push) Has been cancelled
Continuous Integration / Notify Slack & Publish Release Notes (push) Has been cancelled
Publish Documentation / Evaluate release tag (push) Has been cancelled
Publish Documentation / Prebuild Docs (push) Has been cancelled
Publish Documentation / Preview Docs (push) Has been cancelled
Publish Documentation / Check for file changes (push) Has been cancelled
Publish Documentation / Publish Docs (push) Has been cancelled
Automatic PR Merger / mergepal (push) Has been cancelled
CI Github Actions / Run Tests (push) Has been cancelled
Semgrep / Semgrep Workflow Security Scan (push) Has been cancelled
221 lines
6.6 KiB
Python
221 lines
6.6 KiB
Python
from typing import Dict, List, Text
|
|
|
|
import pytest
|
|
|
|
import rasa.nlu.utils.pattern_utils as pattern_utils
|
|
from rasa.shared.nlu.training_data.training_data import TrainingData
|
|
from rasa.shared.nlu.training_data.message import Message
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"lookup_tables, regex_features, expected_patterns",
|
|
[
|
|
(
|
|
{"name": "person", "elements": ["Max", "John"]},
|
|
{},
|
|
[{"name": "person", "pattern": "(\\bMax\\b|\\bJohn\\b)"}],
|
|
),
|
|
({}, {}, []),
|
|
(
|
|
{},
|
|
{"name": "zipcode", "pattern": "[0-9]{5}"},
|
|
[{"name": "zipcode", "pattern": "[0-9]{5}"}],
|
|
),
|
|
(
|
|
{"name": "person", "elements": ["Max", "John"]},
|
|
{"name": "zipcode", "pattern": "[0-9]{5}"},
|
|
[
|
|
{"name": "zipcode", "pattern": "[0-9]{5}"},
|
|
{"name": "person", "pattern": "(\\bMax\\b|\\bJohn\\b)"},
|
|
],
|
|
),
|
|
(
|
|
{"name": "plates", "elements": "data/test/lookup_tables/plates.txt"},
|
|
{"name": "zipcode", "pattern": "[0-9]{5}"},
|
|
[
|
|
{"name": "zipcode", "pattern": "[0-9]{5}"},
|
|
{
|
|
"name": "plates",
|
|
"pattern": "(\\btacos\\b|\\bbeef\\b|\\bmapo\\ "
|
|
"tofu\\b|\\bburrito\\b|\\blettuce\\ wrap\\b)",
|
|
},
|
|
],
|
|
),
|
|
],
|
|
)
|
|
def test_extract_patterns(
|
|
lookup_tables: Dict[Text, List[Text]],
|
|
regex_features: Dict[Text, Text],
|
|
expected_patterns: Dict[Text, Text],
|
|
):
|
|
training_data = TrainingData()
|
|
if lookup_tables:
|
|
training_data.lookup_tables = [lookup_tables]
|
|
if regex_features:
|
|
training_data.regex_features = [regex_features]
|
|
|
|
actual_patterns = pattern_utils.extract_patterns(training_data)
|
|
|
|
assert actual_patterns == expected_patterns
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"entity, regex_features, expected_patterns",
|
|
[
|
|
("", {}, []),
|
|
(
|
|
"zipcode",
|
|
{"name": "zipcode", "pattern": "[0-9]{5}"},
|
|
[{"name": "zipcode", "pattern": "[0-9]{5}"}],
|
|
),
|
|
("entity", {"name": "zipcode", "pattern": "[0-9]{5}"}, []),
|
|
],
|
|
)
|
|
def test_extract_patterns_use_only_entities_regexes(
|
|
entity: Text, regex_features: Dict[Text, Text], expected_patterns: Dict[Text, Text]
|
|
):
|
|
training_data = TrainingData()
|
|
if entity:
|
|
training_data.training_examples = [
|
|
Message(
|
|
data={
|
|
"text": "text",
|
|
"intent": "greet",
|
|
"entities": [{"entity": entity, "value": "text"}],
|
|
}
|
|
)
|
|
]
|
|
if regex_features:
|
|
training_data.regex_features = [regex_features]
|
|
|
|
actual_patterns = pattern_utils.extract_patterns(
|
|
training_data, use_only_entities=True
|
|
)
|
|
|
|
assert actual_patterns == expected_patterns
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"entity, lookup_tables, expected_patterns",
|
|
[
|
|
("", {}, []),
|
|
(
|
|
"person",
|
|
{"name": "person", "elements": ["Max", "John"]},
|
|
[{"name": "person", "pattern": "(\\bMax\\b|\\bJohn\\b)"}],
|
|
),
|
|
("entity", {"name": "person", "elements": ["Max", "John"]}, []),
|
|
],
|
|
)
|
|
def test_extract_patterns_use_only_entities_lookup_tables(
|
|
entity: Text, lookup_tables: Dict[Text, Text], expected_patterns: Dict[Text, Text]
|
|
):
|
|
training_data = TrainingData()
|
|
if entity:
|
|
training_data.training_examples = [
|
|
Message(
|
|
data={
|
|
"text": "text",
|
|
"intent": "greet",
|
|
"entities": [{"entity": entity, "value": "text"}],
|
|
}
|
|
)
|
|
]
|
|
if lookup_tables:
|
|
training_data.lookup_tables = [lookup_tables]
|
|
|
|
actual_patterns = pattern_utils.extract_patterns(
|
|
training_data, use_only_entities=True
|
|
)
|
|
|
|
assert actual_patterns == expected_patterns
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"lookup_tables, regex_features, use_lookup_tables, "
|
|
"use_regex_features, expected_patterns",
|
|
[
|
|
({"name": "person", "elements": ["Max", "John"]}, {}, False, True, []),
|
|
({}, {}, True, True, []),
|
|
({}, {"name": "zipcode", "pattern": "[0-9]{5}"}, True, False, []),
|
|
(
|
|
{"name": "person", "elements": ["Max", "John"]},
|
|
{"name": "zipcode", "pattern": "[0-9]{5}"},
|
|
False,
|
|
False,
|
|
[],
|
|
),
|
|
(
|
|
{"name": "person", "elements": ["Max", "John"]},
|
|
{"name": "zipcode", "pattern": "[0-9]{5}"},
|
|
True,
|
|
False,
|
|
[{"name": "person", "pattern": "(\\bMax\\b|\\bJohn\\b)"}],
|
|
),
|
|
(
|
|
{"name": "person", "elements": ["Max", "John"]},
|
|
{"name": "zipcode", "pattern": "[0-9]{5}"},
|
|
False,
|
|
True,
|
|
[{"name": "zipcode", "pattern": "[0-9]{5}"}],
|
|
),
|
|
],
|
|
)
|
|
def test_extract_patterns_use_only_lookup_tables_or_regex_features(
|
|
lookup_tables: Dict[Text, List[Text]],
|
|
regex_features: Dict[Text, Text],
|
|
use_lookup_tables: bool,
|
|
use_regex_features: bool,
|
|
expected_patterns: Dict[Text, Text],
|
|
):
|
|
training_data = TrainingData()
|
|
if lookup_tables:
|
|
training_data.lookup_tables = [lookup_tables]
|
|
if regex_features:
|
|
training_data.regex_features = [regex_features]
|
|
|
|
actual_patterns = pattern_utils.extract_patterns(
|
|
training_data,
|
|
use_lookup_tables=use_lookup_tables,
|
|
use_regexes=use_regex_features,
|
|
)
|
|
|
|
assert actual_patterns == expected_patterns
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"lookup_tables, regex_features, use_lookup_tables, use_regex_features",
|
|
[
|
|
(
|
|
{"name": "person", "elements": ["Max", "John"]},
|
|
{"name": "zipcode", "pattern": "*[0-9]{5}"},
|
|
True,
|
|
True,
|
|
)
|
|
],
|
|
)
|
|
def test_regex_validation(
|
|
lookup_tables: Dict[Text, List[Text]],
|
|
regex_features: Dict[Text, Text],
|
|
use_lookup_tables: bool,
|
|
use_regex_features: bool,
|
|
):
|
|
"""Tests if exception is raised when regex patterns are invalid."""
|
|
|
|
training_data = TrainingData()
|
|
if lookup_tables:
|
|
training_data.lookup_tables = [lookup_tables]
|
|
if regex_features:
|
|
training_data.regex_features = [regex_features]
|
|
|
|
with pytest.raises(Exception) as e:
|
|
pattern_utils.extract_patterns(
|
|
training_data,
|
|
use_lookup_tables=use_lookup_tables,
|
|
use_regexes=use_regex_features,
|
|
)
|
|
|
|
assert "Model training failed." in str(e.value)
|
|
assert "not a valid regex." in str(e.value)
|
|
assert "Please update your nlu training data configuration" in str(e.value)
|