diff --git a/jarvis/main.py b/jarvis/main.py index d526e98..95455ca 100644 --- a/jarvis/main.py +++ b/jarvis/main.py @@ -36,6 +36,9 @@ def process_request(): if __name__ == '__main__': # Tests WikipediaSkill().register() + + intent_manager.process_handlers() + intent_manager.recognise("cherche sur wikipedia Elon Musk") # start the flask server diff --git a/jarvis/skills/__init__.py b/jarvis/skills/__init__.py index 4e96295..0d5f375 100644 --- a/jarvis/skills/__init__.py +++ b/jarvis/skills/__init__.py @@ -1,5 +1,6 @@ import glob import os +import types from jarvis import get_path_file from jarvis.skills import intent_manager @@ -39,5 +40,14 @@ class Skill: for line in infile.readlines(): intent_manager.register_regex(line.replace('\n', ''), self.name) - def register_intent(self, intent): - intent_manager.register_intent(intent.build(), domain=self.name) + +class SkillRegistering(type): + def __init__(cls, name, bases, attrs): + for key, val in attrs.items(): + if type(val) is types.FunctionType and not str(val).__contains__("__"): + properties = getattr(val, "_register", None) + + intent = properties[0] + intent_name = intent.name + if properties is not None: + intent_manager.intents_handlers[f"{intent_name}"] = [getattr(cls, key), name] diff --git a/jarvis/skills/decorators.py b/jarvis/skills/decorators.py new file mode 100644 index 0000000..19cc38b --- /dev/null +++ b/jarvis/skills/decorators.py @@ -0,0 +1,11 @@ +def intent_handler(*args): + """ + Creates an attribute on the method, so it can + be discovered by the metaclass + """ + + def decorator(f): + f._register = args + return f + + return decorator \ No newline at end of file diff --git a/jarvis/skills/intent_manager.py b/jarvis/skills/intent_manager.py index 54d60d0..4209141 100644 --- a/jarvis/skills/intent_manager.py +++ b/jarvis/skills/intent_manager.py @@ -1,16 +1,20 @@ +import json + from adapt.engine import DomainIntentDeterminationEngine engine = DomainIntentDeterminationEngine() +intents_handlers = dict() + def register_entity(entity_value, entity_type, domain): engine.register_entity(entity_value=entity_value, entity_type=entity_type, domain=domain) - print("[Adapt]: Added entity with type " + entity_type + " for " + domain) + # print("[Adapt]: Added entity with type " + entity_type + " for " + domain) def register_regex(regex, domain): engine.register_regex_entity(regex, domain) - print("[Adapt]: Added new regex for " + domain) + # print("[Adapt]: Added new regex for " + domain) def register_intent(intent, domain): @@ -18,6 +22,23 @@ def register_intent(intent, domain): print("[Adapt]: Registered new intent for skill " + domain) +def process_handlers(): + for handler in intents_handlers: + function_handler = intents_handlers.get(handler) + intent_builder = getattr(function_handler[0], "_register", [])[0] + skill_name = function_handler[1] + + register_intent(intent_builder.build(), domain=skill_name) + + +def handle(intent_name): + if intent_name in intents_handlers: + method = intents_handlers.get(intent_name)[0] + method(None, []) + + def recognise(sentence): for intents in engine.determine_intent(sentence): - print(intents) + json_response = json.loads(json.dumps(intents)) + + handle(json_response['intent_type']) diff --git a/jarvis/skills/research/wikipedia/__init__.py b/jarvis/skills/research/wikipedia/__init__.py index 8cd7fa6..4430ad3 100644 --- a/jarvis/skills/research/wikipedia/__init__.py +++ b/jarvis/skills/research/wikipedia/__init__.py @@ -1,16 +1,14 @@ from adapt.intent import IntentBuilder -from jarvis.skills import Skill +from jarvis.skills import Skill, SkillRegistering +from jarvis.skills.decorators import intent_handler -class WikipediaSkill(Skill): +class WikipediaSkill(Skill, metaclass=SkillRegistering): def __init__(self): + # TODO: shorten that super().__init__("WikipediaSkill", "research", "wikipedia") - def register(self): - super().register() - - wikipedia_query_intent = IntentBuilder("WikipediaQueryIntent")\ - .require("Wikipedia")\ - .require("ArticleTitle") - super().register_intent(wikipedia_query_intent) + @intent_handler(IntentBuilder("WikipediaQueryIntent").require("Wikipedia").require("ArticleTitle")) + def handle_wikipedia_query_intent(self, data): + print("Handle Wikipedia Query Intent Method")