codeant-ai-for-open-source[bot] commented on code in PR #41137:
URL: https://github.com/apache/superset/pull/41137#discussion_r3426249997


##########
scripts/translations/latin_to_cyrillic_sr.py:
##########
@@ -0,0 +1,214 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Normalize Latin-script entries in the Serbian (sr) catalog to Cyrillic.
+
+The AI backfill produced a mix of scripts: most entries are Serbian Cyrillic,
+but a sizeable fraction came back in Serbian Latin (Gajica). The ``sr`` catalog
+is meant to be Cyrillic (``sr_Latn`` is generated separately by transliterating
+it), so this script rewrites any Latin-script translation into Cyrillic.
+
+Serbian Latin -> Cyrillic is a deterministic 1:1 mapping *except* that Latin
+tokens which are not Serbian words — format placeholders (%(name)s, {x}, %s),
+HTML tags, URLs, and brand/technical terms borrowed verbatim from the English
+source (SQL, Superset, CSV, GeoJSON, epoch …) — must stay in Latin. The 
reliable
+signal for "leave this alone" is: the token appears verbatim in the English
+msgid. That mirrors the convention the AI already followed in the entries it
+translated directly into Cyrillic (e.g. "да отворите SQL Lab", 
"Superset-овог").
+
+Only entries whose translation contains no Cyrillic at all are converted, so 
the
+already-correct Cyrillic majority is left untouched.
+
+Usage:
+  python scripts/translations/latin_to_cyrillic_sr.py            # in place
+  python scripts/translations/latin_to_cyrillic_sr.py --dry-run  # report only
+  python scripts/translations/latin_to_cyrillic_sr.py \
+    --src superset/translations/sr/LC_MESSAGES/messages.po --dest /tmp/out.po
+"""
+
+from __future__ import annotations
+
+import argparse
+import re
+import sys
+from pathlib import Path
+
+try:
+    import polib  # type: ignore[import-untyped]
+except ImportError:
+    print("polib is required. Run: pip install polib", file=sys.stderr)
+    sys.exit(1)
+
+TRANSLATIONS_DIR = Path(__file__).parent.parent.parent / "superset" / 
"translations"
+DEFAULT_PO = TRANSLATIONS_DIR / "sr" / "LC_MESSAGES" / "messages.po"
+
+_CYRILLIC = re.compile(r"[Ѐ-ӿ]")
+_LATIN = re.compile(r"[A-Za-zČĆŽŠĐčćžšđ]")
+
+# Spans that must never be transliterated: %-placeholders, brace placeholders,
+# HTML tags, HTML entities, and URLs. Matched in priority order.
+_PROTECT = re.compile(
+    r"""
+    %\([^)]*\)[#0\- +]?\d*(?:\.\d+)?[a-zA-Z]   # %(name)s, %(line)d, %(v).2f
+    | %[#0\- +]?\d*(?:\.\d+)?[a-zA-Z%]          # %s, %d, %.2f, %%
+    | \{[^{}]*\}                                # {name}, {0}
+    | </?[A-Za-z][^>]*>                         # HTML tags
+    | &[A-Za-z#0-9]+;                           # HTML entities
+    | https?://\S+ | www\.\S+                   # URLs
+    """,
+    re.VERBOSE,
+)
+
+# Serbian Latin -> Cyrillic. Digraphs first so dž/lj/nj are not split.
+_DIGRAPHS: list[tuple[str, str]] = [
+    ("dž", "џ"), ("Dž", "Џ"), ("DŽ", "Џ"),
+    ("lj", "љ"), ("Lj", "Љ"), ("LJ", "Љ"),
+    ("nj", "њ"), ("Nj", "Њ"), ("NJ", "Њ"),
+]
+_SINGLE: dict[str, str] = {
+    "a": "а", "b": "б", "c": "ц", "č": "ч", "ć": "ћ", "d": "д", "đ": "ђ",
+    "e": "е", "f": "ф", "g": "г", "h": "х", "i": "и", "j": "ј", "k": "к",
+    "l": "л", "m": "м", "n": "н", "o": "о", "p": "п", "r": "р", "s": "с",
+    "š": "ш", "t": "т", "u": "у", "v": "в", "z": "з", "ž": "ж",
+    "A": "А", "B": "Б", "C": "Ц", "Č": "Ч", "Ć": "Ћ", "D": "Д", "Đ": "Ђ",
+    "E": "Е", "F": "Ф", "G": "Г", "H": "Х", "I": "И", "J": "Ј", "K": "К",
+    "L": "Л", "M": "М", "N": "Н", "O": "О", "P": "П", "R": "Р", "S": "С",
+    "Š": "Ш", "T": "Т", "U": "У", "V": "В", "Z": "З", "Ž": "Ж",
+}
+
+
+def _transliterate_word(word: str) -> str:
+    """Transliterate a run of Latin letters into Cyrillic, honoring 
digraphs."""
+    out: list[str] = []
+    i = 0
+    n = len(word)
+    while i < n:
+        two = word[i : i + 2]
+        # Match a digraph only when the second letter's case is consistent with
+        # a single letter (so "Ljiljana" -> "Љиљана", "LJUBA" -> "ЉУБА").
+        matched = False
+        for src, dst in _DIGRAPHS:
+            if two == src:
+                out.append(dst)
+                i += 2
+                matched = True
+                break
+        if matched:
+            continue
+        out.append(_SINGLE.get(word[i], word[i]))

Review Comment:
   ✅ **Customized review instruction saved!**
   
   **Instruction:**
   > In Serbian Latin-to-Cyrillic transliteration helpers, keep any token with 
non-Serbian Latin letters (for example q, w, x, y) or identifier-style tokens 
such as snake_case verbatim instead of transliterating them 
character-by-character.
   
   **Applied to:**
     - `scripts/translations/**`
   
   ---
   💡 *To manage or update this instruction, visit: [CodeAnt AI 
Settings](https://app.codeant.ai/org/settings/learnings)*



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to