Skip to content

Add "excludes" to Intent #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions adapt/intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def resolve_one_of(tags, at_least_one):


class Intent(object):
def __init__(self, name, requires, at_least_one, optional):
def __init__(self, name, requires, at_least_one, optional, excludes=None):
"""Create Intent object

Args:
Expand All @@ -130,6 +130,7 @@ def __init__(self, name, requires, at_least_one, optional):
self.requires = requires
self.at_least_one = at_least_one
self.optional = optional
self.excludes = excludes or []

def validate(self, tags, confidence):
"""Using this method removes tags from the result of validate_with_tags
Expand Down Expand Up @@ -160,6 +161,14 @@ def validate_with_tags(self, tags, confidence):
local_tags = tags[:]
used_tags = []

# Check excludes first
for exclude_type in self.excludes:
exclude_tag, _canonical_form, _tag_confidence = \
find_first_tag(local_tags, exclude_type)
if exclude_tag:
result['confidence'] = 0.0
return result, []

for require_type, attribute_name in self.requires:
required_tag, canonical_form, tag_confidence = \
find_first_tag(local_tags, require_type)
Expand Down Expand Up @@ -243,6 +252,7 @@ def __init__(self, intent_name):
"""
self.at_least_one = []
self.requires = []
self.excludes = []
self.optional = []
self.name = intent_name

Expand Down Expand Up @@ -277,6 +287,19 @@ def require(self, entity_type, attribute_name=None):
self.requires += [(entity_type, attribute_name)]
return self

def exclude(self, entity_type):
"""
The intent parser must not contain an entity of the provided type.

Args:
entity_type(str): an entity type

Returns:
self: to continue modifications.
"""
self.excludes.append(entity_type)
return self

def optionally(self, entity_type, attribute_name=None):
"""
Parsed intents from this parser can optionally include an entity of the
Expand All @@ -302,4 +325,5 @@ def build(self):
:return: an Intent instance.
"""
return Intent(self.name, self.requires,
self.at_least_one, self.optional)
self.at_least_one, self.optional,
self.excludes)
27 changes: 27 additions & 0 deletions test/IntentEngineTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,30 @@ def testResultsAreSortedByConfidence(self):
assert len(confidences) > 1
assert all(confidences[i] >= confidences[i+1] for i in range(len(confidences)-1))

def testExclude(self):
parser1 = IntentBuilder("Parser1").require("Entity1").exclude("Entity2").build()
self.engine.register_intent_parser(parser1)

parser2 = IntentBuilder("Parser2").require("Entity1").exclude("Entity3").build()
self.engine.register_intent_parser(parser2)

self.engine.register_entity("go", "Entity1")
self.engine.register_entity("tree", "Entity2")
self.engine.register_entity("house", "Entity3")

# Parser 1 cannot contain the word tree
utterance = "go to the tree"
intent = next(self.engine.determine_intent(utterance))
assert intent
assert intent['intent_type'] == 'Parser2'

# Parser 2 cannot contain the word house
utterance = "go to the house"
intent = next(self.engine.determine_intent(utterance))
assert intent
assert intent['intent_type'] == 'Parser1'

# Should fail because both excluded words are present
utterance = "go to the tree house"
with self.assertRaises(StopIteration):
intent = next(self.engine.determine_intent(utterance))