|
| 1 | +# Copyright 2026 OpenVoiceOS |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +""" |
| 15 | +Utility functions for skills, including word list joining with language-specific euphony |
| 16 | +and error traceback formatting. |
| 17 | +""" |
| 18 | + |
| 19 | +from typing import Dict, List, Optional |
| 20 | + |
| 21 | +from ovos_utils.log import LOG |
| 22 | +from ovos_workshop.resource_files import CoreResources |
| 23 | + |
| 24 | + |
| 25 | +def simple_trace(stack_trace: List[str]) -> str: |
| 26 | + """Generate a simplified traceback. |
| 27 | +
|
| 28 | + Args: |
| 29 | + stack_trace: Formatted stack trace (each string ends with \\n) |
| 30 | +
|
| 31 | + Returns: |
| 32 | + Stack trace with any empty lines removed and last line removed |
| 33 | + """ |
| 34 | + stack_trace = stack_trace[:-1] |
| 35 | + tb = 'Traceback:\n' |
| 36 | + for line in stack_trace: |
| 37 | + if line.strip(): |
| 38 | + tb += line |
| 39 | + return tb |
| 40 | + |
| 41 | + |
| 42 | +def get_word(lang: str, connector: str) -> str: |
| 43 | + """Get connector word translation for a language. |
| 44 | +
|
| 45 | + Args: |
| 46 | + lang: BCP-47 language code |
| 47 | + connector: Connector key ("and" or "or") |
| 48 | +
|
| 49 | + Returns: |
| 50 | + Translated connector word, or ", " as fallback |
| 51 | + """ |
| 52 | + data = CoreResources(lang).load_json_file("word_connectors") |
| 53 | + if connector in data: |
| 54 | + return data[connector] |
| 55 | + LOG.warning(f"untranslated word connector '{connector}' for lang: {lang}") |
| 56 | + return ", " |
| 57 | + |
| 58 | + |
| 59 | +def load_euphony_rules(lang: str) -> Optional[Dict]: |
| 60 | + """Load euphony.json for a language if it exists. |
| 61 | +
|
| 62 | + Args: |
| 63 | + lang: BCP-47 language tag |
| 64 | +
|
| 65 | + Returns: |
| 66 | + Dict with euphony rules or None if not found |
| 67 | + """ |
| 68 | + try: |
| 69 | + return CoreResources(lang).load_json_file("euphony") |
| 70 | + except Exception: |
| 71 | + return None |
| 72 | + |
| 73 | + |
| 74 | +def normalize_word(word: str, rules: dict) -> str: |
| 75 | + """Normalize a word for euphony comparison per language rules. |
| 76 | +
|
| 77 | + Args: |
| 78 | + word: The word to normalize |
| 79 | + rules: The euphony rules dict (may contain normalize section) |
| 80 | +
|
| 81 | + Returns: |
| 82 | + Normalized word string |
| 83 | + """ |
| 84 | + if not word: |
| 85 | + return word |
| 86 | + |
| 87 | + normalize = rules.get("normalize", {}) |
| 88 | + |
| 89 | + # Strip leading h if language specifies it |
| 90 | + if normalize.get("strip_leading_h"): |
| 91 | + word = word.lstrip("h") |
| 92 | + |
| 93 | + # Apply character replacements if language specifies them |
| 94 | + replacements = normalize.get("replace_accents", {}) |
| 95 | + for old, new in replacements.items(): |
| 96 | + word = word.replace(old, new) |
| 97 | + |
| 98 | + return word |
| 99 | + |
| 100 | + |
| 101 | +def apply_euphony(connector: str, next_word: str, rules: dict) -> str: |
| 102 | + """Apply euphony transformation to connector based on rules. |
| 103 | +
|
| 104 | + Args: |
| 105 | + connector: The connector word (e.g., "e", "y", "o") |
| 106 | + next_word: The word following the connector |
| 107 | + rules: The euphony rules dict |
| 108 | +
|
| 109 | + Returns: |
| 110 | + Potentially transformed connector word |
| 111 | + """ |
| 112 | + if not next_word or not rules: |
| 113 | + return connector |
| 114 | + |
| 115 | + # Check each rule to see if it applies |
| 116 | + for rule in rules.get("rules", []): |
| 117 | + rule_connector = rule.get("connector") |
| 118 | + if rule_connector != connector: |
| 119 | + continue |
| 120 | + |
| 121 | + # Check the condition type |
| 122 | + condition = rule.get("condition") |
| 123 | + normalized_next = normalize_word(next_word.lower(), rules) |
| 124 | + first_char = normalized_next[0] if normalized_next else "" |
| 125 | + |
| 126 | + if condition == "starts_with_vowel": |
| 127 | + vowels = rule.get("vowels", []) |
| 128 | + if first_char in vowels: |
| 129 | + return rule.get("replace_with", connector) |
| 130 | + |
| 131 | + elif condition == "starts_with_letter": |
| 132 | + letters = rule.get("letters", []) |
| 133 | + if first_char in letters: |
| 134 | + return rule.get("replace_with", connector) |
| 135 | + |
| 136 | + elif condition == "starts_with_any_except": |
| 137 | + # Apply transformation if word does NOT start with any of the excluded patterns |
| 138 | + excluded = rule.get("excluded_patterns", []) |
| 139 | + excluded_match = any(normalized_next.startswith(p) for p in excluded) |
| 140 | + if not excluded_match: |
| 141 | + letters = rule.get("letters", []) |
| 142 | + if first_char in letters: |
| 143 | + return rule.get("replace_with", connector) |
| 144 | + |
| 145 | + return connector |
| 146 | + |
| 147 | + |
| 148 | +def join_word_list(items: List[str], connector: str, sep: str, lang: str) -> str: |
| 149 | + """Join a list into a phrase using language-specific connector and euphony rules. |
| 150 | +
|
| 151 | + Supports language-specific euphony transformations via euphony.json config files. |
| 152 | +
|
| 153 | + Examples: |
| 154 | + join_word_list(["a", "b", "c"], "and", ",", "en-US") |
| 155 | + -> "a, b and c" |
| 156 | +
|
| 157 | + join_word_list(["inverno", "estate"], "and", ",", "it-IT") |
| 158 | + -> "inverno ed estate" (euphony: e + vowel e -> ed) |
| 159 | +
|
| 160 | + join_word_list(["Juan", "Irene"], "and", ",", "es-ES") |
| 161 | + -> "Juan e Irene" (euphony: y + i -> e) |
| 162 | +
|
| 163 | + Args: |
| 164 | + items: List of items to join (converted to strings) |
| 165 | + connector: Connector word key ("and" or "or") |
| 166 | + sep: Separator character between items (default ",") |
| 167 | + lang: BCP-47 language tag (default "en-US") |
| 168 | +
|
| 169 | + Returns: |
| 170 | + Joined phrase with language-appropriate formatting |
| 171 | + """ |
| 172 | + if not items: |
| 173 | + return "" |
| 174 | + if len(items) == 1: |
| 175 | + return str(items[0]) |
| 176 | + |
| 177 | + # Load connector word |
| 178 | + connector_word = get_word(lang, connector) |
| 179 | + |
| 180 | + # Load and apply euphony rules if available |
| 181 | + euphony_rules = load_euphony_rules(lang) |
| 182 | + if euphony_rules: |
| 183 | + connector_word = apply_euphony(connector_word, str(items[-1]), euphony_rules) |
| 184 | + |
| 185 | + # Format separator |
| 186 | + if not sep: |
| 187 | + sep = ", " |
| 188 | + else: |
| 189 | + sep += " " |
| 190 | + |
| 191 | + # Join: items[:-1] with sep, then connector, then final item |
| 192 | + if len(items) == 2: |
| 193 | + # Two items: no separator before connector |
| 194 | + return f"{items[0]} {connector_word} {items[1]}" |
| 195 | + else: |
| 196 | + # Three or more items: use separator |
| 197 | + return (sep.join(str(item) for item in items[:-1]) + |
| 198 | + " " + connector_word + |
| 199 | + " " + items[-1]) |
0 commit comments