Skip to content
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

refactor:upstream classes #6

Merged
merged 11 commits into from
Oct 14, 2024
Merged
Changes from 3 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
43 changes: 34 additions & 9 deletions ovos_adapt/intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

__author__ = 'seanfitz'

import abc
import itertools
from typing import Optional, List

CLIENT_ENTITY_NAME = 'Client'

Expand Down Expand Up @@ -116,8 +118,20 @@ def resolve_one_of(tags, at_least_one):
return None


class Intent(object):
def __init__(self, name, requires, at_least_one, optional, excludes=None):
class _IntentMeta(abc.ABCMeta):
def __instancecheck__(self, instance):
try:
# backwards compat isinstancechecks
from adapt.intent import Intent as _I
return isinstance(instance, _I) or \
super().__instancecheck__(instance)
except ImportError:
return super().__instancecheck__(instance)


class Intent(metaclass=_IntentMeta):
def __init__(self, name: str, requires: Optional[List[str]], at_least_one: Optional[List[str]] = None,
optional: Optional[List[str]] = None, excludes: Optional[List[str]] = None):
"""Create Intent object

Args:
Expand All @@ -127,10 +141,11 @@ def __init__(self, name, requires, at_least_one, optional, excludes=None):
optional(list): Optional Entities used by the intent
"""
self.name = name
self.requires = requires
self.at_least_one = at_least_one
self.optional = optional
self.requires = requires or []
self.at_least_one = at_least_one or []
self.optional = optional or []
self.excludes = excludes or []
assert self.requires or self.at_least_one

def validate(self, tags, confidence):
"""Using this method removes tags from the result of validate_with_tags
Expand Down Expand Up @@ -191,9 +206,7 @@ def validate_with_tags(self, tags, confidence):
for key in best_resolution:
# TODO: at least one should support aliases
result[key] = best_resolution[key][0].get('key')
intent_confidence += \
1.0 * best_resolution[key][0]['entities'][0]\
.get('confidence', 1.0)
intent_confidence += 1.0 * best_resolution[key][0]['entities'][0].get('confidence', 1.0)
JarbasAl marked this conversation as resolved.
Show resolved Hide resolved
used_tags.append(best_resolution[key][0])
if best_resolution in local_tags:
local_tags.remove(best_resolution[key][0])
Expand Down Expand Up @@ -221,7 +234,18 @@ def validate_with_tags(self, tags, confidence):
return result, used_tags


class IntentBuilder(object):
class _IntentBuilderMeta(abc.ABCMeta):
def __instancecheck__(self, instance):
try:
# backwards compat isinstancechecks
from adapt.intent import IntentBuilder as _IB
return isinstance(instance, _IB) or \
super().__instancecheck__(instance)
except ImportError:
return super().__instancecheck__(instance)


class IntentBuilder(metaclass=_IntentBuilderMeta):
"""
IntentBuilder, used to construct intent parsers.

Expand All @@ -242,6 +266,7 @@ class IntentBuilder(object):
.one_of("C","D")\
.optional("G").build()
"""

def __init__(self, intent_name):
"""
Constructor
Expand Down
Loading