Skip to content

Commit

Permalink
feat/pipeline_plugin
Browse files Browse the repository at this point in the history
provide native ovos pipeline plugin, companion to OpenVoiceOS/ovos-core#349
  • Loading branch information
JarbasAl committed Sep 20, 2023
1 parent b5593bd commit d1f3a92
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
107 changes: 107 additions & 0 deletions padacioso/opm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from ovos_config import Configuration
from ovos_plugin_manager.templates.pipeline import IntentPipelinePlugin, IntentMatch
from ovos_utils import classproperty
from ovos_utils.log import LOG

from padacioso import IntentContainer


def _munge(name, skill_id):
return f"{name}:{skill_id}"


def _unmunge(munged):
return munged.split(":", 2)


class PadaciosoPipelinePlugin(IntentPipelinePlugin):

def __init__(self, bus, config=None):
config = config or Configuration().get("padatious", {}) # deprecated
super().__init__(bus, config)
self.engines = {lang: IntentContainer(
self.config.get("fuzz"), n_workers=self.workers)
for lang in self.valid_languages}

# plugin api
@classproperty
def matcher_id(self):
return "padacioso"

def match(self, utterances, lang, message):
return self.calc_intent(utterances, lang=lang)

def train(self):
# no training step needed
return True

# implementation
def _get_engine(self, lang=None):
lang = lang or self.lang
if lang not in self.engines:
self.engines[lang] = IntentContainer(self.cache_dir)
return self.engines[lang]

def detach_intent(self, skill_id, intent_name):
for intent in self.registered_intents:
if intent.name != intent_name or intent.skill_id != skill_id:
continue
LOG.debug("Detaching padacioso intent: " + intent_name)
with self.lock:
munged = _munge(intent.name, intent.skill_id)
for lang in self.engines:
self.engines[lang].remove_intent(munged)
super().detach_intent(intent_name)

def register_entity(self, skill_id, entity_name, samples=None, lang=None):
lang = lang or self.lang
super().register_entity(skill_id, entity_name, samples, lang)
container = self._get_engine(lang)
samples = samples or [entity_name]
with self.lock:
container.add_entity(entity_name, samples)

def register_intent(self, skill_id, intent_name, samples=None, lang=None):
lang = lang or self.lang
super().register_intent(skill_id, intent_name, samples, lang)
container = self._get_engine(lang)
samples = samples or [intent_name]
intent_name = _munge(intent_name, skill_id)
with self.lock:
container.add_intent(intent_name, samples)

def register_entity_from_file(self, skill_id, entity_name, file_name, lang=None):
lang = lang or self.lang
container = self._get_engine(lang)
super().register_entity_from_file(skill_id, entity_name, file_name, lang)
with self.lock:
container.load_entity(entity_name, file_name)

def register_intent_from_file(self, skill_id, intent_name, file_name, lang=None):
lang = lang or self.lang
container = self._get_engine(lang)
super().register_intent_from_file(skill_id, intent_name, file_name, lang)
intent_name = _munge(intent_name, skill_id)
with self.lock:
container.load_intent(intent_name, file_name)

def calc_intent(self, utterance, min_conf=0.0, lang=None):
lang = lang or self.lang
container = self._get_engine(lang)
min_conf = min_conf or self.config.get("min_conf", 0.35)
utterance = utterance.strip().lower()
with self.lock:
intent = container.calc_intent(utterance).__dict__
if intent["conf"] < min_conf:
return None

if isinstance(intent["utterance"], list):
intent["utterance"] = " ".join(intent["utterance"])

intent_type, skill_id = _unmunge(intent["intent_type"])
return IntentMatch(intent_service=self.matcher_id,
intent_type=intent_type,
intent_data=intent.pop("entities"),
confidence=intent["conf"],
utterance=utterance,
skill_id=skill_id)
7 changes: 6 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def required(requirements_file):
return [pkg for pkg in requirements
if pkg.strip() and not pkg.startswith("#")]


PLUGIN_ENTRY_POINT = 'ovos-intent-plugin-padacioso=padacioso.opm:PadaciosoPipelinePlugin'


setup(
name='padacioso',
version=get_version(),
Expand All @@ -56,5 +60,6 @@ def required(requirements_file):
author='jarbasai',
author_email='[email protected]',
install_requires=required("requirements.txt"),
description='dead simple intent parser'
description='dead simple intent parser',
entry_points={'ovos.pipeline': PLUGIN_ENTRY_POINT}
)

0 comments on commit d1f3a92

Please sign in to comment.