def capture_tagged(state): next_pos = state.find(')') if next_pos < 1: raise IntentFormatError("Missing ending ')' in annotated utterance") else: tagged_text = state[:next_pos] state.add_tagged(tagged_text) state.move(next_pos) capture_text(state)
def capture_slot(state): next_colon_pos = state.find(':') next_square_bracket_pos = state.find(']') if next_square_bracket_pos < 0: raise IntentFormatError("Missing ending ']' in annotated utterance") if next_colon_pos < 0 or next_square_bracket_pos < next_colon_pos: slot_name = state[:next_square_bracket_pos] state.move(next_square_bracket_pos) state.add_slot(slot_name) else: slot_name = state[:next_colon_pos] state.move(next_colon_pos) entity = state[:next_square_bracket_pos] state.move(next_square_bracket_pos) state.add_slot(slot_name, entity) if state.peek() == '(': state.read() capture_tagged(state) else: capture_text(state)
def from_yaml(cls, yaml_dict): """Build an :class:`.Intent` from its YAML definition object Args: yaml_dict (dict or :class:`.IOBase`): object containing the YAML definition of the intent. It can be either a stream, or the corresponding python dict. Examples: An intent can be defined with a YAML document following the schema illustrated in the example below: >>> import io >>> from snips_nlu.common.utils import json_string >>> intent_yaml = io.StringIO(''' ... # searchFlight Intent ... --- ... type: intent ... name: searchFlight ... slots: ... - name: origin ... entity: city ... - name: destination ... entity: city ... - name: date ... entity: snips/datetime ... utterances: ... - find me a flight from [origin](Oslo) to [destination](Lima) ... - I need a flight leaving to [destination](Berlin)''') >>> intent = Intent.from_yaml(intent_yaml) >>> print(json_string(intent.json, indent=4, sort_keys=True)) { "utterances": [ { "data": [ { "text": "find me a flight from " }, { "entity": "city", "slot_name": "origin", "text": "Oslo" }, { "text": " to " }, { "entity": "city", "slot_name": "destination", "text": "Lima" } ] }, { "data": [ { "text": "I need a flight leaving to " }, { "entity": "city", "slot_name": "destination", "text": "Berlin" } ] } ] } Raises: IntentFormatError: When the YAML dict does not correspond to the :ref:`expected intent format <yaml_intent_format>` """ if isinstance(yaml_dict, IOBase): yaml_dict = yaml.safe_load(yaml_dict) object_type = yaml_dict.get("type") if object_type and object_type != "intent": raise IntentFormatError("Wrong type: '%s'" % object_type) intent_name = yaml_dict.get("name") if not intent_name: raise IntentFormatError("Missing 'name' attribute") slot_mapping = dict() for slot in yaml_dict.get("slots", []): slot_mapping[slot["name"]] = slot["entity"] utterances = [ IntentUtterance.parse(u.strip()) for u in yaml_dict["utterances"] if u.strip() ] if not utterances: raise IntentFormatError( "Intent must contain at least one utterance") return cls(intent_name, utterances, slot_mapping)