Example #1
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)
Example #2
0
def test_add_synonym():

    synonym_name = "savings"
    synonym_examples = ["pink pig", "savings account"]
    expected_result = {
        "pink pig": synonym_name,
        "savings account": synonym_name
    }

    result = {}

    for example in synonym_examples:
        synonyms_parser.add_synonym(example, synonym_name, result)

    assert result == expected_result
Example #3
0
    def _parse_synonym(self, nlu_item: Dict[Text, Any]) -> None:
        import rasa.shared.nlu.training_data.synonyms_parser as synonyms_parser

        synonym_name = nlu_item[KEY_SYNONYM]
        if not synonym_name:
            rasa.shared.utils.io.raise_warning(
                f"Issue found while processing '{self.filename}': "
                f"The synonym has an empty name. "
                f"Synonyms should have a name defined under the {KEY_SYNONYM} key. "
                f"It will be skipped.",
                docs=DOCS_URL_TRAINING_DATA,
            )
            return

        examples = nlu_item.get(KEY_SYNONYM_EXAMPLES, "")

        if not examples:
            rasa.shared.utils.io.raise_warning(
                f"Issue found while processing '{self.filename}': "
                f"{KEY_SYNONYM}: {synonym_name} doesn't have any examples. "
                f"It will be skipped.",
                docs=DOCS_URL_TRAINING_DATA,
            )
            return

        if not isinstance(examples, str):
            rasa.shared.utils.io.raise_warning(
                f"Unexpected block found in '{self.filename}':\n"
                f"{examples}\n"
                f"It will be skipped.",
                docs=DOCS_URL_TRAINING_DATA,
            )
            return

        for example in self._parse_multiline_example(synonym_name, examples):
            synonyms_parser.add_synonym(example, synonym_name,
                                        self.entity_synonyms)