Complete handling of intent with @intent_manager on top of method (woohoo)
This commit is contained in:
parent
3372594987
commit
dc42b827fb
@ -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
|
||||
|
@ -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]
|
||||
|
11
jarvis/skills/decorators.py
Normal file
11
jarvis/skills/decorators.py
Normal file
@ -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
|
@ -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'])
|
||||
|
@ -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")
|
||||
|
Reference in New Issue
Block a user