diff --git a/ovos_workshop/res/text/es/word_connectors.json b/ovos_workshop/res/text/es/word_connectors.json new file mode 100644 index 0000000..090a7c5 --- /dev/null +++ b/ovos_workshop/res/text/es/word_connectors.json @@ -0,0 +1,4 @@ +{ + "and": "y", + "or": "o" +} diff --git a/ovos_workshop/skills/ovos.py b/ovos_workshop/skills/ovos.py index d025dc0..00e4053 100644 --- a/ovos_workshop/skills/ovos.py +++ b/ovos_workshop/skills/ovos.py @@ -2535,6 +2535,9 @@ def join_word_list(items: List[str], connector: str, sep: str, lang: str) -> str """ if lang.startswith("it"): return _join_word_list_it(items, connector, sep) + elif lang.startswith("es"): + return _join_word_list_es(items, connector, sep) + cons = { "and": _get_word(lang, "and"), "or": _get_word(lang, "or") @@ -2568,15 +2571,43 @@ def _join_word_list_it(items: List[str], connector: str, sep: str = ",") -> str: else: sep += " " - # Join the list with Italian euphonic rules applied to the last connector - joined_string = sep.join(item for item in items[:-1]) + final_connector = cons[connector] + if len(items) > 2: + joined_string = sep.join(item for item in items[:-1]) + else: + joined_string = items[0] # Check for euphonic transformation cases for "e" and "o" if cons[connector] == "e" and items[-1][0].lower() == "e": final_connector = "ed" elif cons[connector] == "o" and items[-1][0].lower() == "o": final_connector = "od" - else: - final_connector = cons[connector] return f"{joined_string} {final_connector} {items[-1]}" + +def _join_word_list_es(items: List[str], connector: str, sep: str = ",") -> str: + cons = { + "and": _get_word("es", "and"), + "or": _get_word("es", "or") + } + if not items: + return "" + if len(items) == 1: + return str(items[0]) + + if not sep: + sep = ", " + else: + sep += " " + + final_connector = cons[connector] + if len(items) > 2: + joined_string = sep.join(item for item in items[:-1]) + else: + joined_string = items[0] + + # Check for euphonic transformation cases for "y" + if cons[connector] == "y" and items[-1][0].lower() == "i": + final_connector = "e" + + return f"{joined_string} {final_connector} {items[-1]}" diff --git a/test/unittests/test_euphony.py b/test/unittests/test_euphony.py index 6eab84c..2cade93 100644 --- a/test/unittests/test_euphony.py +++ b/test/unittests/test_euphony.py @@ -1,6 +1,6 @@ import unittest -from ovos_workshop.skills.ovos import _join_word_list_it +from ovos_workshop.skills.ovos import _join_word_list_it, _join_word_list_es class TestJoinWordListIt(unittest.TestCase): @@ -43,5 +43,13 @@ def test_single_word(self): self.assertEqual(result, "mare") +class TestJoinWordListEs(unittest.TestCase): + + def test_euphonic_conjunction_and(self): + # Test euphonic transformation from "y" to "e" + result = _join_word_list_es(["Juan", "Irene"], "and") + self.assertEqual(result, "Juan e Irene") + + if __name__ == "__main__": unittest.main()