Skip to content

Commit

Permalink
Merge pull request #103 from /issues/86
Browse files Browse the repository at this point in the history
Issue #86
  • Loading branch information
forslund authored Apr 28, 2020
2 parents 2f98c4d + 46a08d7 commit 4635401
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
10 changes: 9 additions & 1 deletion adapt/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ def __best_intent(self, parse_result, context=[]):
"""
best_intent = None
best_tags = None
context_as_entities = [{'entities': [c]} for c in context]
# TODO: there's a bunch of subtlety here around what the values of `match` and `key` should be
# Longer term, this should probably be typed, barring any performance regressions.
context_as_entities = [
{
'key': c['key'],
'entities': [c],
'from_context': True
} for c in context
]
for intent in self.intent_parsers:
i, tags = intent.validate_with_tags(parse_result.get('tags') + context_as_entities, parse_result.get('confidence'))
if not best_intent or (i and i.get('confidence') > best_intent.get('confidence')):
Expand Down
5 changes: 3 additions & 2 deletions adapt/intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def find_first_tag(tags, entity_type, after_index=-1):
for tag in tags:
for entity in tag.get('entities'):
for v, t in entity.get('data'):
if t.lower() == entity_type.lower() and tag.get('start_token', 0) > after_index:
if t.lower() == entity_type.lower() and \
(tag.get('start_token', 0) > after_index or tag.get('from_context', False)):
return tag, v, entity.get('confidence')

return None, None, None
Expand Down Expand Up @@ -171,7 +172,7 @@ def validate_with_tags(self, tags, confidence):
else:
for key in best_resolution:
result[key] = best_resolution[key][0].get('key') # TODO: at least one must support aliases
intent_confidence += 1.0
intent_confidence += 1.0 * best_resolution[key][0]['entities'][0].get('confidence', 1.0)
used_tags.append(best_resolution)
if best_resolution in local_tags:
local_tags.remove(best_resolution)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

setup(
name = "adapt-parser",
version = "0.3.4",
version = "0.3.5",
author = "Sean Fitzgerald",
author_email = "[email protected]",
description = ("A text-to-intent parsing framework."),
Expand Down
34 changes: 34 additions & 0 deletions test/ContextManagerIntegrationTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,39 @@ def testContextOnlyUsedOnce(self):
assert intent['intent_type'] == "DummyIntent"
assert not (intent.get("Foo") and intent.get("Foo2"))

def testContextAndOneOf(self):
# test to cover https://github.com/MycroftAI/adapt/issues/86
engine = IntentDeterminationEngine()
context_manager = ContextManager()

# define vocabulary
weather_keyword = [
"weather"
]

for wk in weather_keyword:
engine.register_entity(wk, "WeatherKeyword")

# structure intent
weather_intent = IntentBuilder("WeatherIntent") \
.require("WeatherKeyword") \
.one_of("Location", "LocationContext").build()

engine.register_intent_parser(weather_intent)
word = 'lizard'
context = 'LocationContext'
entity = {}
entity['data'] = [(word, context)]
entity['match'] = word
entity['key'] = word
context_manager.inject_context(entity)

intents = list(engine.determine_intent('weather', context_manager=context_manager))
self.assertEqual(1, len(intents), "Incorrect number of intents")
result = intents[0]
self.assertEqual("lizard", result.get("LocationContext"), "Context not matched")
self.assertEqual(0.75, result.get('confidence'), "Context confidence not properly applied.")




0 comments on commit 4635401

Please sign in to comment.