Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: list join util #269

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "və",
"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"
}
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"
}
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"
}
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"
}
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
61 changes: 59 additions & 2 deletions ovos_workshop/skills/ovos.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import json
import os
import re
import sys
Expand Down Expand Up @@ -420,7 +421,8 @@ def settings(self, val: dict):
"""
Update settings specific to this skill
"""
LOG.warning("Skills are not supposed to override self.settings, expect breakage! Set individual dict keys instead")
LOG.warning(
"Skills are not supposed to override self.settings, expect breakage! Set individual dict keys instead")
assert isinstance(val, dict)
# init method
if self._settings is None:
Expand Down Expand Up @@ -2064,7 +2066,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 +2504,58 @@ 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_word(lang, connector):
""" 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)[connector]
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_word(lang, "and"),
"or": _get_word(lang, "or")
}
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] +
" " + items[-1])
Loading