-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add intent tests with Adapt and Padatious support
Update resource tests to only test resources and not skill init
- Loading branch information
1 parent
4f9d482
commit 84469e8
Showing
7 changed files
with
446 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Development System | ||
# All trademark and other rights reserved by their respective owners | ||
# Copyright 2008-2021 Neongecko.com Inc. | ||
# BSD-3 | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# 1. Redistributions of source code must retain the above copyright notice, | ||
# this list of conditions and the following disclaimer. | ||
# 2. Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
# 3. Neither the name of the copyright holder nor the names of its | ||
# contributors may be used to endorse or promote products derived from this | ||
# software without specific prior written permission. | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | ||
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
from collections import namedtuple | ||
|
||
IntentMatch = namedtuple('IntentMatch', | ||
['intent_service', 'intent_type', | ||
'intent_data', 'skill_id', 'utterance']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Framework | ||
# All trademark and other rights reserved by their respective owners | ||
# Copyright 2008-2022 Neongecko.com Inc. | ||
# Contributors: Daniel McKnight, Guy Daniels, Elon Gasper, Richard Leeds, | ||
# Regina Bloomstine, Casimiro Ferreira, Andrii Pernatii, Kirill Hrymailo | ||
# BSD-3 License | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# 1. Redistributions of source code must retain the above copyright notice, | ||
# this list of conditions and the following disclaimer. | ||
# 2. Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
# 3. Neither the name of the copyright holder nor the names of its | ||
# contributors may be used to endorse or promote products derived from this | ||
# software without specific prior written permission. | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | ||
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
from typing import Optional | ||
from adapt.engine import IntentDeterminationEngine | ||
from ovos_utils.intents.intent_service_interface import open_intent_envelope | ||
from ovos_utils.log import LOG | ||
from ovos_utils.messagebus import FakeBus, get_message_lang | ||
|
||
from neon_minerva.exceptions import IntentNotMatched, ConfidenceTooLow | ||
from neon_minerva.intent_services import IntentMatch | ||
|
||
|
||
class AdaptContainer: | ||
def __init__(self, lang: str, bus: FakeBus): | ||
self.lang = lang.lower() | ||
self.bus = bus | ||
self.adapt = IntentDeterminationEngine() | ||
self.bus.on('register_vocab', self.handle_register_vocab) | ||
self.bus.on('register_intent', self.handle_register_intent) | ||
|
||
def handle_register_vocab(self, message): | ||
entity_value = message.data.get('entity_value') | ||
entity_type = message.data.get('entity_type') | ||
regex_str = message.data.get('regex') | ||
alias_of = message.data.get('alias_of') | ||
lang = get_message_lang(message) | ||
if lang != self.lang: | ||
return | ||
if regex_str: | ||
self.adapt.register_regex_entity(regex_str) | ||
else: | ||
self.adapt.register_entity(entity_value, entity_type, | ||
alias_of=alias_of) | ||
|
||
def handle_register_intent(self, message): | ||
intent = open_intent_envelope(message) | ||
self.adapt.register_intent_parser(intent) | ||
|
||
def test_intent(self, utterance: str) -> Optional[IntentMatch]: | ||
best_intent = None | ||
try: | ||
intents = [i for i in self.adapt.determine_intent( | ||
utterance, 100, | ||
include_tags=True)] | ||
if intents: | ||
best_intent = max(intents, | ||
key=lambda x: x.get('confidence', 0.0)) | ||
except Exception as err: | ||
LOG.exception(err) | ||
|
||
if not best_intent: | ||
raise IntentNotMatched(utterance) | ||
LOG.debug(best_intent) | ||
skill_id = best_intent['intent_type'].split(":")[0] | ||
_norm_id = skill_id.replace('.', '_') | ||
intent_data = {k.replace(_norm_id, '', 1): v for k, v in | ||
best_intent.items() if k.startswith(_norm_id) and | ||
isinstance(v, str)} | ||
LOG.debug(intent_data) | ||
ret = IntentMatch('Adapt', best_intent['intent_type'], intent_data, | ||
skill_id, utterance) | ||
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Framework | ||
# All trademark and other rights reserved by their respective owners | ||
# Copyright 2008-2022 Neongecko.com Inc. | ||
# Contributors: Daniel McKnight, Guy Daniels, Elon Gasper, Richard Leeds, | ||
# Regina Bloomstine, Casimiro Ferreira, Andrii Pernatii, Kirill Hrymailo | ||
# BSD-3 License | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# 1. Redistributions of source code must retain the above copyright notice, | ||
# this list of conditions and the following disclaimer. | ||
# 2. Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
# 3. Neither the name of the copyright holder nor the names of its | ||
# contributors may be used to endorse or promote products derived from this | ||
# software without specific prior written permission. | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | ||
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
from padatious import IntentContainer | ||
from ovos_utils.log import LOG | ||
from ovos_utils.messagebus import FakeBus | ||
|
||
from neon_minerva.exceptions import IntentNotMatched, ConfidenceTooLow | ||
from neon_minerva.intent_services import IntentMatch | ||
|
||
|
||
class PadatiousContainer: | ||
def __init__(self, lang: str, cache_path: str, bus: FakeBus): | ||
self.lang = lang.lower() | ||
self.bus = bus | ||
self.padatious = IntentContainer(cache_path) | ||
self.bus.on('padatious:register_intent', self.register_intent) | ||
self.bus.on('padatious:register_entity', self.register_entity) | ||
|
||
def register_intent(self, message): | ||
"""Messagebus handler for registering intents. | ||
Args: | ||
message (Message): message triggering action | ||
""" | ||
lang = message.data.get('lang', self.lang) | ||
lang = lang.lower() | ||
if lang == self.lang: | ||
LOG.debug(f"Loading intent: {message.data['name']}") | ||
self.padatious.load_intent(message.data['name'], | ||
message.data['file_name']) | ||
else: | ||
LOG.debug(f"Ignoring {message.data['name']}") | ||
|
||
def register_entity(self, message): | ||
"""Messagebus handler for registering entities. | ||
Args: | ||
message (Message): message triggering action | ||
""" | ||
lang = message.data.get('lang', self.lang) | ||
lang = lang.lower() | ||
if lang == self.lang: | ||
self.padatious.load_entity(message.data['name'], | ||
message.data['file_name']) | ||
|
||
def calc_intent(self, utt: str) -> dict: | ||
intent = self.padatious.calc_intent(utt) | ||
LOG.debug(intent) | ||
return intent.__dict__ if intent else dict() | ||
|
||
|
||
class TestPadatiousMatcher: | ||
def __init__(self, container: PadatiousContainer, | ||
include_med: bool = True, include_low: bool = False): | ||
LOG.debug("Creating test Padatious Matcher") | ||
if include_low: | ||
self.min_conf = 0.5 | ||
elif include_med: | ||
self.min_conf = 0.8 | ||
else: | ||
self.min_conf = 0.95 | ||
self.padatious = container | ||
|
||
def test_intent(self, utterance: str) -> IntentMatch: | ||
intent = self.padatious.calc_intent(utterance) | ||
if not intent: | ||
raise IntentNotMatched(utterance) | ||
conf = intent.get("conf") or 0.0 | ||
if conf < self.min_conf: | ||
raise ConfidenceTooLow(f"{conf} less than minimum {self.min_conf}") | ||
skill_id = intent.get('name').split(':')[0] | ||
sentence = ' '.join(intent.get('sent')) | ||
return IntentMatch('Padatious', intent.get('name'), | ||
intent.get('matches'), skill_id, sentence) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.