BallonsTranslator icon indicating copy to clipboard operation
BallonsTranslator copied to clipboard

[Feature Request] 匯入多種翻譯來源,同時顯示在右側文字編輯面板

Open freelze opened this issue 1 year ago • 2 comments

讓使用者匯入多種翻譯來源, 顯示在右側文字編輯面板, 並使用 diff-match-patch highlight 文字差異,

diff-match-patch code example
import diff_match_patch

# --- Configuration Variables (Text Colors) ---
TEXT1_DELETION_COLOR = "#ff0000"  # Red
TEXT2_INSERTION_COLOR = "#008000"  # Green
# ---------------------------------------------

def highlight_differences(text1, text2):
    """
    Compares two texts and generates HTML output highlighting the differences,
    preserving line breaks, keeping text1 and text2 separate, and using
    configurable text colors.

    Args:
        text1: The first text.
        text2: The second text.

    Returns:
        A tuple containing two HTML strings:
        - The HTML for text1 with deletions highlighted.
        - The HTML for text2 with insertions highlighted.
    """

    dmp = diff_match_patch.diff_match_patch()

    # Split texts into lines to preserve line breaks
    lines1 = text1.splitlines()
    lines2 = text2.splitlines()

    html1_lines = []
    html2_lines = []

    for line1, line2 in zip(lines1, lines2):
        diffs = dmp.diff_main(line1, line2)
        dmp.diff_cleanupSemanticLossless(diffs)

        # Generate HTML for text1 (deletions highlighted, no strikethrough)
        html1 = ""
        for op, data in diffs:
            if op == diff_match_patch.diff_match_patch.DIFF_EQUAL:
                html1 += "<span>" + data + "</span>"
            elif op == diff_match_patch.diff_match_patch.DIFF_DELETE:
                html1 += "<span style=\"background-color:#ffdddd;color: " + TEXT1_DELETION_COLOR + ";\">" + data + "</span>" # Changed <del> to <span>
            elif op == diff_match_patch.diff_match_patch.DIFF_INSERT:
                html1 += ""  # Remove insertions from text1 output
        html1_lines.append(html1)

        # Generate HTML for text2 (insertions highlighted)
        html2 = ""
        for op, data in diffs:
            if op == diff_match_patch.diff_match_patch.DIFF_EQUAL:
                html2 += "<span>" + data + "</span>"
            elif op == diff_match_patch.diff_match_patch.DIFF_INSERT:
                html2 += "<ins style=\"background-color:#e6ffe6;color: " + TEXT2_INSERTION_COLOR + ";\">" + data + "</ins>" # Keep <ins> for semantic meaning
            elif op == diff_match_patch.diff_match_patch.DIFF_DELETE:
                html2 += ""  # Remove deletions from text2 output
        html2_lines.append(html2)

    html1 = "<br>".join(html1_lines)
    html2 = "<br>".join(html2_lines)

    return html1, html2

# Input texts (same as before)
text1 = """1. 2022年5月
2. 看來離開龍宮多一天,神力就會耗盡…
"""

text2 = """1. 2022 年 5 月
2. 只要離開龍宮超過一天,神通力好像就會不夠…
"""

# Generate HTML outputs for both versions
html_text1, html_text2 = highlight_differences(text1, text2)

# Write to output_match_patch_example.html
with open("output_match_patch_example.html", "w", encoding="utf-8") as f:
    f.write("<!DOCTYPE html>\n")
    f.write("<html>\n")
    f.write("<head>\n")
    f.write("<title>Diff Output (Two Versions)</title>\n")
    f.write("<meta charset=\"UTF-8\">\n")
    f.write("<style>\n")
    f.write("  .container { display: flex; }\n")
    f.write("  .column { flex: 50%; padding: 10px; }\n")
    f.write("</style>\n")
    f.write("</head>\n")
    f.write("<body>\n")
    f.write("  <div class=\"container\">\n")
    f.write("    <div class=\"column\">\n")
    f.write("      <h3>Text 1 (Deletions Highlighted)</h3>\n")
    f.write(html_text1)
    f.write("    </div>\n")
    f.write("    <div class=\"column\">\n")
    f.write("      <h3>Text 2 (Insertions Highlighted)</h3>\n")
    f.write(html_text2)
    f.write("    </div>\n")
    f.write("  </div>\n")
    f.write("</body>\n")
    f.write("</html>\n")

print("Diff output saved to output.html")

當然如果能在執行時就讓使用者選擇多種翻譯器的話更好。

自己有稍微改 code 讓它能 import 多種來源,但是有一點 bug。

multi-translation

我希望是能在每一個翻譯前面放一個 checkbox 讓使用者勾選最好的翻譯, 將該翻譯作為 render 的文字, 選到的翻譯可能還需要做個標記讓使用者知道做了選擇。

multi-translation-2

使用者有了大量比較之後, 除了參考AI汉化组机翻竞技场 也能有自己的數據可以統計哪一個模型比較好。

freelze avatar Dec 10 '24 13:12 freelze

想要這個功能

Raiter123 avatar Dec 13 '24 14:12 Raiter123

没有当前选中的翻译结果应该不需要编辑吧,感觉可以设计一个更紧凑的视图方便比对。有时间弄一下,不过我也说不准什么时候

dmMaze avatar Dec 13 '24 16:12 dmMaze