187 lines
6.0 KiB
Python
187 lines
6.0 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
測試修復後的儲存格為單位翻譯邏輯
|
|
驗證 Excel 和 Word 表格的翻譯是否正確對應
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
# 設定編碼
|
|
sys.stdout.reconfigure(encoding='utf-8')
|
|
|
|
from pathlib import Path
|
|
from app import create_app
|
|
from app.services.translation_service import TranslationService
|
|
|
|
def test_excel_cell_based_translation():
|
|
"""測試Excel儲存格為單位的翻譯邏輯"""
|
|
|
|
print("=" * 80)
|
|
print("測試Excel儲存格為單位翻譯邏輯")
|
|
print("=" * 80)
|
|
|
|
app = create_app()
|
|
|
|
with app.app_context():
|
|
service = TranslationService()
|
|
|
|
# 測試案例1: 泰文翻譯 (之前D4, H2缺失)
|
|
print(f"\n1. 測試泰文翻譯儲存格方法")
|
|
print("-" * 60)
|
|
|
|
# 模擬D4儲存格內容
|
|
d4_text = "WB inline"
|
|
d4_translated = service.translate_excel_cell(
|
|
text=d4_text,
|
|
source_language="zh",
|
|
target_language="th",
|
|
user_id=1
|
|
)
|
|
print(f"D4原文: {repr(d4_text)}")
|
|
print(f"D4泰文: {repr(d4_translated)}")
|
|
|
|
# 模擬H2儲存格內容
|
|
h2_text = "製程"
|
|
h2_translated = service.translate_excel_cell(
|
|
text=h2_text,
|
|
source_language="zh",
|
|
target_language="th",
|
|
user_id=1
|
|
)
|
|
print(f"H2原文: {repr(h2_text)}")
|
|
print(f"H2泰文: {repr(h2_translated)}")
|
|
|
|
# 測試案例2: 韓文翻譯 (之前D2-D8缺失)
|
|
print(f"\n2. 測試韓文翻譯儲存格方法")
|
|
print("-" * 60)
|
|
|
|
# 模擬D2儲存格內容 (多行格式)
|
|
d2_text = "WB inline\nDC: 1000V\n@25°C Tstg: -55°C to +125°C"
|
|
d2_translated = service.translate_excel_cell(
|
|
text=d2_text,
|
|
source_language="zh",
|
|
target_language="ko",
|
|
user_id=1
|
|
)
|
|
print(f"D2原文: {repr(d2_text)}")
|
|
print(f"D2韓文: {repr(d2_translated[:60])}...")
|
|
|
|
# 檢查是否使用了原始DIFY翻譯的特徵
|
|
if "와이어 본딩" in d2_translated:
|
|
print(f" 🎯 ✅ 使用了原始DIFY翻譯特徵")
|
|
elif "연결" in d2_translated:
|
|
print(f" ✋ ❌ 仍使用手動補充翻譯")
|
|
else:
|
|
print(f" ❓ 翻譯來源不明")
|
|
|
|
def test_word_table_cell_translation():
|
|
"""測試Word表格儲存格為單位的翻譯邏輯"""
|
|
|
|
print(f"\n" + "=" * 80)
|
|
print("測試Word表格儲存格為單位翻譯邏輯")
|
|
print("=" * 80)
|
|
|
|
app = create_app()
|
|
|
|
with app.app_context():
|
|
service = TranslationService()
|
|
|
|
print(f"\n1. 測試Word表格儲存格翻譯方法")
|
|
print("-" * 60)
|
|
|
|
# 測試案例: Word表格儲存格包含多段落的情況
|
|
cell_text = "超温\n存放\n工务部"
|
|
cell_translated = service.translate_word_table_cell(
|
|
text=cell_text,
|
|
source_language="zh",
|
|
target_language="th",
|
|
user_id=1
|
|
)
|
|
print(f"表格儲存格原文: {repr(cell_text)}")
|
|
print(f"表格儲存格泰文: {repr(cell_translated)}")
|
|
|
|
# 另一個案例: 單段落儲存格
|
|
single_cell = "製程控制"
|
|
single_translated = service.translate_word_table_cell(
|
|
text=single_cell,
|
|
source_language="zh",
|
|
target_language="ko",
|
|
user_id=1
|
|
)
|
|
print(f"\n單段落儲存格原文: {repr(single_cell)}")
|
|
print(f"單段落儲存格韓文: {repr(single_translated)}")
|
|
|
|
def test_translation_cache_mapping():
|
|
"""測試翻譯快取與儲存格的對應關係"""
|
|
|
|
print(f"\n" + "=" * 80)
|
|
print("測試翻譯快取與儲存格的對應關係")
|
|
print("=" * 80)
|
|
|
|
app = create_app()
|
|
|
|
with app.app_context():
|
|
from sqlalchemy import text as sql_text
|
|
from app import db
|
|
|
|
# 檢查之前提到的快取記錄是否能正確對應
|
|
print(f"\n1. 檢查泰文翻譯快取記錄")
|
|
print("-" * 60)
|
|
|
|
# D4對應的ROW 392, 393
|
|
d4_cache = db.session.execute(sql_text("""
|
|
SELECT id, source_text, translated_text, created_at
|
|
FROM dt_translation_cache
|
|
WHERE id IN (392, 393) AND target_language = 'th'
|
|
ORDER BY id
|
|
""")).fetchall()
|
|
|
|
for row in d4_cache:
|
|
print(f"ROW {row[0]}: {repr(row[1][:30])}... -> {repr(row[2][:30])}...")
|
|
|
|
# H2對應的ROW 381-385
|
|
h2_cache = db.session.execute(sql_text("""
|
|
SELECT id, source_text, translated_text, created_at
|
|
FROM dt_translation_cache
|
|
WHERE id BETWEEN 381 AND 385 AND target_language = 'th'
|
|
ORDER BY id
|
|
""")).fetchall()
|
|
|
|
print(f"\nH2相關快取記錄:")
|
|
for row in h2_cache:
|
|
print(f"ROW {row[0]}: {repr(row[1][:20])}... -> {repr(row[2][:20])}...")
|
|
|
|
def main():
|
|
"""主測試函數"""
|
|
|
|
print("🧪 開始測試儲存格為單位的翻譯邏輯")
|
|
print("預期: 翻譯不再進行切片,整個儲存格作為單位處理")
|
|
|
|
try:
|
|
# 測試Excel儲存格翻譯
|
|
test_excel_cell_based_translation()
|
|
|
|
# 測試Word表格儲存格翻譯
|
|
test_word_table_cell_translation()
|
|
|
|
# 測試快取對應關係
|
|
test_translation_cache_mapping()
|
|
|
|
print(f"\n" + "=" * 80)
|
|
print("✅ 儲存格為單位翻譯邏輯測試完成!")
|
|
print("📊 總結:")
|
|
print(" - Excel: 使用 translate_excel_cell() 方法")
|
|
print(" - Word表格: 使用 translate_word_table_cell() 方法")
|
|
print(" - 兩者都不進行內容切片,保持儲存格完整性")
|
|
print("=" * 80)
|
|
|
|
except Exception as e:
|
|
print(f"❌ 測試過程中發生錯誤: {str(e)}")
|
|
import traceback
|
|
print(f"錯誤詳情: {traceback.format_exc()}")
|
|
|
|
if __name__ == "__main__":
|
|
main() |