diff --git a/jarvis/main.py b/jarvis/main.py index 777f56b..d526e98 100644 --- a/jarvis/main.py +++ b/jarvis/main.py @@ -1,7 +1,9 @@ import flask from flask import Flask, request, jsonify, Response -import jarvis.ia.process +from jarvis.ia import process +from jarvis.skills import intent_manager +from jarvis.skills.research.wikipedia import WikipediaSkill from utils import config_utils, flask_utils, intents_utils, utils app = Flask(__name__) @@ -15,7 +17,7 @@ def process_request(): flask.abort(Response('You must provide a \'sentence\' parameter (not empty aswell)!')) sentence = data['sentence'] - tag_for_request = jarvis.ia.process.get_tag_for_sentence(sentence) + tag_for_request = process.get_tag_for_sentence(sentence) print("SENTENCE : " + sentence + " /// TAG : " + tag_for_request) @@ -32,6 +34,10 @@ def process_request(): if __name__ == '__main__': + # Tests + WikipediaSkill().register() + intent_manager.recognise("cherche sur wikipedia Elon Musk") + # start the flask server app.config['JSON_AS_ASCII'] = False app.run(port=config_utils.get_in_config("PORT"), debug=False, host='0.0.0.0', threaded=True) diff --git a/jarvis/skills/__init__.py b/jarvis/skills/__init__.py index e69de29..af32180 100644 --- a/jarvis/skills/__init__.py +++ b/jarvis/skills/__init__.py @@ -0,0 +1,42 @@ +import glob +import os + +from jarvis import get_path_file +from jarvis.skills import intent_manager +from jarvis.utils import languages_utils + + +class Skill: + def __init__(self, name, category, skill_folder): + self.name = name + self.category = category + self.skill_folder = skill_folder + + def register(self): + self.register_entities() + self.register_regex() + print("Registred entitie(s) and regex(s) for " + self.name) + + def register_entities(self): + path = os.path.dirname(get_path_file.__file__) + "/skills/" + self.category + "/" + self.skill_folder + path = path + "/vocab/" + languages_utils.get_language() + "/*.voc" + + files = glob.glob(path, recursive=True) + for file in files: + with open(file, "r") as infile: + for line in infile.readlines(): + filename = file.split("/")[-1].split(".voc")[0] + + intent_manager.engine.register_entity(line.replace('\n', ''), filename, self.name) + # intent_manager.register_entity(line.replace('\n', ''), filename, self.name) + + def register_regex(self): + path = os.path.dirname(get_path_file.__file__) + "/skills/" + self.category + "/" + self.skill_folder + path = path + "/regex/" + languages_utils.get_language() + "/*.rx" + + files = glob.glob(path, recursive=True) + for file in files: + with open(file, "r") as infile: + for line in infile.readlines(): + intent_manager.engine.register_regex_entity(line.replace('\n', ''), self.name) + # intent_manager.register_regex(line.replace('\n', ''), self.name) diff --git a/jarvis/skills/intent_manager.py b/jarvis/skills/intent_manager.py new file mode 100644 index 0000000..844217a --- /dev/null +++ b/jarvis/skills/intent_manager.py @@ -0,0 +1,40 @@ +from adapt.engine import DomainIntentDeterminationEngine +from adapt.intent import IntentBuilder + +engine = DomainIntentDeterminationEngine() + + +def register_entity(value, type, domain): + engine.register_entity(entity_value=value, entity_type=type, domain=domain) + + print("entity : " + value + " / " + type + " / " + domain) + # print("Registred entity : \"" + entity + "\" for domain : " + domain) + + +def register_regex(regex, domain): + engine.register_regex_entity(regex, domain) + # print("Registred regex : \"" + regex + "\" for domain : " + domain) + + +def register_intent(intent, domain): + if domain not in engine.domains: + engine.register_domain(domain) + + # TODO: INVESTIGATE WHY IT WORKS WHEN DID MANUALLY BUT DOESN'T WHEN DID AUTOMATICALLY VIA THE INTENT + # register_entity('cherche', "Wikipedia", domain='WikipediaSkill') + # register_regex('.*(wiki|sur|au sujet de|wikipedia)(?! (sur|au sujet)) (?P.+)', + # domain='WikipediaSkill') + + # engine.register_entity('cherche', "Wikipedia", domain='WikipediaSkill') + # engine.register_regex_entity('.*(wiki|sur|au sujet de|wikipedia)(?! (sur|au sujet)) (?P.+)', + # domain='WikipediaSkill') + + # intent = IntentBuilder("WikipediaQueryIntent").require("Wikipedia").optionally('ArticleTitle').build() + + engine.register_intent_parser(intent, domain='WikipediaSkill') + # print("Registred intent : " + domain) + + +def recognise(sentence): + for intents in engine.determine_intent(sentence): + print(intents) diff --git a/jarvis/skills/research/__init__.py b/jarvis/skills/research/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/jarvis/skills/research/wikipedia/__init__.py b/jarvis/skills/research/wikipedia/__init__.py new file mode 100644 index 0000000..37d109e --- /dev/null +++ b/jarvis/skills/research/wikipedia/__init__.py @@ -0,0 +1,13 @@ +from adapt.intent import IntentBuilder + +from jarvis.skills import Skill, intent_manager + + +class WikipediaSkill(Skill): + def __init__(self): + super().__init__("WikipediaSkill", "research", "wikipedia") + + def register(self): + super().register() + + intent_manager.engine.register_intent_parser(IntentBuilder("WikipediaQueryIntent").require("Wikipedia").build(), domain=self.name) diff --git a/jarvis/skills/research/wikipedia/dialog/fr-fr/searching.dialog b/jarvis/skills/research/wikipedia/dialog/fr-fr/searching.dialog new file mode 100644 index 0000000..358a8de --- /dev/null +++ b/jarvis/skills/research/wikipedia/dialog/fr-fr/searching.dialog @@ -0,0 +1,3 @@ +Un moment, je cherche pour {{query}} +Laissez moi regarder pour {{query}} +Je vais regarder sur Wikipédia pour {{query}} \ No newline at end of file diff --git a/jarvis/skills/research/wikipedia/regex/fr-fr/article.title.rx b/jarvis/skills/research/wikipedia/regex/fr-fr/article.title.rx new file mode 100644 index 0000000..20617f4 --- /dev/null +++ b/jarvis/skills/research/wikipedia/regex/fr-fr/article.title.rx @@ -0,0 +1 @@ +.*(wiki|sur|au sujet de|wikipedia)(?! (sur|au sujet)) (?P.+) \ No newline at end of file diff --git a/jarvis/skills/research/wikipedia/vocab/fr-fr/Wikipedia.voc b/jarvis/skills/research/wikipedia/vocab/fr-fr/Wikipedia.voc new file mode 100644 index 0000000..4888936 --- /dev/null +++ b/jarvis/skills/research/wikipedia/vocab/fr-fr/Wikipedia.voc @@ -0,0 +1,6 @@ +wiki +wikipédia +parle moi de +parle nous de +que dit wikipédia à propos de +cherche \ No newline at end of file diff --git a/jarvis/skills/research/wikipedia_search/__init__.py b/jarvis/skills/research/wikipedia_search/__init__.py deleted file mode 100644 index e69de29..0000000