import os import threading from . import to_translate from . import common import datetime import time import re def start(trans): # 允许的最大线程 threads=trans['threads'] if threads is None or int(threads)<0: max_threads=10 else: max_threads=int(threads) # 当前执行的索引位置 run_index=0 start_time = datetime.datetime.now() try: with open(trans['file_path'], 'r', encoding='utf-8') as file: content = file.read() except Exception as e: print(f"无法读取文件 {trans['file_path']}: {e}") return False texts=[] # 按段落分割内容 paragraphs = content.split('\n\n') # 假设段落之间用两个换行符分隔 # 支持最多单词量 max_word=1000 # 翻译每个段落 for paragraph in paragraphs: if check_text(paragraph): # 如果段落长度超过 1000 字,进行分割 if len(paragraph) > max_word: sub_paragraphs = split_paragraph(paragraph, max_word) for sub_paragraph in sub_paragraphs: texts.append({"text":sub_paragraph,"origin":sub_paragraph, "complete":False, "sub":True}) else: texts.append({"text":paragraph,"origin":paragraph, "complete":False, "sub":False}) # print(texts) max_run=max_threads if len(texts)>max_threads else len(texts) before_active_count=threading.activeCount() event=threading.Event() while run_index<=len(texts)-1: if threading.activeCount() max_length: # 如果当前部分长度加上句子长度超过最大长度,保存当前部分 parts.append(' '.join(current_part)) current_part = [sentence] # 开始新的部分 current_length = len(sentence) else: current_part.append(sentence) current_length += len(sentence) # 添加最后一部分 if current_part: parts.append(' '.join(current_part)) return parts def check_text(text): return text!=None and len(text)>0 and not common.is_all_punc(text)