Skip to content

Commit

Permalink
add voc_list helper function (#54)
Browse files Browse the repository at this point in the history
* add `voc_list` helper function

* ovos public method
  • Loading branch information
emphasize authored Mar 2, 2023
1 parent 01dfd24 commit c575e24
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
31 changes: 21 additions & 10 deletions ovos_workshop/skills/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from dataclasses import dataclass
from hashlib import md5
from inspect import signature
from typing import List
from itertools import chain
from os.path import join, abspath, dirname, basename, isfile
from threading import Event
Expand Down Expand Up @@ -133,7 +134,7 @@ def __init__(self, name=None, bus=None, resources_dir=None,
self.reload_skill = True #: allow reloading (default True)

self.events = EventContainer(bus)
self.voc_match_cache = {}
self._voc_cache = {}

# loaded lang file resources
self._lang_resources = {}
Expand Down Expand Up @@ -1047,6 +1048,20 @@ def ask_selection(self, options, dialog='',
resp = match
return resp

# method not present in mycroft-core
def _voc_list(self, voc_filename, lang=None) -> List[str]:

lang = lang or self.lang
cache_key = lang + voc_filename

if cache_key not in self._voc_cache:
vocab = self._resources.load_vocabulary_file(voc_filename) or \
CoreResources(lang).load_vocabulary_file(voc_filename)
if vocab:
self._voc_cache[cache_key] = list(chain(*vocab))

return self._voc_cache.get(cache_key) or []

def voc_match(self, utt, voc_filename, lang=None, exact=False):
"""Determine if the given utterance contains the vocabulary provided.
Expand All @@ -1070,21 +1085,17 @@ def voc_match(self, utt, voc_filename, lang=None, exact=False):
bool: True if the utterance has the given vocabulary it
"""
match = False
lang = lang or self.lang
cache_key = lang + voc_filename
if cache_key not in self.voc_match_cache:
vocab = self._resources.load_vocabulary_file(voc_filename) or \
CoreResources(lang).load_vocabulary_file(voc_filename)
self.voc_match_cache[cache_key] = list(chain(*vocab))
if utt:
_vocs = self._voc_list(voc_filename, lang)

if utt and _vocs:
if exact:
# Check for exact match
match = any(i.strip() == utt
for i in self.voc_match_cache[cache_key])
for i in _vocs)
else:
# Check for matches against complete words
match = any([re.match(r'.*\b' + i + r'\b.*', utt)
for i in self.voc_match_cache[cache_key]])
for i in _vocs])

return match

Expand Down
23 changes: 16 additions & 7 deletions ovos_workshop/skills/ovos.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
import time
from typing import List

from ovos_utils.intents import IntentBuilder, Intent
from ovos_utils.log import LOG
Expand Down Expand Up @@ -132,17 +133,25 @@ def voc_match(self, *args, **kwargs):
except FileNotFoundError:
return False

def remove_voc(self, utt, voc_filename, lang=None):
""" removes any entry in .voc file from the utterance """
lang = lang or self.lang
cache_key = lang + voc_filename
def voc_list(self, voc_filename, lang=None) -> List[str]:
"""
Get vocabulary list and cache the results
Args:
voc_filename (str): Name of vocabulary file (e.g. 'yes' for
'res/text/en-us/yes.voc')
lang (str): Language code, defaults to self.lang
if cache_key not in self.voc_match_cache:
self.voc_match(utt, voc_filename, lang)
Returns:
list: List of vocabulary found in voc_filename
"""
return self._voc_list(voc_filename, lang)

def remove_voc(self, utt, voc_filename, lang=None):
""" removes any entry in .voc file from the utterance """
if utt:
# Check for matches against complete words
for i in self.voc_match_cache.get(cache_key) or []:
for i in self.voc_list(voc_filename, lang):
# Substitute only whole words matching the token
utt = re.sub(r'\b' + i + r"\b", "", utt)

Expand Down
9 changes: 9 additions & 0 deletions test/unittests/skills/test_mycroft_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,15 @@ def test_voc_match_exact(self):
exact=True))
self.assertFalse(s.voc_match("would you please turn off the lights",
"turn_off_test", exact=True))

def test_voc_list(self):
s = SimpleSkill1()
s.root_dir = abspath(dirname(__file__))

self.assertEqual(s._voc_list("turn_off_test"),
["turn off", "switch off"])
cache_key = s.lang+"turn_off_test"
self.assertIn(cache_key, s._voc_cache)

def test_translate_locations(self):
"""Assert that the a translatable list can be loaded from dialog and
Expand Down

0 comments on commit c575e24

Please sign in to comment.