2021-07-27 23:10:01 +02:00
|
|
|
import glob
|
|
|
|
import os
|
2021-07-28 19:31:47 +02:00
|
|
|
import types
|
2021-07-27 23:10:01 +02:00
|
|
|
|
|
|
|
from jarvis import get_path_file
|
|
|
|
from jarvis.skills import intent_manager
|
|
|
|
from jarvis.utils import languages_utils
|
|
|
|
|
|
|
|
|
|
|
|
class Skill:
|
2021-07-28 20:35:20 +02:00
|
|
|
def __init__(self, name):
|
2021-07-27 23:10:01 +02:00
|
|
|
self.name = name
|
2021-07-28 20:35:20 +02:00
|
|
|
|
|
|
|
path = self.__module__.split(".")
|
|
|
|
self.category = path[2]
|
|
|
|
self.skill_folder = path[3]
|
2021-07-27 23:10:01 +02:00
|
|
|
|
|
|
|
def register(self):
|
|
|
|
self.register_entities()
|
|
|
|
self.register_regex()
|
2021-07-28 18:05:34 +02:00
|
|
|
print("[" + self.name + "] Registered entity/entities and regex(s)")
|
2021-07-27 23:10:01 +02:00
|
|
|
|
|
|
|
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]
|
|
|
|
|
2021-07-29 20:16:22 +02:00
|
|
|
intent_manager.register_entity_adapt(line.replace('\n', ''), filename, self.name)
|
2021-07-27 23:10:01 +02:00
|
|
|
|
|
|
|
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():
|
2021-07-29 20:16:22 +02:00
|
|
|
intent_manager.register_regex_adapt(line.replace('\n', ''), self.name)
|
|
|
|
|
|
|
|
|
|
|
|
def get_array_for_intent_file(filename, category, skill_folder):
|
|
|
|
path = os.path.dirname(get_path_file.__file__) + "/skills/" + category + "/" + skill_folder
|
|
|
|
path = path + "/vocab/" + languages_utils.get_language() + "/" + filename
|
|
|
|
|
|
|
|
with open(file=path, mode="r") as infile:
|
|
|
|
lines = []
|
|
|
|
|
|
|
|
for line in infile.readlines():
|
|
|
|
lines.append(line.replace('\n', ''))
|
|
|
|
|
|
|
|
return lines
|
2021-07-28 18:05:34 +02:00
|
|
|
|
2021-07-28 19:31:47 +02:00
|
|
|
|
|
|
|
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__("__"):
|
2021-07-29 20:16:22 +02:00
|
|
|
intent_type = getattr(val, "_type", None)
|
2021-07-29 14:57:55 +02:00
|
|
|
|
2021-07-29 20:16:22 +02:00
|
|
|
if intent_type is not None:
|
2021-07-29 14:57:55 +02:00
|
|
|
properties = getattr(val, "_data", None)
|
|
|
|
|
|
|
|
if properties is not None:
|
2021-07-29 20:16:22 +02:00
|
|
|
if intent_type == 'adapt':
|
2021-07-29 14:57:55 +02:00
|
|
|
intent = properties[0]
|
|
|
|
intent_name = intent.name
|
2021-07-30 12:31:59 +02:00
|
|
|
|
|
|
|
intent_manager.intents_handlers_adapt[f"{intent_name}"] = [getattr(cls, key), name, key,
|
|
|
|
attrs['__module__']]
|
2021-07-29 20:16:22 +02:00
|
|
|
elif intent_type == 'padatious':
|
2021-07-29 14:57:55 +02:00
|
|
|
intent_file = properties[0]
|
2021-07-29 20:16:22 +02:00
|
|
|
intent_name = properties[1]
|
|
|
|
|
|
|
|
intent_category = str(attrs['__module__']).split('.')[2]
|
|
|
|
skill_folder = str(attrs['__module__']).split('.')[3]
|
|
|
|
|
2021-07-30 12:31:59 +02:00
|
|
|
intent_manager.intents_handlers_padatious[f"{intent_name}"] = [key,
|
2021-07-29 20:16:22 +02:00
|
|
|
get_array_for_intent_file(
|
|
|
|
intent_file,
|
|
|
|
intent_category,
|
2021-07-30 12:31:59 +02:00
|
|
|
skill_folder),
|
|
|
|
attrs['__module__']]
|