def parse(self, msg, **meta): """Parse a raw message. The interpreter will be used to determine which intent(s) has been formulated by the user and the state machine will move to the appropriate state calling the right skill handler. It will also handle some specific intents such as the cancel one and ask states. Args: msg (str): Raw message to parse meta (dict): Optional metadata to add to the request object """ self._logger.info('Parsing sentence "%s"' % msg) intents = self._interpreter.parse(msg, self._current_scopes) or [Intent(STATE_FALLBACK, text=msg)] # Add meta to each parsed intents for intent in intents: intent.meta.update(meta) cancel_intent = next((i for i in intents if i.name == STATE_CANCEL), None) # Either way, extend the intent queue with new intents if self.state != STATE_ASK: # pylint: disable=E1101 intents = [i for i in intents if i.name != STATE_CANCEL] self._logger.info('"%s" intent(s) found: %s' % (len(intents), ', '.join([str(i) for i in intents]))) self._intents_queue.extend(intents) # If the user wants to cancel the current action, immediately go to the cancel state if cancel_intent: # pylint: disable=E1101 self.go(STATE_CANCEL, intent=cancel_intent) else: if self.state == STATE_ASK: # pylint: disable=E1101 # If choices are limited, try to extract a match if self._choices: msg = find_match(self._choices, msg) # Here msg will be None if choices could not be matched, so nothing should be done anymore if msg: values = self._interpreter.parse_slot(self._request.intent.name, self._asked_slot, msg) # Update slots and meta self._request.intent.update_slots(**{ self._asked_slot: values }) self._request.intent.meta.update(meta) self._logger.info('Updated slot "%s" with values %s' % (self._asked_slot, ['"%s"' % v for v in values])) self.go(self._request.intent.name, intent=self._request.intent) elif self.state == STATE_ASLEEP: # pylint: disable=E1101 self._process_next_intent()
def test_it_find_nothing_if_value_is_empty(self): expect(find_match(self.choices, None)).to.be.none
def test_it_find_nothing_when_there_is_no_match(self): expect(find_match(self.choices, 'nothing here')).to.be.none
def test_it_find_match_when_word_not_complete(self): expect(find_match(self.choices, 'kitch')).to.equal('kitchen')
def test_it_find_match_when_it_has_typo(self): expect(find_match(self.choices, 'bedoorm')).to.equal('bedroom')
def test_it_find_one_match_when_multiple_choices_matches(self): expect(find_match(self.choices, 'room')).to.equal('bedroom')