diff --git a/ovos_padatious/intent.py b/ovos_padatious/intent.py index 0623ede..588ac7b 100644 --- a/ovos_padatious/intent.py +++ b/ovos_padatious/intent.py @@ -11,7 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +import os import json import math from os.path import join @@ -47,6 +47,7 @@ def match(self, sent, entities=None): def save(self, folder): prefix = join(folder, self.name) + os.makedirs(folder, exist_ok=True) with open(prefix + '.hash', 'wb') as f: f.write(self.hash) self.simple_intent.save(prefix) diff --git a/ovos_padatious/opm.py b/ovos_padatious/opm.py index 1dd1d6e..4d51ca3 100644 --- a/ovos_padatious/opm.py +++ b/ovos_padatious/opm.py @@ -51,6 +51,8 @@ def _match_level(self, utterances, limit, lang=None, message: Optional[Message] limit (float): required confidence level. """ m: IntentHandlerMatch = self.service._match_level(utterances, limit, lang, message) + if not m: + return None return IntentMatch("Padatious", m.match_type, m.match_data, m.skill_id, m.utterance) def match_high(self, utterances, lang=None, message=None) -> Optional[IntentMatch]: diff --git a/ovos_padatious/train_data.py b/ovos_padatious/train_data.py index 1520ea2..678dfb8 100644 --- a/ovos_padatious/train_data.py +++ b/ovos_padatious/train_data.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from ovos_padatious.util import tokenize, expand_parentheses, remove_comments +from ovos_padatious.util import tokenize, expand_lines class TrainData(object): @@ -25,8 +25,8 @@ def __init__(self): self.sent_lists = {} def add_lines(self, name, lines): - lines = remove_comments(lines) - self.sent_lists[name] = sum([expand_parentheses(tokenize(line)) for line in lines], []) + lines = expand_lines(lines) + self.sent_lists[name] = sum([tokenize(line) for line in lines], []) self.sent_lists[name] = [i for i in self.sent_lists[name] if i] def remove_lines(self, name): diff --git a/ovos_padatious/trainable.py b/ovos_padatious/trainable.py index e46ea32..37d70c4 100644 --- a/ovos_padatious/trainable.py +++ b/ovos_padatious/trainable.py @@ -11,11 +11,11 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +import os from abc import ABCMeta, abstractmethod -class Trainable(object): +class Trainable: __metaclass__ = ABCMeta def __init__(self, name, hsh=b''): @@ -23,10 +23,12 @@ def __init__(self, name, hsh=b''): self.hash = hsh def load_hash(self, prefix): - with open(prefix + '.hash', 'rb') as f: - self.hash = f.read() + if os.path.isfile(prefix + '.hash'): + with open(prefix + '.hash', 'rb') as f: + self.hash = f.read() def save_hash(self, prefix): + os.makedirs(os.path.dirname(prefix), exist_ok=True) with open(prefix + '.hash', 'wb') as f: f.write(self.hash) diff --git a/ovos_padatious/util.py b/ovos_padatious/util.py index f449907..9d76ba4 100644 --- a/ovos_padatious/util.py +++ b/ovos_padatious/util.py @@ -12,6 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. +from ovos_utils import flatten_list +from ovos_utils.bracket_expansion import expand_template + from xxhash import xxh32 from ovos_padatious.bracket_expansion import SentenceTreeParser @@ -92,6 +95,11 @@ def expand_parentheses(sent): return SentenceTreeParser(sent).expand_parentheses() +def expand_lines(lines): + lines = [expand_template(i) for i in remove_comments(lines)] + return flatten_list(lines) + + def remove_comments(lines): return [i for i in lines if not i.startswith('//')] diff --git a/requirements.txt b/requirements.txt index a4e3c9b..7ed1abf 100755 --- a/requirements.txt +++ b/requirements.txt @@ -2,5 +2,5 @@ fann2>=1.0.7, < 1.1.0 xxhash ovos-plugin-manager>=0.5.0,<1.0.0 ovos-workshop>=0.1.7,<4.0.0 -ovos-utils>=0.3.4,<1.0.0 +ovos-utils>=0.6.0,<1.0.0 langcodes \ No newline at end of file