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)
def test_add_item_to_lookup_tables_unloaded_file(): lookup_item_title = "additional_currencies" lookup_tables = [{"name": lookup_item_title, "elements": "lookup.txt"}] with pytest.raises(TypeError): lookup_tables_parser.add_item_to_lookup_tables(lookup_item_title, "Pound", lookup_tables)
def test_add_item_to_lookup_tables(): lookup_item_title = "additional_currencies" lookup_examples = ["Peso", "Euro", "Dollar"] result = [] for example in lookup_examples: lookup_tables_parser.add_item_to_lookup_tables(lookup_item_title, example, result) assert result == [{"name": lookup_item_title, "elements": lookup_examples}]
def _parse_lookup(self, nlu_item: Dict[Text, Any]): import rasa.shared.nlu.training_data.lookup_tables_parser as lookup_tables_parser lookup_item_name = nlu_item[KEY_LOOKUP] if not lookup_item_name: rasa.shared.utils.io.raise_warning( f"Issue found while processing '{self.filename}': " f"The lookup item has an empty name. " f"Lookup items should have a name defined under the '{KEY_LOOKUP}' " f"key. It will be skipped.", docs=DOCS_URL_TRAINING_DATA, ) return examples = nlu_item.get(KEY_LOOKUP_EXAMPLES, "") if not examples: rasa.shared.utils.io.raise_warning( f"Issue found while processing '{self.filename}': " f"'{KEY_LOOKUP}: {lookup_item_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"This block will be skipped.", docs=DOCS_URL_TRAINING_DATA, ) return for example in self._parse_multiline_example(lookup_item_name, examples): lookup_tables_parser.add_item_to_lookup_tables( lookup_item_name, example, self.lookup_tables)