Skip to content

Commit

Permalink
Render NLU intent metadata back into YAML files
Browse files Browse the repository at this point in the history
  • Loading branch information
chdorner committed Jan 20, 2021
1 parent 7d454f6 commit 5b2176d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 42 deletions.
57 changes: 35 additions & 22 deletions rasa/shared/nlu/training_data/formats/rasa_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
KEY_LOOKUP = "lookup"
KEY_LOOKUP_EXAMPLES = "examples"
KEY_METADATA = "metadata"
KEY_METADATA_INTENT = "intent"
KEY_METADATA_EXAMPLE = "example"

MULTILINE_TRAINING_EXAMPLE_LEADING_SYMBOL = "-"
Expand Down Expand Up @@ -475,40 +476,52 @@ def process_training_examples_by_key(
result = []

for entity_key, examples in training_examples.items():
converted_examples = []
render_as_objects = False
for example in examples:
converted_example = {
KEY_INTENT_TEXT: example_extraction_predicate(example)
}

if isinstance(example, dict) and KEY_METADATA_EXAMPLE in example.get(
KEY_METADATA, {}
):
render_as_objects = True
converted_example[KEY_METADATA] = example[KEY_METADATA][
KEY_METADATA_EXAMPLE
]

converted_examples.append(converted_example)
converted, intent_metadata = RasaYAMLWriter._convert_training_examples(
examples, example_extraction_predicate
)

next_item = OrderedDict()
next_item[key_name] = entity_key
if intent_metadata:
next_item[KEY_METADATA] = intent_metadata

render_as_objects = True in [KEY_METADATA in ex for ex in converted]
if render_as_objects:
rendered_examples = RasaYAMLWriter._render_training_examples_as_objects(
converted_examples
rendered = RasaYAMLWriter._render_training_examples_as_objects(
converted
)
else:
rendered_examples = RasaYAMLWriter._render_training_examples_as_text(
converted_examples
)
next_item[key_examples] = rendered_examples
rendered = RasaYAMLWriter._render_training_examples_as_text(converted)
next_item[key_examples] = rendered

result.append(next_item)

return result

@staticmethod
def _convert_training_examples(
training_examples: List[Dict], example_extraction_predicate=lambda x: x
) -> Tuple[List[Dict], Optional[Dict]]:
"""Returns converted training examples and potential intent metadata."""
result = []
intent_metadata = None

for example in training_examples:
converted = {KEY_INTENT_TEXT: example_extraction_predicate(example)}

if isinstance(example, dict) and KEY_METADATA in example:
metadata = example[KEY_METADATA]

if KEY_METADATA_EXAMPLE in metadata:
converted[KEY_METADATA] = metadata[KEY_METADATA_EXAMPLE]

if intent_metadata is None and KEY_METADATA_INTENT in metadata:
intent_metadata = metadata[KEY_METADATA_INTENT]

result.append(converted)

return result, intent_metadata

@staticmethod
def _render_training_examples_as_objects(examples: List[Dict]) -> List[Dict]:
def render(example: Dict) -> Dict:
Expand Down
23 changes: 3 additions & 20 deletions tests/shared/nlu/training_data/formats/test_rasa_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,12 @@
- text: |
how much CO2 to [new york]{{"entity": "city", "role": "to"}}?
- intent: greet
metadata: initiate-conversation
examples: |
- Hi
- Hello
"""

INTENT_EXAMPLES_WITH_METADATA_ROUNDTRIP = f"""version: "{LATEST_TRAINING_DATA_FORMAT_VERSION}"
nlu:
- intent: intent_name
examples:
- text: |
how much CO2 will that use?
metadata:
sentiment: positive
- text: |
how much carbon will a one way flight from [new york]{{"entity": "city", "role": "from"}} to california produce?
metadata: co2-trip-calculation
- text: |
how much CO2 to [new york]{{"entity": "city", "role": "to"}}?
- intent: greet
examples: |
- Hi
- Hello
"""

MINIMAL_VALID_EXAMPLE = """
nlu:\n
Expand Down Expand Up @@ -198,10 +181,10 @@ def test_intent_with_metadata_is_parsed():

def test_metadata_roundtrip():
reader = RasaYAMLReader()
result = reader.reads(INTENT_EXAMPLES_WITH_METADATA_ROUNDTRIP)
result = reader.reads(INTENT_EXAMPLES_WITH_METADATA)

dumped = RasaYAMLWriter().dumps(result)
assert dumped == INTENT_EXAMPLES_WITH_METADATA_ROUNDTRIP
assert dumped == INTENT_EXAMPLES_WITH_METADATA

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

0 comments on commit 5b2176d

Please sign in to comment.