dream
168 строк · 6.5 Кб
1# Copyright 2017 Neural Networks and Deep Learning lab, MIPT
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import pathlib
16import uuid
17import logging
18
19from programy.clients.embed.basic import EmbeddedDataFileBot
20
21from common.dialogflow_framework.programy.text_preprocessing import clean_text
22
23logger = logging.getLogger(__name__)
24
25
26class MindfulDataFileBot(EmbeddedDataFileBot):
27def ask_question(self, userid, question):
28client_context = self.create_client_context(userid)
29return self.renderer.render(client_context, self.process_question(client_context, question))
30
31def __call__(self, human_utterances, clean_response=False):
32userid = uuid.uuid4().hex
33for uttr in human_utterances:
34uttr = clean_text(uttr)
35response = self.ask_question(userid, uttr)
36response = response if response else ""
37response = clean_text(response) if clean_response else response
38return response
39
40
41def get_configuration_files(
42aiml_dirs=None,
43learnf_dirs=None,
44sets_dirs=None,
45maps_dirs=None,
46rdfs_dirs=None,
47conversations_dir=None,
48services_dir=None,
49properties_txt_file=None,
50defaults_txt_file=None,
51denormals_txt_file=None,
52normals_txt_file=None,
53genders_txt_file=None,
54persons_txt_file=None,
55person2s_txt_file=None,
56triggers_txt_file=None,
57regexes_txt_file=None,
58spellings_txt_file=None,
59duplicates_txt_file=None,
60errors_txt_file=None,
61licenses_keys_file=None,
62usergroups_yaml_file=None,
63preprocessors_conf_file=None,
64postprocessors_conf_file=None,
65postquestionprocessors_conf_file=None,
66):
67# example:
68# ############# From storage
69# aiml_dirs -> aiml = [".../storage/categories"],
70# learnf_dirs -> learnf = [".../storage/categories/learnf"],
71# sets_dirs -> sets = [".../storage/sets"],
72# maps_dirs -> maps = [".../storage/maps"],
73# rdfs_dirs -> rdfs = [".../storage/rdfs"],
74# conversations_dir -> conversations = ".../storage/conversations",
75# services_dir -> services = ".../storage/services",
76# ############# From properties
77# properties_txt_file -> properties = ".../storage/properties/properties.txt",
78# defaults_txt_file -> defaults = ".../storage/properties/defaults.txt",
79# ############# From lookups
80# denormals_txt_file -> denormals = ".../storage/lookups/denormal.txt",
81# normals_txt_file -> normals = ".../storage/lookups/normal.txt",
82# genders_txt_file -> genders = ".../storage/lookups/gender.txt",
83# persons_txt_file -> persons = ".../storage/lookups/person.txt",
84# person2s_txt_file -> person2s = ".../storage/lookups/person2.txt",
85# ############# From triggers
86# triggers_txt_file -> triggers = ".../storage/triggers/triggers.txt",
87# ############# From regex
88# regexes_txt_file -> regexes = ".../storage/regex/regex-templates.txt",
89# ############# From spelling
90# spellings_txt_file -> spellings = ".../storage/spelling/corpus.txt",
91# ############# From debug
92# duplicates_txt_file -> duplicates = ".../storage/debug/duplicates.txt",
93# errors_txt_file -> errors = ".../storage/debug/errors.txt",
94# ############# From licenses
95# licenses_keys_file -> licenses = ".../storage/licenses/license.keys",
96# ############# From security
97# usergroups_yaml_file -> usergroups = ".../storage/security/usergroups.yaml",
98# ############# From processing
99# preprocessors_conf_file -> preprocessors = ".../storage/processing/preprocessors.conf",
100# postprocessors_conf_file -> postprocessors = ".../storage/processing/postprocessors.conf",
101# postquestionprocessors_conf_file -> postquestionprocessors = ".../storage/processing/postquestionprocessors.conf",
102files = {}
103if aiml_dirs:
104files["aiml"] = aiml_dirs
105if learnf_dirs:
106files["learnf"] = learnf_dirs
107if sets_dirs:
108files["sets"] = sets_dirs
109if maps_dirs:
110files["maps"] = maps_dirs
111if rdfs_dirs:
112files["rdfs"] = rdfs_dirs
113if conversations_dir:
114files["conversations"] = conversations_dir
115if services_dir:
116files["services"] = services_dir
117if properties_txt_file:
118files["properties"] = properties_txt_file
119if defaults_txt_file:
120files["defaults"] = defaults_txt_file
121if denormals_txt_file:
122files["denormals"] = denormals_txt_file
123if normals_txt_file:
124files["normals"] = normals_txt_file
125if genders_txt_file:
126files["genders"] = genders_txt_file
127if persons_txt_file:
128files["persons"] = persons_txt_file
129if person2s_txt_file:
130files["person2s"] = person2s_txt_file
131if triggers_txt_file:
132files["triggers"] = triggers_txt_file
133if regexes_txt_file:
134files["regexes"] = regexes_txt_file
135if spellings_txt_file:
136files["spellings"] = spellings_txt_file
137if duplicates_txt_file:
138files["duplicates"] = duplicates_txt_file
139if errors_txt_file:
140files["errors"] = errors_txt_file
141if licenses_keys_file:
142files["licenses"] = licenses_keys_file
143if usergroups_yaml_file:
144files["usergroups"] = usergroups_yaml_file
145if preprocessors_conf_file:
146files["preprocessors"] = preprocessors_conf_file
147if postprocessors_conf_file:
148files["postprocessors"] = postprocessors_conf_file
149if postquestionprocessors_conf_file:
150files["postquestionprocessors"] = postquestionprocessors_conf_file
151
152dropped_files = {}
153for name, file in files.items():
154if isinstance(file, list):
155sub_files = [pathlib.Path(sub_file) for sub_file in file]
156sub_files = [sub_file for sub_file in sub_files if not sub_file.exists()]
157if sub_files:
158dropped_files[name] = sub_files
159elif not pathlib.Path(file).exists():
160dropped_files[name] = pathlib.Path(file)
161if dropped_files:
162raise Exception(f"dropped_files={dropped_files} is not empty")
163return files
164
165
166def get_programy_model(configuration_files):
167model = MindfulDataFileBot(configuration_files)
168return model
169