Made Skill class a lot more clean and readable

This commit is contained in:
Mathieu B 2021-08-02 20:04:28 +02:00
parent e90e0dc547
commit 7c5f0114c9
2 changed files with 67 additions and 40 deletions

View File

@ -30,13 +30,10 @@ class Skill:
data = {}
file = self.path + "/dialog/" + languages_utils.get_language() + "/" + dialog + ".dialog"
if os.path.exists(file):
with open(file, "r") as infile:
random_line = random.choice(infile.readlines())
random_line = get_random_line_from_file(file)
for key, val in data.items():
if "{{" + key + "}}" in random_line:
# TODO: replace when found a better TTS engine for french
# as the french tts don't support float in sentence, convert it to an integer
if is_float(val):
@ -45,8 +42,6 @@ class Skill:
random_line = random_line.replace("{{" + key + "}}", val)
infile.close()
self.speak(random_line)
return "Error, dialog not found for : " + dialog
@ -64,46 +59,78 @@ class Skill:
def register_entities_adapt(self):
path = self.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]
all_lines_by_file_dict = get_lines_of_all_files_in_path(path, return_as_dict_with_filename=True)
intent_manager.register_entity_adapt(line.replace('\n', ''), filename, self.name)
for filename in all_lines_by_file_dict:
for line in all_lines_by_file_dict.get(filename):
intent_manager.register_entity_adapt(line, filename, self.name)
def register_entities_padatious(self):
path = self.path + "/vocab/" + languages_utils.get_language() + "/*.entity"
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(".entity")[0]
all_lines_by_file_dict = get_lines_of_all_files_in_path(path, return_as_dict_with_filename=True)
intent_manager.register_entity_adapt(line.replace('\n', ''), filename, self.name)
for filename in all_lines_by_file_dict:
intent_manager.register_entity_padatious(filename, all_lines_by_file_dict.get(filename))
def register_regex(self):
path = self.path + "/regex/" + languages_utils.get_language() + "/*.rx"
files = glob.glob(path, recursive=True)
for file in files:
with open(file, "r") as infile:
result = get_lines_of_all_files_in_path(path)
for line in result:
intent_manager.register_regex_adapt(line, self.name)
def get_random_line_from_file(filepath):
if os.path.exists(filepath):
with open(filepath, "r") as infile:
random_line = random.choice(infile.readlines())
infile.close()
return random_line
else:
print("File " + filepath + " doesn't exist...")
def get_all_lines_from_file(filepath):
if os.path.exists(filepath):
with open(file=filepath, mode="r") as infile:
lines = []
for line in infile.readlines():
intent_manager.register_regex_adapt(line.replace('\n', ''), self.name)
lines.append(line.removesuffix('\n'))
infile.close()
return lines
else:
print("File " + filepath + " doesn't exist...")
def get_lines_of_all_files_in_path(path, return_as_dict_with_filename=False):
files = glob.glob(path, recursive=True)
result = dict()
result_list = []
for file in files:
lines = get_all_lines_from_file(file)
if return_as_dict_with_filename:
filename = file.split("/")[-1].split('.')[0]
result[filename] = lines
else:
result_list.extend(lines)
if return_as_dict_with_filename:
return result
return result_list
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
return get_all_lines_from_file(path)
def is_float(value):

View File

@ -17,8 +17,8 @@ def register_entity_adapt(entity_value, entity_type, domain):
# print("[Adapt]: Added entity with type " + entity_type + " for " + domain)
def register_entity_padatious(entity_name, list):
padatious_intents_container.add_entity(entity_name, list)
def register_entity_padatious(entity_name, file_lines_list):
padatious_intents_container.add_entity(entity_name, file_lines_list)
# print("[Padatious]: Added entity with name " + entity_name + " with " str(len(list)) + "examples.")