Beispiel #1
0
def test_generate_message_raises_on_overlapping_but_not_identical_spans(
    message_text: Text,
    entities: List[Dict[Text, Any]],
):
    message = Message.build(message_text, "dummy_intent", entities=entities)
    with pytest.raises(ValueError):
        TrainingDataWriter.generate_message(message)
Beispiel #2
0
def test_evaluation_store_serialise(entity_predictions, entity_targets):
    from rasa.shared.nlu.training_data.formats.readerwriter import TrainingDataWriter

    store = EvaluationStore(entity_predictions=entity_predictions,
                            entity_targets=entity_targets)

    targets, predictions = store.serialise()

    assert len(targets) == len(predictions)

    i_pred = 0
    i_target = 0
    for i, prediction in enumerate(predictions):
        target = targets[i]
        if prediction != "None" and target != "None":
            predicted = entity_predictions[i_pred]
            assert prediction == TrainingDataWriter.generate_entity(
                predicted.get("text"), predicted)
            assert predicted.get("start") == entity_targets[i_target].get(
                "start")
            assert predicted.get("end") == entity_targets[i_target].get("end")

        if prediction != "None":
            i_pred += 1
        if target != "None":
            i_target += 1
Beispiel #3
0
def test_generate_message(
    message_text: Text,
    expected_text: Text,
    entities: List[Dict[Text, Any]],
):
    message = Message.build(message_text, "dummy_intent", entities=entities)
    message_text = TrainingDataWriter.generate_message(message)

    assert message_text == expected_text
Beispiel #4
0
def md_format_message(
    text: Text, intent: Optional[Text], entities: Union[Text, List[Any]]
) -> Text:
    """Uses NLU parser information to generate a message with inline entity annotations.

    Arguments:
        text: text of the message
        intent: intent of the message
        entities: entities of the message

    Return:
        Message with entities annotated inline, e.g.
        `I am from [Berlin]{"entity": "city"}`.
    """
    from rasa.shared.nlu.training_data.formats.readerwriter import TrainingDataWriter
    from rasa.shared.nlu.training_data import entities_parser

    message_from_md = entities_parser.parse_training_example(text, intent)
    deserialised_entities = deserialise_entities(entities)
    return TrainingDataWriter.generate_message(
        {"text": message_from_md.get(TEXT), "entities": deserialised_entities}
    )
Beispiel #5
0
    def process_training_examples_by_key(
        training_examples: Dict,
        key_name: Text,
        key_examples: Text,
        example_extraction_predicate=lambda x: x,
    ) -> List[OrderedDict]:
        from ruamel.yaml.scalarstring import LiteralScalarString

        result = []
        for entity_key, examples in training_examples.items():

            converted_examples = [
                TrainingDataWriter.generate_list_item(
                    example_extraction_predicate(example).strip(STRIP_SYMBOLS))
                for example in examples
            ]

            next_item = OrderedDict()
            next_item[key_name] = entity_key
            next_item[key_examples] = LiteralScalarString(
                "".join(converted_examples))
            result.append(next_item)

        return result
Beispiel #6
0
 def _generate_entity_training_data(entity: Dict[Text, Any]) -> Text:
     return TrainingDataWriter.generate_entity(entity.get("text"), entity)
Beispiel #7
0
 def render(example: Dict) -> Text:
     return TrainingDataWriter.generate_list_item(
         example[KEY_INTENT_TEXT])
Beispiel #8
0
 def render(example: Dict) -> Dict:
     text = example[KEY_INTENT_TEXT]
     example[KEY_INTENT_TEXT] = LiteralScalarString(
         TrainingDataWriter.generate_string_item(text))
     return example