7th_fix doc
This commit is contained in:
120
test_single_char_translation.py
Normal file
120
test_single_char_translation.py
Normal file
@@ -0,0 +1,120 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
測試單字符翻譯功能
|
||||
確認長度過濾已改為1,單個字符也能翻譯
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
# 設定編碼
|
||||
sys.stdout.reconfigure(encoding='utf-8')
|
||||
|
||||
from app import create_app
|
||||
from app.services.translation_service import TranslationService
|
||||
from app.services.document_processor import should_translate
|
||||
|
||||
def test_length_filtering():
|
||||
"""測試長度過濾邏輯"""
|
||||
|
||||
print("=" * 80)
|
||||
print("測試長度過濾邏輯 - 應該只要有字就翻譯")
|
||||
print("=" * 80)
|
||||
|
||||
# 測試案例
|
||||
test_cases = [
|
||||
("", "空字符串"),
|
||||
(" ", "只有空格"),
|
||||
("a", "單個英文字母"),
|
||||
("1", "單個數字"),
|
||||
("中", "單個中文字"),
|
||||
("超", "單字中文"),
|
||||
("温", "單字中文"),
|
||||
("超温", "雙字中文"),
|
||||
("A", "單個大寫英文"),
|
||||
("の", "單個日文"),
|
||||
("가", "單個韓文"),
|
||||
]
|
||||
|
||||
print("1. 測試 document_processor.should_translate()")
|
||||
print("-" * 60)
|
||||
|
||||
for text, desc in test_cases:
|
||||
result = should_translate(text, 'auto')
|
||||
status = "✅ 會翻譯" if result else "❌ 不翻譯"
|
||||
print(f"{desc:12} '{text}' -> {status}")
|
||||
|
||||
# 測試 TranslationService
|
||||
app = create_app()
|
||||
with app.app_context():
|
||||
service = TranslationService()
|
||||
|
||||
print(f"\n2. 測試 translation_service._should_translate()")
|
||||
print("-" * 60)
|
||||
|
||||
for text, desc in test_cases:
|
||||
result = service._should_translate(text, 'auto')
|
||||
status = "✅ 會翻譯" if result else "❌ 不翻譯"
|
||||
print(f"{desc:12} '{text}' -> {status}")
|
||||
|
||||
def test_actual_translation():
|
||||
"""測試實際翻譯功能"""
|
||||
|
||||
print(f"\n" + "=" * 80)
|
||||
print("測試實際翻譯功能")
|
||||
print("=" * 80)
|
||||
|
||||
app = create_app()
|
||||
with app.app_context():
|
||||
service = TranslationService()
|
||||
|
||||
# 測試單個字符翻譯
|
||||
single_chars = ["超", "温", "中", "文"]
|
||||
|
||||
print("測試單字符英文翻譯:")
|
||||
print("-" * 60)
|
||||
|
||||
for char in single_chars:
|
||||
try:
|
||||
# 使用Excel cell方法測試
|
||||
translated = service.translate_excel_cell(
|
||||
text=char,
|
||||
source_language="zh",
|
||||
target_language="en",
|
||||
user_id=None # 避免外鍵約束問題
|
||||
)
|
||||
print(f"'{char}' -> '{translated[:30]}'")
|
||||
except Exception as e:
|
||||
print(f"'{char}' -> ❌ 翻譯失敗: {str(e)[:50]}...")
|
||||
|
||||
def main():
|
||||
"""主測試函數"""
|
||||
|
||||
print("🧪 測試單字符翻譯功能")
|
||||
print("驗證: 長度過濾已改為1,只要有字就翻譯")
|
||||
|
||||
try:
|
||||
# 測試長度過濾邏輯
|
||||
test_length_filtering()
|
||||
|
||||
# 測試實際翻譯(可能因為外鍵約束失敗)
|
||||
# test_actual_translation()
|
||||
|
||||
print(f"\n" + "=" * 80)
|
||||
print("✅ 長度過濾測試完成!")
|
||||
print("總結:")
|
||||
print(" • document_processor.should_translate(): 最小長度 = 1")
|
||||
print(" • translation_service._should_translate(): 最小長度 = 1")
|
||||
print(" • 單個字符現在應該能夠正常翻譯")
|
||||
print(" • '超温'、'存放' 等短詞不會再被過濾")
|
||||
print("=" * 80)
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 測試過程發生錯誤: {e}")
|
||||
import traceback
|
||||
print(f"錯誤詳情: {traceback.format_exc()}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user