Exemplo n.º 1
0
    def _parse_intent(self, intent_data: Dict[Text, Any]) -> None:
        import rasa.shared.nlu.training_data.entities_parser as entities_parser
        import rasa.shared.nlu.training_data.synonyms_parser as synonyms_parser

        intent = intent_data.get(KEY_INTENT, "")
        if not intent:
            rasa.shared.utils.io.raise_warning(
                f"Issue found while processing '{self.filename}': "
                f"The intent has an empty name. "
                f"Intents should have a name defined under the {KEY_INTENT} key. "
                f"It will be skipped.",
                docs=DOCS_URL_TRAINING_DATA,
            )
            return

        examples = intent_data.get(KEY_INTENT_EXAMPLES, "")
        intent_metadata = intent_data.get(KEY_METADATA)
        for example, entities, metadata in self._parse_training_examples(
                examples, intent):

            plain_text = entities_parser.replace_entities(example)

            synonyms_parser.add_synonyms_from_entities(plain_text, entities,
                                                       self.entity_synonyms)

            self.training_examples.append(
                Message.build(plain_text, intent, entities, intent_metadata,
                              metadata))
Exemplo n.º 2
0
    def _parse_item(self, line: Text) -> None:
        """Parses an md list item line based on the current section type."""
        import rasa.shared.nlu.training_data.lookup_tables_parser as lookup_tables_parser  # noqa: E501
        import rasa.shared.nlu.training_data.synonyms_parser as synonyms_parser
        from rasa.shared.nlu.training_data import entities_parser

        match = re.match(item_regex, line)
        if match:
            item = match.group(1)
            if self.current_section == INTENT:
                parsed = entities_parser.parse_training_example(
                    item, self.current_title)
                synonyms_parser.add_synonyms_from_entities(
                    parsed.get(TEXT), parsed.get("entities", []),
                    self.entity_synonyms)
                self.training_examples.append(parsed)
            elif self.current_section == SYNONYM:
                synonyms_parser.add_synonym(item, self.current_title,
                                            self.entity_synonyms)
            elif self.current_section == REGEX:
                self.regex_features.append({
                    "name": self.current_title,
                    "pattern": item
                })
            elif self.current_section == LOOKUP:
                lookup_tables_parser.add_item_to_lookup_tables(
                    self.current_title, item, self.lookup_tables)
Exemplo n.º 3
0
def test_add_synonyms_from_entities():

    training_example = "I want to fly from Berlin to LA"

    entities = [
        {
            "start": 19,
            "end": 25,
            "value": "Berlin",
            "entity": "city",
            "role": "to"
        },
        {
            "start": 29,
            "end": 31,
            "value": "Los Angeles",
            "entity": "city",
            "role": "from",
        },
    ]

    result = {}

    synonyms_parser.add_synonyms_from_entities(training_example, entities,
                                               result)

    assert result == {"LA": "Los Angeles"}