Skip to content

Commit

Permalink
Make context manager an input to engine.determine_intent, as opposed to
Browse files Browse the repository at this point in the history
a member of engine.
  • Loading branch information
clusterfudge committed Nov 12, 2016
1 parent e27f493 commit b89702f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
14 changes: 9 additions & 5 deletions adapt/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ class IntentDeterminationEngine(pyee.EventEmitter):
This system makes heavy use of generators to enable greedy algorithms to short circuit large portions of
computation.
"""
def __init__(self, tokenizer=None, trie=None, context_manager=None):
def __init__(self, tokenizer=None, trie=None):
pyee.EventEmitter.__init__(self)
self.tokenizer = tokenizer or EnglishTokenizer()
self.trie = trie or Trie()
self.regular_expressions_entities = []
self._regex_strings = set()
self.tagger = EntityTagger(self.trie, self.tokenizer, self.regular_expressions_entities)
self.intent_parsers = []
self.context_manager = context_manager

def __best_intent(self, parse_result, context=[]):
best_intent = None
Expand All @@ -48,12 +47,17 @@ def __get_unused_context(self, parse_result, context):
result_context = [c for c in context if c['key'] not in tags_keys]
return result_context

def determine_intent(self, utterance, num_results=1, include_tags=False):
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.
Expand All @@ -64,8 +68,8 @@ def determine_intent(self, utterance, num_results=1, include_tags=False):
self.emit("tagged_entities", result)))

context = []
if self.context_manager:
context = self.context_manager.get_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)
Expand Down
8 changes: 4 additions & 4 deletions test/ContextManagerIntegrationTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class ContextManagerIntegrationTest(unittest.TestCase):
def setUp(self):
self.context_manager = ContextManager()
self.engine = IntentDeterminationEngine(context_manager=self.context_manager)
self.engine = IntentDeterminationEngine()

def testBasicContextualFollowup(self):
intent1 = IntentBuilder("TimeQueryIntent")\
Expand All @@ -32,7 +32,7 @@ def testBasicContextualFollowup(self):
self.engine.register_entity("weather", "WeatherKeyword")

utterance1 = "what time is it in seattle"
intent = next(self.engine.determine_intent(utterance1, include_tags=True))
intent = next(self.engine.determine_intent(utterance1, include_tags=True, context_manager=self.context_manager))
assert intent
assert intent['intent_type'] == 'TimeQueryIntent'
assert '__tags__' in intent
Expand All @@ -41,7 +41,7 @@ def testBasicContextualFollowup(self):
self.context_manager.inject_context(context_entity)

utterance2 = "what's the weather like?"
intent = next(self.engine.determine_intent(utterance2))
intent = next(self.engine.determine_intent(utterance2, context_manager=self.context_manager))
assert intent
assert intent['intent_type'] == 'WeatherQueryIntent'

Expand All @@ -57,7 +57,7 @@ def testContextOnlyUsedOnce(self):
self.engine.register_entity("foo", "Foo")
self.engine.register_entity("fop", "Foo")

intent = next(self.engine.determine_intent("foo", include_tags=True))
intent = next(self.engine.determine_intent("foo", include_tags=True, context_manager=self.context_manager))
assert intent
assert intent['intent_type'] == "DummyIntent"
assert not (intent.get("Foo") and intent.get("Foo2"))
Expand Down

0 comments on commit b89702f

Please sign in to comment.