|
@@ -142,62 +142,43 @@ ERROR_THRESHOLD = config.get('translation', 'error_threshold')
|
|
SUCCESS_THRESHOLD = config.get('translation', 'success_threshold')
|
|
SUCCESS_THRESHOLD = config.get('translation', 'success_threshold')
|
|
|
|
|
|
# 更新其他类的初始化参数
|
|
# 更新其他类的初始化参数
|
|
-class LineCountManager:
|
|
|
|
|
|
+class TranslationStats:
|
|
def __init__(self):
|
|
def __init__(self):
|
|
- self.current_line_count = INITIAL_LINE_COUNT
|
|
|
|
- self.consecutive_errors = 0
|
|
|
|
- self.consecutive_successes = 0
|
|
|
|
- self.last_error_time = None
|
|
|
|
- self.error_cooldown = config.get('translation', 'error_cooldown')
|
|
|
|
- self.version = f"1.0.{INITIAL_LINE_COUNT}"
|
|
|
|
- self.error_history = []
|
|
|
|
|
|
+ self.start_time = time.time()
|
|
|
|
+ self.total_chars = 0
|
|
|
|
+ self.translated_chars = 0
|
|
|
|
+ self.total_requests = 0
|
|
|
|
+ self.successful_requests = 0
|
|
|
|
+ self.failed_requests = 0
|
|
|
|
|
|
- def adjust_line_count(self, success):
|
|
|
|
- """根据翻译结果调整行数"""
|
|
|
|
- current_time = time.time()
|
|
|
|
-
|
|
|
|
- # 检查是否在冷却期内
|
|
|
|
- if self.last_error_time and (current_time - self.last_error_time) < self.error_cooldown:
|
|
|
|
- return self.current_line_count
|
|
|
|
-
|
|
|
|
|
|
+ def update_stats(self, original_text, translated_text, success=True):
|
|
|
|
+ self.total_chars += len(original_text)
|
|
|
|
+ self.translated_chars += len(translated_text)
|
|
|
|
+ self.total_requests += 1
|
|
if success:
|
|
if success:
|
|
- self.consecutive_errors = 0
|
|
|
|
- self.consecutive_successes = 0 # 重置成功计数,但不增加行数
|
|
|
|
|
|
+ self.successful_requests += 1
|
|
else:
|
|
else:
|
|
- self.consecutive_successes = 0
|
|
|
|
- self.consecutive_errors += 1
|
|
|
|
- self.last_error_time = current_time
|
|
|
|
-
|
|
|
|
- # 记录错误
|
|
|
|
- self.error_history.append({
|
|
|
|
- 'time': current_time,
|
|
|
|
- 'line_count': self.current_line_count
|
|
|
|
- })
|
|
|
|
-
|
|
|
|
- # 如果连续错误次数达到阈值,减少行数
|
|
|
|
- if self.consecutive_errors >= ERROR_THRESHOLD:
|
|
|
|
- if self.current_line_count > MIN_LINE_COUNT:
|
|
|
|
- self.current_line_count -= 1
|
|
|
|
- self.consecutive_errors = 0
|
|
|
|
- self.version = f"1.0.{self.current_line_count}"
|
|
|
|
- logging.warning(f"翻译连续失败,减少行数到 {self.current_line_count},版本更新为 {self.version}")
|
|
|
|
-
|
|
|
|
- return self.current_line_count
|
|
|
|
|
|
+ self.failed_requests += 1
|
|
|
|
|
|
- def get_error_stats(self):
|
|
|
|
- """获取错误统计信息"""
|
|
|
|
- if not self.error_history:
|
|
|
|
- return "无错误记录"
|
|
|
|
|
|
+ def get_stats(self):
|
|
|
|
+ elapsed_time = time.time() - self.start_time
|
|
|
|
+ chars_per_second = self.translated_chars / elapsed_time if elapsed_time > 0 else 0
|
|
|
|
+ success_rate = (self.successful_requests / self.total_requests * 100) if self.total_requests > 0 else 0
|
|
|
|
|
|
- recent_errors = [e for e in self.error_history if time.time() - e['time'] < 3600] # 最近一小时的错误
|
|
|
|
return {
|
|
return {
|
|
- "总错误数": len(self.error_history),
|
|
|
|
- "最近一小时错误数": len(recent_errors),
|
|
|
|
- "当前行数": self.current_line_count,
|
|
|
|
- "连续错误": self.consecutive_errors,
|
|
|
|
- "连续成功": self.consecutive_successes
|
|
|
|
|
|
+ "总字符数": self.total_chars,
|
|
|
|
+ "已翻译字符数": self.translated_chars,
|
|
|
|
+ "翻译速度": f"{chars_per_second:.2f} 字符/秒",
|
|
|
|
+ "成功率": f"{success_rate:.1f}%",
|
|
|
|
+ "总请求数": self.total_requests,
|
|
|
|
+ "成功请求": self.successful_requests,
|
|
|
|
+ "失败请求": self.failed_requests,
|
|
|
|
+ "运行时间": f"{elapsed_time:.1f} 秒"
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+# 创建全局的统计对象
|
|
|
|
+translation_stats = TranslationStats()
|
|
|
|
+
|
|
class DatabaseManager:
|
|
class DatabaseManager:
|
|
def __init__(self):
|
|
def __init__(self):
|
|
self.db_path = config.get('database', 'path')
|
|
self.db_path = config.get('database', 'path')
|
|
@@ -229,28 +210,26 @@ class DatabaseManager:
|
|
total_lines INTEGER,
|
|
total_lines INTEGER,
|
|
processed_lines INTEGER,
|
|
processed_lines INTEGER,
|
|
status TEXT,
|
|
status TEXT,
|
|
- version TEXT,
|
|
|
|
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
error_count INTEGER DEFAULT 0,
|
|
error_count INTEGER DEFAULT 0,
|
|
retry_count INTEGER DEFAULT 0
|
|
retry_count INTEGER DEFAULT 0
|
|
)
|
|
)
|
|
''')
|
|
''')
|
|
|
|
|
|
- # 创建翻译组进度表
|
|
|
|
|
|
+ # 创建行进度表
|
|
c.execute('''
|
|
c.execute('''
|
|
- CREATE TABLE IF NOT EXISTS group_progress (
|
|
|
|
|
|
+ CREATE TABLE IF NOT EXISTS line_progress (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
file_path TEXT,
|
|
file_path TEXT,
|
|
- group_index INTEGER,
|
|
|
|
|
|
+ line_index INTEGER,
|
|
original_text TEXT,
|
|
original_text TEXT,
|
|
translated_text TEXT,
|
|
translated_text TEXT,
|
|
status TEXT,
|
|
status TEXT,
|
|
- version TEXT,
|
|
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
error_count INTEGER DEFAULT 0,
|
|
error_count INTEGER DEFAULT 0,
|
|
retry_count INTEGER DEFAULT 0,
|
|
retry_count INTEGER DEFAULT 0,
|
|
- UNIQUE(file_path, group_index, version)
|
|
|
|
|
|
+ UNIQUE(file_path, line_index)
|
|
)
|
|
)
|
|
''')
|
|
''')
|
|
|
|
|
|
@@ -259,7 +238,7 @@ class DatabaseManager:
|
|
CREATE TABLE IF NOT EXISTS error_log (
|
|
CREATE TABLE IF NOT EXISTS error_log (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
file_path TEXT,
|
|
file_path TEXT,
|
|
- group_index INTEGER,
|
|
|
|
|
|
+ line_index INTEGER,
|
|
error_type TEXT,
|
|
error_type TEXT,
|
|
error_message TEXT,
|
|
error_message TEXT,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
@@ -293,38 +272,38 @@ class DatabaseManager:
|
|
c = self.get_connection().cursor()
|
|
c = self.get_connection().cursor()
|
|
c.execute('''
|
|
c.execute('''
|
|
INSERT OR REPLACE INTO file_progress
|
|
INSERT OR REPLACE INTO file_progress
|
|
- (file_path, total_lines, processed_lines, status, version, last_updated)
|
|
|
|
- VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
|
|
|
- ''', (file_path, total_lines, processed_lines, status, line_count_manager.version))
|
|
|
|
|
|
+ (file_path, total_lines, processed_lines, status, last_updated)
|
|
|
|
+ VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)
|
|
|
|
+ ''', (file_path, total_lines, processed_lines, status))
|
|
self.get_connection().commit()
|
|
self.get_connection().commit()
|
|
|
|
|
|
- def get_group_progress(self, file_path, group_index):
|
|
|
|
- """获取翻译组进度"""
|
|
|
|
|
|
+ def get_line_progress(self, file_path, line_index):
|
|
|
|
+ """获取行翻译进度"""
|
|
c = self.get_connection().cursor()
|
|
c = self.get_connection().cursor()
|
|
c.execute('''
|
|
c.execute('''
|
|
- SELECT * FROM group_progress
|
|
|
|
- WHERE file_path = ? AND group_index = ? AND version = ?
|
|
|
|
- ''', (file_path, group_index, line_count_manager.version))
|
|
|
|
|
|
+ SELECT * FROM line_progress
|
|
|
|
+ WHERE file_path = ? AND line_index = ?
|
|
|
|
+ ''', (file_path, line_index))
|
|
return c.fetchone()
|
|
return c.fetchone()
|
|
|
|
|
|
- def update_group_progress(self, file_path, group_index, original_text, translated_text, status):
|
|
|
|
- """更新翻译组进度"""
|
|
|
|
|
|
+ def update_line_progress(self, file_path, line_index, original_text, translated_text, status):
|
|
|
|
+ """更新行翻译进度"""
|
|
c = self.get_connection().cursor()
|
|
c = self.get_connection().cursor()
|
|
c.execute('''
|
|
c.execute('''
|
|
- INSERT OR REPLACE INTO group_progress
|
|
|
|
- (file_path, group_index, original_text, translated_text, status, version, updated_at)
|
|
|
|
- VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
|
|
|
- ''', (file_path, group_index, original_text, translated_text, status, line_count_manager.version))
|
|
|
|
|
|
+ INSERT OR REPLACE INTO line_progress
|
|
|
|
+ (file_path, line_index, original_text, translated_text, status, updated_at)
|
|
|
|
+ VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
|
|
|
+ ''', (file_path, line_index, original_text, translated_text, status))
|
|
self.get_connection().commit()
|
|
self.get_connection().commit()
|
|
|
|
|
|
- def log_error(self, file_path, group_index, error_type, error_message):
|
|
|
|
|
|
+ def log_error(self, file_path, line_index, error_type, error_message):
|
|
"""记录错误"""
|
|
"""记录错误"""
|
|
c = self.get_connection().cursor()
|
|
c = self.get_connection().cursor()
|
|
c.execute('''
|
|
c.execute('''
|
|
INSERT INTO error_log
|
|
INSERT INTO error_log
|
|
- (file_path, group_index, error_type, error_message)
|
|
|
|
|
|
+ (file_path, line_index, error_type, error_message)
|
|
VALUES (?, ?, ?, ?)
|
|
VALUES (?, ?, ?, ?)
|
|
- ''', (file_path, group_index, error_type, error_message))
|
|
|
|
|
|
+ ''', (file_path, line_index, error_type, error_message))
|
|
self.get_connection().commit()
|
|
self.get_connection().commit()
|
|
|
|
|
|
def get_error_stats(self):
|
|
def get_error_stats(self):
|
|
@@ -352,7 +331,7 @@ class TranslationCache:
|
|
self.misses = 0
|
|
self.misses = 0
|
|
|
|
|
|
# 创建全局实例
|
|
# 创建全局实例
|
|
-line_count_manager = LineCountManager()
|
|
|
|
|
|
+line_count_manager = TranslationStats()
|
|
db_manager = DatabaseManager()
|
|
db_manager = DatabaseManager()
|
|
async_translation_manager = AsyncTranslationManager()
|
|
async_translation_manager = AsyncTranslationManager()
|
|
translation_cache = TranslationCache()
|
|
translation_cache = TranslationCache()
|
|
@@ -361,52 +340,15 @@ translation_cache = TranslationCache()
|
|
VERSION = "1.0.1" # 版本号,用于区分不同版本的翻译
|
|
VERSION = "1.0.1" # 版本号,用于区分不同版本的翻译
|
|
line_count = 2 # 每组行数,越大越快,但越容易出错
|
|
line_count = 2 # 每组行数,越大越快,但越容易出错
|
|
|
|
|
|
-class TranslationStats:
|
|
|
|
- def __init__(self):
|
|
|
|
- self.start_time = time.time()
|
|
|
|
- self.total_chars = 0
|
|
|
|
- self.translated_chars = 0
|
|
|
|
- self.total_requests = 0
|
|
|
|
- self.successful_requests = 0
|
|
|
|
- self.failed_requests = 0
|
|
|
|
-
|
|
|
|
- def update_stats(self, original_text, translated_text, success=True):
|
|
|
|
- self.total_chars += len(original_text)
|
|
|
|
- self.translated_chars += len(translated_text)
|
|
|
|
- self.total_requests += 1
|
|
|
|
- if success:
|
|
|
|
- self.successful_requests += 1
|
|
|
|
- else:
|
|
|
|
- self.failed_requests += 1
|
|
|
|
-
|
|
|
|
- def get_stats(self):
|
|
|
|
- elapsed_time = time.time() - self.start_time
|
|
|
|
- chars_per_second = self.translated_chars / elapsed_time if elapsed_time > 0 else 0
|
|
|
|
- success_rate = (self.successful_requests / self.total_requests * 100) if self.total_requests > 0 else 0
|
|
|
|
-
|
|
|
|
- return {
|
|
|
|
- "总字符数": self.total_chars,
|
|
|
|
- "已翻译字符数": self.translated_chars,
|
|
|
|
- "翻译速度": f"{chars_per_second:.2f} 字符/秒",
|
|
|
|
- "成功率": f"{success_rate:.1f}%",
|
|
|
|
- "总请求数": self.total_requests,
|
|
|
|
- "成功请求": self.successful_requests,
|
|
|
|
- "失败请求": self.failed_requests,
|
|
|
|
- "运行时间": f"{elapsed_time:.1f} 秒"
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-# 创建全局的统计对象
|
|
|
|
-translation_stats = TranslationStats()
|
|
|
|
-
|
|
|
|
def get_completed_groups(conn, file_path):
|
|
def get_completed_groups(conn, file_path):
|
|
- """获取已完成的翻译组"""
|
|
|
|
|
|
+ """获取已完成的翻译行"""
|
|
c = conn.cursor()
|
|
c = conn.cursor()
|
|
c.execute('''
|
|
c.execute('''
|
|
- SELECT group_index, translated_text
|
|
|
|
- FROM group_progress
|
|
|
|
- WHERE file_path = ? AND status = 'completed' AND version = ?
|
|
|
|
- ORDER BY group_index
|
|
|
|
- ''', (file_path, line_count_manager.version))
|
|
|
|
|
|
+ SELECT line_index, translated_text
|
|
|
|
+ FROM line_progress
|
|
|
|
+ WHERE file_path = ? AND status = 'completed'
|
|
|
|
+ ORDER BY line_index
|
|
|
|
+ ''', (file_path,))
|
|
return c.fetchall()
|
|
return c.fetchall()
|
|
|
|
|
|
# """ - 输出内容要求用代码块包裹起来
|
|
# """ - 输出内容要求用代码块包裹起来
|
|
@@ -450,7 +392,6 @@ def translate_text(text):
|
|
print(content, end='', flush=True)
|
|
print(content, end='', flush=True)
|
|
|
|
|
|
print() # 换行
|
|
print() # 换行
|
|
- line_count_manager.adjust_line_count(True)
|
|
|
|
|
|
|
|
# 更新统计信息
|
|
# 更新统计信息
|
|
translation_stats.update_stats(text, translated_text, True)
|
|
translation_stats.update_stats(text, translated_text, True)
|
|
@@ -459,7 +400,6 @@ def translate_text(text):
|
|
|
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
logging.error(f"翻译出错: {str(e)}")
|
|
logging.error(f"翻译出错: {str(e)}")
|
|
- line_count_manager.adjust_line_count(False)
|
|
|
|
|
|
|
|
# 更新统计信息
|
|
# 更新统计信息
|
|
translation_stats.update_stats(text, "", False)
|
|
translation_stats.update_stats(text, "", False)
|
|
@@ -487,9 +427,12 @@ def process_html_file(file_path, conn):
|
|
if content is None:
|
|
if content is None:
|
|
raise Exception(f"无法使用支持的编码读取文件: {file_path}")
|
|
raise Exception(f"无法使用支持的编码读取文件: {file_path}")
|
|
|
|
|
|
- # 使用正则表达式提取body标签内的内容
|
|
|
|
|
|
+ # 使用正则表达式提取body标签内的内容和title标签
|
|
body_pattern = re.compile(r'<body[^>]*>(.*?)</body>', re.DOTALL)
|
|
body_pattern = re.compile(r'<body[^>]*>(.*?)</body>', re.DOTALL)
|
|
|
|
+ title_pattern = re.compile(r'<title>(.*?)</title>', re.DOTALL)
|
|
|
|
+
|
|
body_match = body_pattern.search(content)
|
|
body_match = body_pattern.search(content)
|
|
|
|
+ title_match = title_pattern.search(content)
|
|
|
|
|
|
if not body_match:
|
|
if not body_match:
|
|
print(f"警告: {file_path} 中没有找到body标签")
|
|
print(f"警告: {file_path} 中没有找到body标签")
|
|
@@ -497,6 +440,17 @@ def process_html_file(file_path, conn):
|
|
|
|
|
|
body_content = body_match.group(1)
|
|
body_content = body_match.group(1)
|
|
|
|
|
|
|
|
+ # 处理title标签
|
|
|
|
+ if title_match:
|
|
|
|
+ title_content = title_match.group(1).strip()
|
|
|
|
+ if title_content: # 只有当标题内容不为空时才处理
|
|
|
|
+ print(f"\n翻译标题: {title_content}")
|
|
|
|
+ translated_title = translate_text(title_content)
|
|
|
|
+ # 替换原始title内容
|
|
|
|
+ content = content.replace(f"<title>{title_content}</title>", f"<title>{translated_title}</title>")
|
|
|
|
+ else:
|
|
|
|
+ print("\n跳过空标题")
|
|
|
|
+
|
|
# 按行分割内容,保留所有HTML标签行,但只翻译包含 <p class 的行
|
|
# 按行分割内容,保留所有HTML标签行,但只翻译包含 <p class 的行
|
|
lines = []
|
|
lines = []
|
|
for line in body_content.split('\n'):
|
|
for line in body_content.split('\n'):
|
|
@@ -506,78 +460,45 @@ def process_html_file(file_path, conn):
|
|
|
|
|
|
total_lines = len(lines)
|
|
total_lines = len(lines)
|
|
|
|
|
|
- # 获取已完成的翻译组
|
|
|
|
- completed_groups = get_completed_groups(conn, file_path)
|
|
|
|
- completed_indices = {group[0] for group in completed_groups}
|
|
|
|
|
|
+ # 获取已完成的翻译
|
|
|
|
+ completed_lines = get_completed_groups(conn, file_path)
|
|
|
|
+ completed_indices = {line[0] for line in completed_lines}
|
|
|
|
|
|
# 计算已处理的进度
|
|
# 计算已处理的进度
|
|
if progress:
|
|
if progress:
|
|
- print(f"文件 {file_path} 已处理进度: {progress[2]}/{progress[1]} 行 ({round(progress[2]*100/progress[1], 2)}%)")
|
|
|
|
|
|
+ print(f"文件 {file_path} 已处理进度: {progress['processed_lines']}/{progress['total_lines']} 行 ({round(progress['processed_lines']*100/progress['total_lines'], 2)}%)")
|
|
|
|
|
|
- # 按组处理内容
|
|
|
|
|
|
+ # 逐行处理内容
|
|
translated_lines = []
|
|
translated_lines = []
|
|
try:
|
|
try:
|
|
- with tqdm(range(0, len(lines), line_count_manager.current_line_count),
|
|
|
|
- desc=f"处理文件 {os.path.basename(file_path)}",
|
|
|
|
- unit="组") as pbar:
|
|
|
|
|
|
+ with tqdm(range(len(lines)), desc=f"处理文件 {os.path.basename(file_path)}", unit="行") as pbar:
|
|
for i in pbar:
|
|
for i in pbar:
|
|
- group_index = i // line_count_manager.current_line_count
|
|
|
|
-
|
|
|
|
# 检查是否已完成
|
|
# 检查是否已完成
|
|
- if group_index in completed_indices:
|
|
|
|
|
|
+ if i in completed_indices:
|
|
# 使用已完成的翻译
|
|
# 使用已完成的翻译
|
|
- for group in completed_groups:
|
|
|
|
- if group[0] == group_index:
|
|
|
|
- translated_lines.extend(group[1].split('\n'))
|
|
|
|
|
|
+ for line in completed_lines:
|
|
|
|
+ if line[0] == i:
|
|
|
|
+ translated_lines.append(line[1])
|
|
break
|
|
break
|
|
continue
|
|
continue
|
|
|
|
|
|
- group = lines[i:i+line_count_manager.current_line_count]
|
|
|
|
- if group:
|
|
|
|
- # 保存原始文本
|
|
|
|
- original_text = "\n".join(group)
|
|
|
|
-
|
|
|
|
- # 收集需要翻译的段落
|
|
|
|
- paragraphs_to_translate = []
|
|
|
|
- paragraph_indices = []
|
|
|
|
- for idx, line in enumerate(group):
|
|
|
|
- if '<p class' in line or line.startswith('<h'):
|
|
|
|
- paragraphs_to_translate.append(line)
|
|
|
|
- paragraph_indices.append(idx)
|
|
|
|
-
|
|
|
|
- # 如果有需要翻译的段落,进行翻译
|
|
|
|
- if paragraphs_to_translate:
|
|
|
|
- translated_paragraphs = []
|
|
|
|
- for paragraph in paragraphs_to_translate:
|
|
|
|
- print(f"\n翻译段落 {len(translated_paragraphs) + 1}/{len(paragraphs_to_translate)}:")
|
|
|
|
- translated_paragraph = translate_text(paragraph)
|
|
|
|
- translated_paragraphs.append(translated_paragraph)
|
|
|
|
-
|
|
|
|
- # 将翻译后的段落放回原位置
|
|
|
|
- translated_group = group.copy()
|
|
|
|
- for idx, translated in zip(paragraph_indices, translated_paragraphs):
|
|
|
|
- translated_group[idx] = translated
|
|
|
|
- else:
|
|
|
|
- translated_group = group
|
|
|
|
-
|
|
|
|
- translated_text = "\n".join(translated_group)
|
|
|
|
-
|
|
|
|
- # 更新翻译组进度
|
|
|
|
- db_manager.update_group_progress(file_path, group_index, original_text, translated_text, 'completed')
|
|
|
|
-
|
|
|
|
- # 分割翻译后的文本
|
|
|
|
- translated_lines.extend(translated_group)
|
|
|
|
-
|
|
|
|
- # 更新文件进度
|
|
|
|
- processed_lines = min((group_index + 1) * line_count_manager.current_line_count, total_lines)
|
|
|
|
- db_manager.update_file_progress(file_path, total_lines, processed_lines, 'in_progress')
|
|
|
|
-
|
|
|
|
- # 显示当前统计信息
|
|
|
|
- stats = translation_stats.get_stats()
|
|
|
|
- pbar.set_postfix(stats)
|
|
|
|
-
|
|
|
|
- # 添加较小的延迟以避免API限制
|
|
|
|
- time.sleep(0.1) # 减少延迟时间
|
|
|
|
|
|
+ line = lines[i]
|
|
|
|
+ if '<p class' in line or line.startswith('<h'):
|
|
|
|
+ print(f"\n翻译第 {i+1}/{len(lines)} 行:")
|
|
|
|
+ translated_line = translate_text(line)
|
|
|
|
+ translated_lines.append(translated_line)
|
|
|
|
+ else:
|
|
|
|
+ translated_lines.append(line)
|
|
|
|
+
|
|
|
|
+ # 更新文件进度
|
|
|
|
+ db_manager.update_file_progress(file_path, total_lines, i + 1, 'in_progress')
|
|
|
|
+
|
|
|
|
+ # 显示当前统计信息
|
|
|
|
+ stats = translation_stats.get_stats()
|
|
|
|
+ pbar.set_postfix(stats)
|
|
|
|
+
|
|
|
|
+ # 添加较小的延迟以避免API限制
|
|
|
|
+ time.sleep(0.1)
|
|
|
|
|
|
# 替换原始内容
|
|
# 替换原始内容
|
|
if translated_lines:
|
|
if translated_lines:
|
|
@@ -647,19 +568,57 @@ def process_html_file(file_path, conn):
|
|
return
|
|
return
|
|
|
|
|
|
def main():
|
|
def main():
|
|
- ops_dir = "002/Ops"
|
|
|
|
|
|
+ ops_dir = config.get('paths', 'input_dir')
|
|
html_files = [f for f in os.listdir(ops_dir) if f.endswith('.html')]
|
|
html_files = [f for f in os.listdir(ops_dir) if f.endswith('.html')]
|
|
|
|
|
|
- print(f"找到 {len(html_files)} 个HTML文件需要处理")
|
|
|
|
|
|
+ # 按文件名排序
|
|
|
|
+ html_files.sort()
|
|
|
|
+
|
|
|
|
+ total_files = len(html_files)
|
|
|
|
+ print(f"找到 {total_files} 个HTML文件需要处理")
|
|
print(f"开始时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
|
print(f"开始时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
|
|
|
|
|
# 初始化数据库连接
|
|
# 初始化数据库连接
|
|
conn = db_manager.get_connection()
|
|
conn = db_manager.get_connection()
|
|
|
|
|
|
try:
|
|
try:
|
|
- for filename in tqdm(html_files, desc="处理文件", unit="文件"):
|
|
|
|
|
|
+ for file_index, filename in enumerate(html_files, 1):
|
|
file_path = os.path.join(ops_dir, filename)
|
|
file_path = os.path.join(ops_dir, filename)
|
|
- process_html_file(file_path, conn)
|
|
|
|
|
|
+ print(f"\n开始处理第 {file_index}/{total_files} 个文件: {filename}")
|
|
|
|
+ print("-" * 50)
|
|
|
|
+
|
|
|
|
+ # 检查文件是否已完成
|
|
|
|
+ progress = db_manager.get_file_progress(file_path)
|
|
|
|
+ if progress and progress['status'] == 'completed':
|
|
|
|
+ print(f"文件 {filename} 已经完成翻译,跳过")
|
|
|
|
+ continue
|
|
|
|
+
|
|
|
|
+ try:
|
|
|
|
+ process_html_file(file_path, conn)
|
|
|
|
+ print(f"\n完成第 {file_index}/{total_files} 个文件: {filename}")
|
|
|
|
+ print("-" * 50)
|
|
|
|
+ except Exception as e:
|
|
|
|
+ print(f"\n处理文件 {filename} 时出错: {str(e)}")
|
|
|
|
+ print("继续处理下一个文件...")
|
|
|
|
+ continue
|
|
|
|
+
|
|
|
|
+ # 显示当前总体进度
|
|
|
|
+ completed_files = sum(1 for f in html_files[:file_index]
|
|
|
|
+ if db_manager.get_file_progress(os.path.join(ops_dir, f)) and
|
|
|
|
+ db_manager.get_file_progress(os.path.join(ops_dir, f))['status'] == 'completed')
|
|
|
|
+ print(f"\n总体进度: {completed_files}/{total_files} 个文件完成 "
|
|
|
|
+ f"({round(completed_files*100/total_files, 2)}%)")
|
|
|
|
+
|
|
|
|
+ # 显示统计信息
|
|
|
|
+ print("\n当前统计信息:")
|
|
|
|
+ for key, value in translation_stats.get_stats().items():
|
|
|
|
+ print(f"{key}: {value}")
|
|
|
|
+
|
|
|
|
+ # 在文件之间添加短暂延迟
|
|
|
|
+ if file_index < total_files:
|
|
|
|
+ print("\n等待 5 秒后处理下一个文件...")
|
|
|
|
+ time.sleep(5)
|
|
|
|
+
|
|
except KeyboardInterrupt:
|
|
except KeyboardInterrupt:
|
|
print("\n程序被用户中断")
|
|
print("\n程序被用户中断")
|
|
finally:
|
|
finally:
|