Skip to content

Commit

Permalink
Issues/114 (#128)
Browse files Browse the repository at this point in the history
* Addresses issues/114

* Slightly clearer conditional

* Update comments

* Use tag_confidence for optional tags
  • Loading branch information
clusterfudge authored Apr 26, 2021
1 parent 818ea55 commit e748b20
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
19 changes: 10 additions & 9 deletions adapt/intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,14 @@ def validate(self, tags, confidence):
intent, tags = self.validate_with_tags(tags, confidence)
return intent

def validate_with_tags(self, tags, confidence):
def validate_with_tags(self, tags, parse_weight):
"""Validate whether tags has required entites for this intent to fire
Args:
tags(list): Tags and Entities used for validation
confidence(float): ?
parse_weight(float): The weight associate to the parse result,
as indicated by the parser. This is influenced by a parser
that uses edit distance or context.
Returns:
intent, tags: Returns intent and tags used by the intent on
Expand All @@ -152,7 +154,7 @@ def validate_with_tags(self, tags, confidence):
used_tags = []

for require_type, attribute_name in self.requires:
required_tag, canonical_form, confidence = find_first_tag(local_tags, require_type)
required_tag, canonical_form, tag_confidence = find_first_tag(local_tags, require_type)
if not required_tag:
result['confidence'] = 0.0
return result, []
Expand All @@ -161,8 +163,7 @@ def validate_with_tags(self, tags, confidence):
if required_tag in local_tags:
local_tags.remove(required_tag)
used_tags.append(required_tag)
# TODO: use confidence based on edit distance and context
intent_confidence += confidence
intent_confidence += tag_confidence

if len(self.at_least_one) > 0:
best_resolution = resolve_one_of(tags, self.at_least_one)
Expand All @@ -178,18 +179,18 @@ def validate_with_tags(self, tags, confidence):
local_tags.remove(best_resolution)

for optional_type, attribute_name in self.optional:
optional_tag, canonical_form, conf = find_first_tag(local_tags, optional_type)
optional_tag, canonical_form, tag_confidence = find_first_tag(local_tags, optional_type)
if not optional_tag or attribute_name in result:
continue
result[attribute_name] = canonical_form
if optional_tag in local_tags:
local_tags.remove(optional_tag)
used_tags.append(optional_tag)
intent_confidence += 1.0
intent_confidence += tag_confidence

total_confidence = intent_confidence / len(tags) * confidence
total_confidence = (intent_confidence / len(tags) * parse_weight) if tags else 0.0

target_client, canonical_form, confidence = find_first_tag(local_tags, CLIENT_ENTITY_NAME)
target_client, canonical_form, parse_weight = find_first_tag(local_tags, CLIENT_ENTITY_NAME)

result['target'] = target_client.get('key') if target_client else None
result['confidence'] = total_confidence
Expand Down
11 changes: 11 additions & 0 deletions test/IntentEngineTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,14 @@ def matcher(regexp):
self.engine.drop_regex_entity(match_func=matcher)
assert len(self.engine._regex_strings) == 2
assert len(self.engine.regular_expressions_entities) == 2
def testEmptyTags(self):
# Validates https://github.com/MycroftAI/adapt/issues/114
engine = IntentDeterminationEngine()
engine.register_entity("Kevin",
"who") # same problem if several entities
builder = IntentBuilder("Buddies")
builder.optionally("who") # same problem if several entity types
engine.register_intent_parser(builder.build())

intents = [i for i in engine.determine_intent("Julien is a friend")]
assert len(intents) == 0

0 comments on commit e748b20

Please sign in to comment.