Skip to content

Commit

Permalink
feat: list join util
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Nov 7, 2024
1 parent c71926e commit 6810e19
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 7 deletions.
4 changes: 4 additions & 0 deletions ovos_workshop/res/text/az/word_connectors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"and": "",
"or": "ya"
}
4 changes: 4 additions & 0 deletions ovos_workshop/res/text/ca/word_connectors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"and": "i",
"or": "o"
}
File renamed without changes.
4 changes: 4 additions & 0 deletions ovos_workshop/res/text/cs/word_connectors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"and": "a",
"or": "nebo"
}
4 changes: 4 additions & 0 deletions ovos_workshop/res/text/da/word_connectors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"and": "og",
"or": "eller"
}
File renamed without changes.
4 changes: 4 additions & 0 deletions ovos_workshop/res/text/de/word_connectors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"and": "und",
"or": "oder"
}
File renamed without changes.
4 changes: 4 additions & 0 deletions ovos_workshop/res/text/en/word_connectors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"and": "and",
"or": "or"
}
4 changes: 4 additions & 0 deletions ovos_workshop/res/text/fa/word_connectors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"and": "و",
"or": "یا"
}
4 changes: 4 additions & 0 deletions ovos_workshop/res/text/pl/word_connectors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"and": "oraz",
"or": "lub"
}
File renamed without changes.
4 changes: 4 additions & 0 deletions ovos_workshop/res/text/sl/word_connectors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"and": "in",
"or": "ali"
}
4 changes: 4 additions & 0 deletions ovos_workshop/res/text/uk/word_connectors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"and": "та",
"or": "або"
}
13 changes: 7 additions & 6 deletions ovos_workshop/skills/common_query_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ def __init__(self, *args, **kwargs):
}
super().__init__(*args, **kwargs)

noise_words_filepath = f"text/{self.lang}/noise_words.list"
default_res = f"{dirname(dirname(__file__))}/res/text/{self.lang}" \
lang = self.lang.split("-")[0]
noise_words_filepath = f"text/{lang}/noise_words.list"
default_res = f"{dirname(dirname(__file__))}/res/text/{lang}" \
f"/noise_words.list"
noise_words_filename = \
resolve_resource_file(noise_words_filepath,
Expand All @@ -79,7 +80,7 @@ def __init__(self, *args, **kwargs):
if noise_words_filename:
with open(noise_words_filename) as f:
translated_noise_words = f.read().strip()
self._translated_noise_words[self.lang] = \
self._translated_noise_words[lang] = \
translated_noise_words.split()

@property
Expand All @@ -89,13 +90,13 @@ def translated_noise_words(self) -> List[str]:
"""
log_deprecation("self.translated_noise_words will become a "
"private variable", "0.1.0")
return self._translated_noise_words.get(self.lang, [])
return self._translated_noise_words.get(self.lang.split("-")[0], [])

@translated_noise_words.setter
def translated_noise_words(self, val: List[str]):
log_deprecation("self.translated_noise_words will become a "
"private variable", "0.1.0")
self._translated_noise_words[self.lang] = val
self._translated_noise_words[self.lang.split("-")[0]] = val

def bind(self, bus):
"""Overrides the default bind method of MycroftSkill.
Expand Down Expand Up @@ -179,7 +180,7 @@ def remove_noise(self, phrase: str, lang: str = None) -> str:
@param lang: language of `phrase`, else defaults to `self.lang`
@return: cleaned `phrase` with extra words removed
"""
lang = lang or self.lang
lang = (lang or self.lang).split("-")[0]
phrase = ' ' + phrase + ' '
for word in self._translated_noise_words.get(lang, []):
mtch = ' ' + word + ' '
Expand Down
76 changes: 75 additions & 1 deletion ovos_workshop/skills/ovos.py
Original file line number Diff line number Diff line change
Expand Up @@ -2064,7 +2064,7 @@ def ask_selection(self, options: List[str], dialog: str = '',
number = pronounce_number(idx + 1, self.lang)
self.speak(f"{number}, {opt}", wait=True)
else:
opt_str = join_list(options, "or", lang=self.lang) + "?"
opt_str = join_word_list(options, "or", sep=",", lang=self.lang) + "?"
self.speak(opt_str, wait=True)

resp = self.get_response(dialog=dialog, data=data)
Expand Down Expand Up @@ -2502,3 +2502,77 @@ def __init__(self, skill: OVOSSkill):
ui_directories = get_ui_directories(skill.root_dir)
GUIInterface.__init__(self, skill_id=skill_id, bus=bus, config=config,
ui_directories=ui_directories)


def _get_and_word(lang):
""" Helper to get word translations
Args:
lang (str, optional): an optional BCP-47 language code, if omitted
the default language will be used.
Returns:
str: translated version of resource name
"""
res_file = f"{dirname(dirname(__file__))}/res/text/{lang}" \
f"/word_connectors.json"
if not os.path.isfile(res_file):
LOG.warning(f"untranslated file: {res_file}")
return ", "
with open(res_file) as f:
w = json.load(f)["and"]
return w

def _get_or_word(lang):
""" Helper to get word translations
Args:
lang (str, optional): an optional BCP-47 language code, if omitted
the default language will be used.
Returns:
str: translated version of resource name
"""
res_file = f"{dirname(dirname(__file__))}/res/text/{lang}" \
f"/word_connectors.json"
if not os.path.isfile(res_file):
LOG.warning(f"untranslated file: {res_file}")
return ", "
with open(res_file) as f:
w = json.load(f)["or"]
return w


def join_word_list(items: List[str], connector: str, sep: str, lang:str) -> str:
""" Join a list into a phrase using the given connector word
Examples:
join_word_list([1,2,3], "or") -> "1, 2 or 3"
join_word_list([1,2,3], "and") -> "1, 2 and 3"
join_word_list([1,2,3], "and", ";") -> "1; 2 and 3"
Args:
items (array): items to be joined
connector (str): connecting word (resource name), like "and" or "or"
sep (str, optional): separator character, default = ","
lang (str, optional): an optional BCP-47 language code, if omitted
the default language will be used.
Returns:
str: the connected list phrase
"""
cons = {
"and": _get_and_word,
"or": _get_or_word
}
if not items:
return ""
if len(items) == 1:
return str(items[0])

if not sep:
sep = ", "
else:
sep += " "
return (sep.join(str(item) for item in items[:-1]) +
" " + cons[connector](lang) +
" " + items[-1])

0 comments on commit 6810e19

Please sign in to comment.