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
136 lines
4.4 KiB
Python
136 lines
4.4 KiB
Python
import logging
|
|
from collections import defaultdict
|
|
from typing import Any, Dict, Text
|
|
|
|
from rasa.shared.constants import DOCS_URL_MIGRATION_GUIDE
|
|
from rasa.shared.nlu.constants import TEXT, INTENT, ENTITIES
|
|
from rasa.shared.nlu.training_data.formats.readerwriter import (
|
|
JsonTrainingDataReader,
|
|
TrainingDataWriter,
|
|
)
|
|
from rasa.shared.nlu.training_data.util import transform_entity_synonyms
|
|
from rasa.shared.utils.io import json_to_string
|
|
|
|
from rasa.shared.nlu.training_data.training_data import TrainingData
|
|
from rasa.shared.nlu.training_data.message import Message
|
|
import rasa.shared.utils.io
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class RasaReader(JsonTrainingDataReader):
|
|
"""Reader for Rasa NLU training data in JSON format.
|
|
|
|
Example:
|
|
{
|
|
"rasa_nlu_data": {
|
|
"regex_features": [
|
|
{
|
|
"name": "zipcode",
|
|
"pattern": "[0-9]{5}"
|
|
}
|
|
],
|
|
"entity_synonyms": [
|
|
{
|
|
"value": "chinese",
|
|
"synonyms": ["Chinese", "Chines", "chines"]
|
|
}
|
|
],
|
|
"common_examples": [
|
|
{
|
|
"text": "hey",
|
|
"intent": "greet",
|
|
"entities": []
|
|
},
|
|
{
|
|
"text": "howdy",
|
|
"intent": "greet",
|
|
"entities": []
|
|
}
|
|
]
|
|
}
|
|
}
|
|
"""
|
|
|
|
def __init__(self) -> None:
|
|
"""Creates reader."""
|
|
super().__init__()
|
|
rasa.shared.utils.io.raise_deprecation_warning(
|
|
"NLU data in Rasa JSON format is deprecated and will be removed in Rasa "
|
|
"Open Source 4.0.0. Please convert your JSON NLU data to the "
|
|
"Rasa YAML format.",
|
|
docs=DOCS_URL_MIGRATION_GUIDE,
|
|
)
|
|
|
|
def read_from_json(self, js: Dict[Text, Any], **_: Any) -> "TrainingData":
|
|
"""Loads training data stored in the rasa NLU data format."""
|
|
import rasa.shared.nlu.training_data.schemas.data_schema as schema
|
|
import rasa.shared.utils.validation as validation_utils
|
|
|
|
validation_utils.validate_training_data(js, schema.rasa_nlu_data_schema())
|
|
|
|
data = js["rasa_nlu_data"]
|
|
common_examples = data.get("common_examples", [])
|
|
entity_synonyms = data.get("entity_synonyms", [])
|
|
regex_features = data.get("regex_features", [])
|
|
lookup_tables = data.get("lookup_tables", [])
|
|
|
|
entity_synonyms = transform_entity_synonyms(entity_synonyms)
|
|
|
|
training_examples = []
|
|
for ex in common_examples:
|
|
# taking care of custom entries
|
|
msg = Message.build(
|
|
text=ex.pop(TEXT, ""),
|
|
intent=ex.pop(INTENT, None),
|
|
entities=ex.pop(ENTITIES, None),
|
|
**ex,
|
|
)
|
|
training_examples.append(msg)
|
|
|
|
return TrainingData(
|
|
training_examples, entity_synonyms, regex_features, lookup_tables
|
|
)
|
|
|
|
|
|
class RasaWriter(TrainingDataWriter):
|
|
"""Dumps NLU data as Rasa JSON string."""
|
|
|
|
def __init__(self) -> None:
|
|
"""Creates writer."""
|
|
super().__init__()
|
|
rasa.shared.utils.io.raise_deprecation_warning(
|
|
"NLU data in Rasa JSON format is deprecated and will be removed in Rasa "
|
|
"Open Source 4.0.0. Please convert your JSON NLU data to the "
|
|
"Rasa YAML format.",
|
|
docs=DOCS_URL_MIGRATION_GUIDE,
|
|
)
|
|
|
|
def dumps(self, training_data: "TrainingData", **kwargs: Any) -> Text:
|
|
"""Writes Training Data to a string in json format."""
|
|
js_entity_synonyms = defaultdict(list)
|
|
for k, v in training_data.entity_synonyms.items():
|
|
if k != v:
|
|
js_entity_synonyms[v].append(k)
|
|
|
|
formatted_synonyms = [
|
|
{"value": value, "synonyms": syns}
|
|
for value, syns in js_entity_synonyms.items()
|
|
]
|
|
|
|
formatted_examples = [
|
|
example.as_dict_nlu() for example in training_data.training_examples
|
|
]
|
|
|
|
return json_to_string(
|
|
{
|
|
"rasa_nlu_data": {
|
|
"common_examples": formatted_examples,
|
|
"regex_features": training_data.regex_features,
|
|
"lookup_tables": training_data.lookup_tables,
|
|
"entity_synonyms": formatted_synonyms,
|
|
}
|
|
},
|
|
**kwargs,
|
|
)
|