def _parse_item(self, line: Text) -> None: """Parses an md list item line based on the current section type.""" import rasa.nlu.training_data.lookup_tables_parser as lookup_tables_parser import rasa.nlu.training_data.synonyms_parser as synonyms_parser from rasa.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.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 _parse_intent(self, data: Dict[Text, Any]) -> None: from rasa.nlu.training_data import Message import rasa.nlu.training_data.entities_parser as entities_parser import rasa.nlu.training_data.synonyms_parser as synonyms_parser import rasa.nlu.constants as nlu_constants intent = data.get(KEY_INTENT, "") if not intent: 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_NLU, ) return examples = data.get(KEY_INTENT_EXAMPLES, "") for example, entities in self._parse_training_examples(examples, intent): synonyms_parser.add_synonyms_from_entities( example, entities, self.entity_synonyms ) plain_text = entities_parser.replace_entities(example) message = Message.build(plain_text, intent) if entities: message.set(nlu_constants.ENTITIES, entities) self.training_examples.append(message)
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"}
def parse_training_example(self, example: Text) -> "Message": """Extract entities and synonyms, and convert to plain text.""" from rasa.nlu.training_data import Message import rasa.nlu.training_data.entities_parser as entities_parser import rasa.nlu.training_data.synonyms_parser as synonyms_parser entities = entities_parser.find_entities_in_training_example(example) plain_text = entities_parser.replace_entities(example) synonyms_parser.add_synonyms_from_entities(plain_text, entities, self.entity_synonyms) message = Message.build(plain_text, self.current_title) if len(entities) > 0: message.set("entities", entities) return message