Skip to content

Commit

Permalink
Take care of possible multiple text examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Khizov committed Dec 3, 2020
1 parent 4b2ba7e commit 9566db7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion rasa/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ async def model_fingerprint(file_importer: "TrainingDataImporter") -> Fingerprin
domain = copy.copy(domain)
# don't include the response texts in the fingerprint.
# Their fingerprint is separate.
domain.templates = []
domain.templates = {}

return {
FINGERPRINT_CONFIG_KEY: _get_fingerprint_of_config(
Expand Down
30 changes: 11 additions & 19 deletions rasa/shared/core/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,26 +1065,18 @@ def get_responses_with_multilines(
Returns:
`responses` with preserved multilines in the `text` key.
"""
from ruamel.yaml.scalarstring import (
LiteralScalarString,
DoubleQuotedScalarString,
)

final_responses = responses
for response, items in final_responses.items():
if not len(items) == 1:
# should always be "1", but not guaranteed, see responses schema.
continue
response_text = items[0].get(KEY_RESPONSES_TEXT, "")
if not response_text:
continue
if "\n" in response_text:
from ruamel.yaml.scalarstring import LiteralScalarString

final_responses = responses.copy()
for response_name, examples in final_responses.items():
for i, example in enumerate(examples):
response_text = example.get(KEY_RESPONSES_TEXT, "")
if not response_text or "\n" not in response_text:
continue
# Has new lines, use `LiteralScalarString`
final_text = LiteralScalarString(response_text)
else:
# Doesn't have new lines, use `DoubleQuotedScalarString`
final_text = DoubleQuotedScalarString(response_text)
final_responses[response][0][KEY_RESPONSES_TEXT] = final_text
final_responses[response_name][i][
KEY_RESPONSES_TEXT
] = LiteralScalarString(response_text)

return final_responses

Expand Down
4 changes: 3 additions & 1 deletion tests/shared/core/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,8 +1197,10 @@ def test_responses_text_multiline_is_preserved():
First line
Second line
Third line
- text: One more response
utter_cancel:
- text: "First line"
- text: First line
- text: Second line
"""

domain = Domain.from_yaml(test_yaml)
Expand Down
4 changes: 3 additions & 1 deletion tests/shared/nlu/training_data/formats/test_rasa_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,10 @@ def test_responses_text_multiline_is_preserved():
First line
Second line
Third line
- text: One more response
utter_cancel:
- text: "First line"
- text: First line
- text: Second line
"""
)

Expand Down

0 comments on commit 9566db7

Please sign in to comment.