def determine_intent(self, utterance, num_results=1, include_tags=False, context_manager=None): """ Given an utterance, provide a valid intent. Args: utterance(str): an ascii or unicode string representing natural language speech include_tags(list): includes the parsed tags (including position and confidence) as part of result context_manager(list): a context manager to provide context to the utterance num_results(int): a maximum number of results to be returned. Returns: A generator that yields dictionaries. """ parser = Parser(self.tokenizer, self.tagger) parser.on('tagged_entities', (lambda result: self.emit("tagged_entities", result))) context = [] if context_manager: context = context_manager.get_context() for result in parser.parse(utterance, N=num_results, context=context): self.emit("parse_result", result) # create a context without entities used in result remaining_context = self.__get_unused_context(result, context) best_intent, tags = self.__best_intent(result, remaining_context) if best_intent and best_intent.get('confidence', 0.0) > 0: if include_tags: best_intent['__tags__'] = tags yield best_intent
class IntentTest: def __init__(self): self.trie = Trie() self.tokenizer = EnglishTokenizer() self.regex_entities = [] self.tagger = EntityTagger(self.trie, self.tokenizer, regex_entities=self.regex_entities) self.trie.insert("play", ("play", "PlayVerb")) self.trie.insert("play", ("play", "Command")) self.trie.insert("the big bang theory", ("the big bang theory", "Television Show")) self.trie.insert("all that", ("all that", "Television Show")) self.trie.insert("all that", ("all that", "Radio Station")) self.trie.insert("the big", ("the big", "Not a Thing")) self.trie.insert("barenaked ladies", ("barenaked ladies", "Radio Station")) self.trie.insert("show", ("show", "Command")) self.trie.insert("what", ("what", "Question")) self.parser = Parser(self.tokenizer, self.tagger) self.intent = IntentBuilder("Test Intent").require( "PlayVerb").one_of("Television Show", "Radio Station").build() def teststring(self, stringA): results = [] for result in self.parser.parse(stringA): result_intent = self.intent.validate(result.get('tags'), result.get('confidence')) results.append(result_intent) return results
def determine_intent(self, utterance, num_results=1, include_tags=False, context_manager=None): """ Given an utterance, provide a valid intent. :param utterance: an ascii or unicode string representing natural language speech :param include_tags: includes the parsed tags (including position and confidence) as part of result :param context_manager: a context manager to provide context to the utterance :param num_results: a maximum number of results to be returned. :return: A generator that yields dictionaries. """ parser = Parser(self.tokenizer, self.tagger) parser.on('tagged_entities', (lambda result: self.emit("tagged_entities", result))) context = [] if context_manager: context = context_manager.get_context() for result in parser.parse(utterance, N=num_results, context=context): self.emit("parse_result", result) # create a context without entities used in result remaining_context = self.__get_unused_context(result, context) best_intent, tags = self.__best_intent(result, remaining_context) if best_intent and best_intent.get('confidence', 0.0) > 0: if include_tags: best_intent['__tags__'] = tags yield best_intent
def determine_intent(self, utterance, num_results=1): """ Given an utterance, provide a valid intent. :param utterance: an ascii or unicode string representing natural language speech :param num_results: a maximum number of results to be returned. :return: A generator the yields dictionaries. """ parser = Parser(self.tokenizer, self.tagger) parser.on("tagged_entities", (lambda result: self.emit("tagged_entities", result))) for result in parser.parse(utterance, N=num_results): self.emit("parse_result", result) best_intent = self.__best_intent(result) if best_intent and best_intent.get("confidence", 0.0) > 0: yield best_intent
def determine_intent(self, utterance, num_results=1): """ Given an utterance, provide a valid intent. :param utterance: an ascii or unicode string representing natural language speech :param num_results: a maximum number of results to be returned. :return: A generator the yields dictionaries. """ parser = Parser(self.tokenizer, self.tagger) parser.on('tagged_entities', (lambda result: self.emit("tagged_entities", result))) for result in parser.parse(utterance, N=num_results): self.emit("parse_result", result) best_intent = self.__best_intent(result) if best_intent and best_intent.get('confidence', 0.0) > 0: yield best_intent
def determine_good_intents(self, utterance, num_results=1, include_tags=False, context_manager=None): """ Given an utterance, provide a valid intent. :param utterance: an ascii or unicode string representing natural language speech :param include_tags: includes the parsed tags (including position and confidence) as part of result :param context_manager: a context manager to provide context to the utterance :param num_results: a maximum number of results to be returned. :return: A generator that yields dictionaries. """ parser = Parser(self.tokenizer, self.tagger) parser.on('tagged_entities', (lambda result: self.emit("tagged_entities", result))) context = [] if context_manager: context = context_manager.get_context() all_good_intents = [] for result in parser.parse(utterance, N=num_results, context=context): self.emit("parse_result", result) # create a context without entities used in result remaining_context = self.__get_unused_context(result, context) good_intents = self.__good_intents(result, include_tags, remaining_context) all_good_intents += good_intents all_good_intents = sorted(all_good_intents, key=lambda i: i['confidence'], reverse=True) for intent in all_good_intents: yield intent
class IntentTest(unittest.TestCase): def setUp(self): self.trie = Trie() self.tokenizer = EnglishTokenizer() self.regex_entities = [] self.tagger = EntityTagger(self.trie, self.tokenizer, regex_entities=self.regex_entities) self.trie.insert("play", ("play", "PlayVerb")) self.trie.insert("the big bang theory", ("the big bang theory", "Television Show")) self.trie.insert("the big", ("the big", "Not a Thing")) self.trie.insert("barenaked ladies", ("barenaked ladies", "Radio Station")) self.trie.insert("show", ("show", "Command")) self.trie.insert("what", ("what", "Question")) self.parser = Parser(self.tokenizer, self.tagger) def tearDown(self): pass def test_basic_intent(self): intent = IntentBuilder("play television intent")\ .require("PlayVerb")\ .require("Television Show")\ .build() for result in self.parser.parse("play the big bang theory"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('PlayVerb') == 'play' assert result_intent.get('Television Show') == "the big bang theory" def test_at_least_one(self): intent = IntentBuilder("play intent")\ .require("PlayVerb")\ .one_of("Television Show", "Radio Station")\ .build() for result in self.parser.parse("play the big bang theory"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('PlayVerb') == 'play' assert result_intent.get('Television Show') == "the big bang theory" for result in self.parser.parse("play the barenaked ladies"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('PlayVerb') == 'play' assert result_intent.get('Radio Station') == "barenaked ladies" def test_at_least_on_no_required(self): intent = IntentBuilder("play intent") \ .one_of("Television Show", "Radio Station") \ .build() for result in self.parser.parse("play the big bang theory"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('Television Show') == "the big bang theory" for result in self.parser.parse("play the barenaked ladies"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('Radio Station') == "barenaked ladies" def test_at_least_one_alone(self): intent = IntentBuilder("OptionsForLunch") \ .one_of("Question", "Command") \ .build() for result in self.parser.parse("show"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('Command') == "show" def test_basic_intent_with_alternate_names(self): intent = IntentBuilder("play television intent")\ .require("PlayVerb", "Play Verb")\ .require("Television Show", "series")\ .build() for result in self.parser.parse("play the big bang theory"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('Play Verb') == 'play' assert result_intent.get('series') == "the big bang theory" def test_intent_with_regex_entity(self): self.trie = Trie() self.tagger = EntityTagger(self.trie, self.tokenizer, self.regex_entities) self.parser = Parser(self.tokenizer, self.tagger) self.trie.insert("theory", ("theory", "Concept")) regex = re.compile(r"the (?P<Event>.*)") self.regex_entities.append(regex) intent = IntentBuilder("mock intent")\ .require("Event")\ .require("Concept").build() for result in self.parser.parse("the big bang theory"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('Event') == 'big bang' assert result_intent.get('Concept') == "theory" def test_intent_using_alias(self): self.trie.insert("big bang", ("the big bang theory", "Television Show")) intent = IntentBuilder("play television intent")\ .require("PlayVerb", "Play Verb")\ .require("Television Show", "series")\ .build() for result in self.parser.parse("play the big bang theory"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('Play Verb') == 'play' assert result_intent.get('series') == "the big bang theory"
class IntentTest(unittest.TestCase): def setUp(self): self.trie = Trie() self.tokenizer = EnglishTokenizer() self.regex_entities = [] self.tagger = EntityTagger(self.trie, self.tokenizer, regex_entities=self.regex_entities) self.trie.insert("play", ("play", "PlayVerb")) self.trie.insert("the big bang theory", ("the big bang theory", "Television Show")) self.trie.insert("the big", ("the big", "Not a Thing")) self.trie.insert("barenaked ladies", ("barenaked ladies", "Radio Station")) self.trie.insert("show", ("show", "Command")) self.trie.insert("what", ("what", "Question")) self.parser = Parser(self.tokenizer, self.tagger) def tearDown(self): pass def test_basic_intent(self): intent = IntentBuilder("play television intent")\ .require("PlayVerb")\ .require("Television Show")\ .build() for result in self.parser.parse("play the big bang theory"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('PlayVerb') == 'play' assert result_intent.get( 'Television Show') == "the big bang theory" def test_at_least_one(self): intent = IntentBuilder("play intent")\ .require("PlayVerb")\ .one_of("Television Show", "Radio Station")\ .build() for result in self.parser.parse("play the big bang theory"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('PlayVerb') == 'play' assert result_intent.get( 'Television Show') == "the big bang theory" for result in self.parser.parse("play the barenaked ladies"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('PlayVerb') == 'play' assert result_intent.get('Radio Station') == "barenaked ladies" def test_at_least_on_no_required(self): intent = IntentBuilder("play intent") \ .one_of("Television Show", "Radio Station") \ .build() for result in self.parser.parse("play the big bang theory"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get( 'Television Show') == "the big bang theory" for result in self.parser.parse("play the barenaked ladies"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('Radio Station') == "barenaked ladies" def test_at_least_one_alone(self): intent = IntentBuilder("OptionsForLunch") \ .one_of("Question", "Command") \ .build() for result in self.parser.parse("show"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('Command') == "show" def test_basic_intent_with_alternate_names(self): intent = IntentBuilder("play television intent")\ .require("PlayVerb", "Play Verb")\ .require("Television Show", "series")\ .build() for result in self.parser.parse("play the big bang theory"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('Play Verb') == 'play' assert result_intent.get('series') == "the big bang theory" def test_intent_with_regex_entity(self): self.trie = Trie() self.tagger = EntityTagger(self.trie, self.tokenizer, self.regex_entities) self.parser = Parser(self.tokenizer, self.tagger) self.trie.insert("theory", ("theory", "Concept")) regex = re.compile(r"the (?P<Event>.*)") self.regex_entities.append(regex) intent = IntentBuilder("mock intent")\ .require("Event")\ .require("Concept").build() for result in self.parser.parse("the big bang theory"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('Event') == 'big bang' assert result_intent.get('Concept') == "theory" def test_intent_using_alias(self): self.trie.insert("big bang", ("the big bang theory", "Television Show")) intent = IntentBuilder("play television intent")\ .require("PlayVerb", "Play Verb")\ .require("Television Show", "series")\ .build() for result in self.parser.parse("play the big bang theory"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('Play Verb') == 'play' assert result_intent.get('series') == "the big bang theory"
class IntentTest(unittest.TestCase): def setUp(self): self.trie = Trie() self.tokenizer = EnglishTokenizer() self.regex_entities = [] self.tagger = EntityTagger(self.trie, self.tokenizer, regex_entities=self.regex_entities) self.trie.insert("play", ("play", "PlayVerb")) self.trie.insert("stop", ("stop", "StopVerb")) self.trie.insert("the big bang theory", ("the big bang theory", "Television Show")) self.trie.insert("the big", ("the big", "Not a Thing")) self.trie.insert("barenaked ladies", ("barenaked ladies", "Radio Station")) self.trie.insert("show", ("show", "Command")) self.trie.insert("what", ("what", "Question")) self.parser = Parser(self.tokenizer, self.tagger) def tearDown(self): pass def test_basic_intent(self): intent = IntentBuilder("play television intent") \ .require("PlayVerb") \ .require("Television Show") \ .build() for result in self.parser.parse("play the big bang theory"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('PlayVerb') == 'play' assert result_intent.get( 'Television Show') == "the big bang theory" def test_at_least_one(self): intent = IntentBuilder("play intent") \ .require("PlayVerb") \ .one_of("Television Show", "Radio Station") \ .build() for result in self.parser.parse("play the big bang theory"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('PlayVerb') == 'play' assert result_intent.get( 'Television Show') == "the big bang theory" for result in self.parser.parse("play the barenaked ladies"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('PlayVerb') == 'play' assert result_intent.get('Radio Station') == "barenaked ladies" def test_at_least_one_with_tag_in_multiple_slots(self): self.trie.insert("temperature", ("temperature", "temperature")) self.trie.insert("living room", ("living room", "living room")) self.trie.insert("what is", ("what is", "what is")) intent = IntentBuilder("test intent") \ .one_of("what is") \ .one_of("temperature", "living room") \ .one_of("temperature") \ .build() for result in self.parser.parse( "what is the temperature in the living room"): result_intent = intent.validate(result.get("tags"), result.get("confidence")) assert result_intent.get("confidence") > 0.0 assert result_intent.get("temperature") == "temperature" assert result_intent.get("living room") == "living room" assert result_intent.get("what is") == "what is" def test_at_least_on_no_required(self): intent = IntentBuilder("play intent") \ .one_of("Television Show", "Radio Station") \ .build() for result in self.parser.parse("play the big bang theory"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get( 'Television Show') == "the big bang theory" for result in self.parser.parse("play the barenaked ladies"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('Radio Station') == "barenaked ladies" def test_at_least_one_alone(self): intent = IntentBuilder("OptionsForLunch") \ .one_of("Question", "Command") \ .build() for result in self.parser.parse("show"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('Command') == "show" def test_basic_intent_with_alternate_names(self): intent = IntentBuilder("play television intent") \ .require("PlayVerb", "Play Verb") \ .require("Television Show", "series") \ .build() for result in self.parser.parse("play the big bang theory"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('Play Verb') == 'play' assert result_intent.get('series') == "the big bang theory" def test_intent_with_regex_entity(self): self.trie = Trie() self.tagger = EntityTagger(self.trie, self.tokenizer, self.regex_entities) self.parser = Parser(self.tokenizer, self.tagger) self.trie.insert("theory", ("theory", "Concept")) regex = re.compile(r"the (?P<Event>.*)") self.regex_entities.append(regex) intent = IntentBuilder("mock intent") \ .require("Event") \ .require("Concept").build() for result in self.parser.parse("the big bang theory"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('Event') == 'big bang' assert result_intent.get('Concept') == "theory" def test_intent_using_alias(self): self.trie.insert("big bang", ("the big bang theory", "Television Show")) intent = IntentBuilder("play television intent") \ .require("PlayVerb", "Play Verb") \ .require("Television Show", "series") \ .build() for result in self.parser.parse("play the big bang theory"): result_intent = intent.validate(result.get('tags'), result.get('confidence')) assert result_intent.get('confidence') > 0.0 assert result_intent.get('Play Verb') == 'play' assert result_intent.get('series') == "the big bang theory" def test_resolve_one_of(self): tags = [{ "confidence": 1.0, "end_token": 1, "entities": [{ "confidence": 1.0, "data": [["what is", "skill_iot_controlINFORMATION_QUERY"]], "key": "what is", "match": "what is" }], "from_context": False, "key": "what is", "match": "what is", "start_token": 0 }, { "end_token": 3, "entities": [{ "confidence": 1.0, "data": [["temperature", "skill_weatherTemperature"], ["temperature", "skill_iot_controlTEMPERATURE"]], "key": "temperature", "match": "temperature" }], "from_context": False, "key": "temperature", "match": "temperature", "start_token": 3 }, { "confidence": 1.0, "end_token": 7, "entities": [{ "confidence": 1.0, "data": [["living room", "skill_iot_controlENTITY"]], "key": "living room", "match": "living room" }], "from_context": False, "key": "living room", "match": "living room", "start_token": 6 }] at_least_one = [["skill_iot_controlINFORMATION_QUERY"], [ "skill_iot_controlTEMPERATURE", "skill_iot_controlENTITY" ], ["skill_iot_controlTEMPERATURE"]] result = { "skill_iot_controlENTITY": [{ "confidence": 1.0, "end_token": 7, "entities": [{ "confidence": 1.0, "data": [["living room", "skill_iot_controlENTITY"]], "key": "living room", "match": "living room" }], "from_context": False, "key": "living room", "match": "living room", "start_token": 6 }], "skill_iot_controlINFORMATION_QUERY": [{ "confidence": 1.0, "end_token": 1, "entities": [{ "confidence": 1.0, "data": [["what is", "skill_iot_controlINFORMATION_QUERY"]], "key": "what is", "match": "what is" }], "from_context": False, "key": "what is", "match": "what is", "start_token": 0 }], "skill_iot_controlTEMPERATURE": [{ "end_token": 3, "entities": [{ "confidence": 1.0, "data": [["temperature", "skill_weatherTemperature"], ["temperature", "skill_iot_controlTEMPERATURE"]], "key": "temperature", "match": "temperature" }], "from_context": False, "key": "temperature", "match": "temperature", "start_token": 3 }] } assert resolve_one_of(tags, at_least_one) == result
class ParserTest(unittest.TestCase): def setUp(self): self.trie = Trie() self.tokenizer = EnglishTokenizer() self.regex_entities = [] self.tagger = EntityTagger(self.trie, self.tokenizer, regex_entities=self.regex_entities) self.trie.insert("play", ("play", "PlayVerb")) self.trie.insert("the big bang theory", ("the big bang theory", "Television Show")) self.trie.insert("the big", ("the big", "Not a Thing")) self.trie.insert("barenaked ladies", ("barenaked ladies", "Radio Station")) self.trie.insert("show", ("show", "Command")) self.trie.insert("what", ("what", "Question")) self.parser = Parser(self.tokenizer, self.tagger) pass def test_basic_intent(self): s = "show play the big bang theory" verify = { 'confidence': 0.9310344827586207, 'tags': [{ 'end_token': 0, 'entities': [{ 'confidence': 1.0, 'data': [('show', 'Command')], 'key': 'show', 'match': 'show' }], 'from_context': False, 'key': 'show', 'match': 'show', 'start_token': 0 }, { 'end_token': 1, 'entities': [{ 'confidence': 1.0, 'data': [('play', 'PlayVerb')], 'key': 'play', 'match': 'play' }], 'from_context': False, 'key': 'play', 'match': 'play', 'start_token': 1 }, { 'confidence': 1.0, 'end_token': 5, 'entities': [{ 'confidence': 1.0, 'data': [('the big bang theory', 'Television Show')], 'key': 'the big bang theory', 'match': 'the big bang theory' }], 'from_context': False, 'key': 'the big bang theory', 'match': 'the big bang theory', 'start_token': 2 }], 'time': 0.0001361370086669922, 'utterance': 'show play the big bang theory' } for result in self.parser.parse(s): assert (result['tags'] == verify['tags'])