Skip to content

Commit

Permalink
chore: Update snips-nlu to 0.19.6 and update code accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
YuukanOO committed Apr 28, 2019
1 parent a03c327 commit 688408d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
14 changes: 7 additions & 7 deletions pytlas/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,26 @@ def is_builtin(state):

return state.startswith(STATE_PREFIX) and state.endswith(STATE_SUFFIX) if state else False

def build_scopes(intents):
def build_scopes(intents, include_cancel_state=True):
"""Build all scopes given an intents list. It will create a dict which contains
association between a context and available scopes.
Scopes are intents which could be triggered from a particular context. They will be given
to the interpreter to restrict the list of intents to be parsed. That's why STATE_CANCEL is
always added to every scopes.
Builtin nested scopes will be discarded they do not have specific meanings for the NLU.
always added to every scopes if include_cancel_state is set to True.
Args:
intents (list): List of intents
include_cancel_state (bool): Should builtin state cancel be always added?
Returns:
dict: Dictionary mapping each context to a list of available scopes
"""

scopes = {
None: [STATE_CANCEL], # represents the root scopes, ie. when not in a specific context
# represents the root scopes, ie. when not in a specific context
None: [STATE_CANCEL] if include_cancel_state else [],
}

for intent in intents:
Expand All @@ -60,7 +60,7 @@ def build_scopes(intents):
root = intent[:last_idx]

if root not in scopes:
scopes[root] = [STATE_CANCEL] # Cancel is always present
scopes[root] = [STATE_CANCEL] if include_cancel_state else []

scopes[root].append(intent)
except:
Expand Down Expand Up @@ -149,7 +149,7 @@ def build(self):
intents = [i for i in self._interpreter.intents if not is_builtin(i)]
states = [STATE_ASLEEP, STATE_ASK, STATE_FALLBACK, STATE_CANCEL] + intents

self._available_scopes = build_scopes(intents)
self._available_scopes = build_scopes(intents, STATE_CANCEL in self._interpreter.intents)

MachineKlass = Machine
kwargs = {} # Additional arguments to give to the machine
Expand Down
2 changes: 1 addition & 1 deletion pytlas/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '4.0.4'
__version__ = '4.0.5'
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
extras_require={
'snips': [
# For snips, target a specific version since it may break sometimes
'snips-nlu==0.19.5',
'snips-nlu==0.19.6',
],
'test': [
'nose~=1.3.7',
Expand Down
23 changes: 22 additions & 1 deletion tests/test_agent_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def setup(self):
}

self.interpreter = Interpreter('test', 'en')
self.interpreter.intents = [i for i in self.handlers.keys() if STATE_FALLBACK not in i]
self.interpreter.intents = [i for i in self.handlers.keys() if STATE_FALLBACK not in i] + [STATE_CANCEL]
self.agent = Agent(self.interpreter, handlers=self.handlers, model=self)

def test_it_should_parse_scopes_correctly(self):
Expand Down Expand Up @@ -77,6 +77,27 @@ def test_it_should_parse_scopes_correctly(self):
expect(scope).to.contain(STATE_CANCEL)
expect(scope).to.contain('an intent/a sub intent/another one')

def test_it_should_not_contains_the_cancel_scope_if_interpreter_does_not_understand_it(self):
intents = ['an intent', 'an intent/a sub intent', 'an intent/a sub intent/another one', 'one more']

r = build_scopes(intents, include_cancel_state=False)

scope = r[None]

expect(scope).to.have.length_of(2)
expect(scope).to.contain('an intent')
expect(scope).to.contain('one more')

scope = r['an intent']

expect(scope).to.have.length_of(1)
expect(scope).to.contain('an intent/a sub intent')

scope = r['an intent/a sub intent']

expect(scope).to.have.length_of(1)
expect(scope).to.contain('an intent/a sub intent/another one')

def test_it_should_be_initialized_with_the_none_context(self):
expect(self.agent.current_context).to.equal(None)
expect(self.agent._available_scopes[None]).to.contain('manage_list')
Expand Down

0 comments on commit 688408d

Please sign in to comment.