#!/usr/bin/env python3 """ 简单翻译测试 专门测试你的 EPUB 文件 """ import asyncio import sys from pathlib import Path # 添加 src 目录到路径 sys.path.insert(0, str(Path(__file__).parent / "src")) from src.translator import EPUBTranslator from src.utils import load_config, setup_logging from rich.console import Console async def test_specific_epub(): """测试特定的 EPUB 文件""" console = Console() epub_file = "The ingenuity gap - Facing the economic, environmental, and other challenges of an increasingly complex and unpredictable future_副本.epub" if not Path(epub_file).exists(): console.print(f"[red]文件不存在: {epub_file}[/red]") return try: # 加载配置 config = load_config('config/config.json') setup_logging(config) # 初始化翻译器 translator = EPUBTranslator(config) console.print("[bold blue]测试 EPUB 翻译功能[/bold blue]\n") # 显示书籍信息 console.print("[cyan]正在分析书籍结构...[/cyan]") # 运行测试翻译 console.print("[yellow]开始测试翻译...[/yellow]") result = await translator.translate_epub(epub_file, test_mode=True) if isinstance(result, dict): console.print("\n[bold green]测试结果:[/bold green]") # 序言测试结果 preface_result = result.get('preface', {}) if preface_result.get('status') == 'success': console.print("\n[green]✓ 序言翻译测试成功[/green]") console.print(f"原文长度: {preface_result.get('original_length', 0)} 字符") console.print(f"译文长度: {preface_result.get('translation_length', 0)} 字符") console.print(f"原文预览: {preface_result.get('original', '')[:100]}...") console.print(f"译文预览: {preface_result.get('translation', '')[:100]}...") else: console.print(f"[yellow]序言测试: {preface_result.get('reason', '失败')}[/yellow]") # 章节测试结果 chapter_result = result.get('chapter', {}) if chapter_result.get('status') == 'success': console.print("\n[green]✓ 章节翻译测试成功[/green]") console.print(f"章节: {chapter_result.get('chapter_title', '')}") console.print(f"原文长度: {chapter_result.get('original_length', 0)} 字符") console.print(f"译文长度: {chapter_result.get('translation_length', 0)} 字符") console.print(f"原文预览: {chapter_result.get('original', '')[:100]}...") console.print(f"译文预览: {chapter_result.get('translation', '')[:100]}...") else: console.print(f"[yellow]章节测试: {chapter_result.get('reason', '失败')}[/yellow]") if (preface_result.get('status') == 'success' or chapter_result.get('status') == 'success'): console.print("\n[bold green]🎉 翻译测试成功!可以进行完整翻译。[/bold green]") # 询问是否进行完整翻译 console.print("\n[yellow]是否进行完整翻译?这可能需要较长时间和一定费用。[/yellow]") response = input("输入 'yes' 继续完整翻译,其他任意键退出: ") if response.lower() == 'yes': console.print("\n[cyan]开始完整翻译...[/cyan]") output_file = await translator.translate_epub(epub_file, test_mode=False) console.print(f"\n[bold green]🎉 翻译完成![/bold green]") console.print(f"输出文件: {output_file}") else: console.print("[yellow]已取消完整翻译[/yellow]") else: console.print("\n[red]翻译测试失败,请检查 API Key 和网络连接[/red]") else: console.print(f"[red]测试失败: {result}[/red]") except Exception as e: console.print(f"[red]测试过程出错: {e}[/red]") import traceback console.print(traceback.format_exc()) if __name__ == "__main__": asyncio.run(test_specific_epub())