#!/usr/bin/env python3 """ 更新后的测试脚本 测试新的缓存和智能分块功能 """ 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_new_features(): """测试新功能""" 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) console.print("[bold blue]测试新的 EPUB 翻译功能[/bold blue]\n") # 测试1: 缓存功能 console.print("[cyan]1. 测试缓存功能...[/cyan]") # 启用缓存的翻译器 translator_with_cache = EPUBTranslator(config, use_cache=True) console.print("[yellow]首次运行(无缓存)...[/yellow]") result1 = await translator_with_cache.translate_epub(epub_file, test_mode=True) console.print("[yellow]第二次运行(应该使用缓存)...[/yellow]") result2 = await translator_with_cache.translate_epub(epub_file, test_mode=True) # 检查缓存命中 if (result1.get('preface', {}).get('from_cache') or result1.get('chapter', {}).get('from_cache')): console.print("[green]✓ 缓存功能正常工作[/green]") else: console.print("[yellow]缓存可能是首次使用[/yellow]") # 测试2: 禁用缓存 console.print("\n[cyan]2. 测试禁用缓存...[/cyan]") translator_no_cache = EPUBTranslator(config, use_cache=False) result3 = await translator_no_cache.translate_epub(epub_file, test_mode=True) console.print("[green]✓ 禁用缓存功能正常[/green]") # 测试3: 智能分块 console.print("\n[cyan]3. 测试智能分块功能...[/cyan]") from src.epub_parser import EPUBParser from src.text_processor import TextProcessor parser = EPUBParser(epub_file) processor = TextProcessor(config) # 获取第一个章节 content_items = parser.extract_translatable_content(['chapter']) if content_items: first_chapter = content_items[0] paragraphs = processor.extract_paragraphs(first_chapter['content']) console.print(f"章节段落数: {len(paragraphs)}") # 测试智能分块 chunks = processor.create_smart_chunks(paragraphs) console.print(f"生成翻译块数: {len(chunks)}") console.print(f"配置的块大小: {config['translation']['chunk_size']} 字符") # 显示块统计 for i, chunk in enumerate(chunks[:3], 1): # 只显示前3个 total_chars = sum(len(p['text']) for p in chunk) console.print(f" 块 {i}: {len(chunk)} 个段落, {total_chars} 字符") console.print("[green]✓ 智能分块功能正常[/green]") # 测试4: 翻译失败处理 console.print("\n[cyan]4. 测试翻译失败处理...[/cyan]") # 这里我们可以模拟一个翻译失败的情况 # 通过检查配置中的 never_fallback_to_original 设置 never_fallback = config['translation'].get('never_fallback_to_original', True) console.print(f"翻译失败时不回填原文: {'是' if never_fallback else '否'}") if never_fallback: console.print("[green]✓ 翻译失败处理配置正确[/green]") else: console.print("[yellow]⚠ 建议启用 never_fallback_to_original[/yellow]") # 显示缓存统计 console.print("\n[cyan]5. 缓存统计信息...[/cyan]") if translator_with_cache.cache: stats = translator_with_cache.cache.get_cache_stats() console.print(f"缓存文件数: {stats.get('total_files', 0)}") console.print(f"缓存大小: {stats.get('total_size_mb', 0)} MB") console.print(f"缓存目录: {stats.get('cache_directory', '')}") 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_with_cache.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]") except Exception as e: console.print(f"[red]测试过程出错: {e}[/red]") import traceback console.print(traceback.format_exc()) if __name__ == "__main__": asyncio.run(test_new_features())