Skip to content

Commit

Permalink
dump-name
Browse files Browse the repository at this point in the history
  • Loading branch information
xd2333 committed Jun 9, 2024
1 parent d490e73 commit f401f39
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
28 changes: 27 additions & 1 deletion GalTransl/Frontend/GPT.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async def doLLMTranslateSingleFile(
gptapi = CSakuraTranslate(
projectConfig, eng_type, endpoint, proxyPool
)
case "rebuildr" | "rebuilda":
case "rebuildr" | "rebuilda" | "dump-name":
gptapi = CRebuildTranslate(projectConfig, eng_type)
case _:
raise ValueError(f"不支持的翻译引擎类型 {eng_type}")
Expand Down Expand Up @@ -114,6 +114,16 @@ async def doLLMTranslateSingleFile(
except Exception as e:
LOGGER.error(f"文件 {file_name} 加载翻译列表失败: {e}")
return False

# 导出人名表功能
if "dump-name" in eng_type:
global name_dict
for tran in trans_list:
if tran.speaker and type(tran.speaker) == str:
if tran.speaker not in name_dict:
name_dict[tran.speaker] = 0
name_dict[tran.speaker] += 1
return True

# 2、翻译前处理
for i, tran in enumerate(trans_list):
Expand Down Expand Up @@ -250,6 +260,11 @@ async def doLLMTranslate(
LOGGER.info(f"当前使用 {workersPerProject} 个Sakura worker引擎")
else:
endpoint_queue = None

# 人名表初始化
if "dump-name" in eng_type:
global name_dict
name_dict = {}

file_list = get_file_list(projectConfig.getInputPath())
if not file_list:
Expand Down Expand Up @@ -279,3 +294,14 @@ async def doLLMTranslate(
# await atqdm.gather(*tasks)
await gather(*tasks) # run
progress_bar.close()

if "dump-name" in eng_type:
import csv
proj_dir = projectConfig.getProjectDir()
name_dict = dict(sorted(name_dict.items(), key=lambda item: item[1], reverse=True))
with open(joinpath(proj_dir, "人名替换表.csv"), "w", encoding="utf-8", newline="") as f:
writer = csv.writer(f)
writer.writerow(["JP_Name", "CN_Name", "Count"]) # 写入表头
for name, count in name_dict.items():
writer.writerow([name, "", count])
LOGGER.info(f"name已保存到'人名替换表.csv'(UTF-8编码,用Emeditor编辑),填入CN_Name后可用于后续翻译name字段。")
3 changes: 2 additions & 1 deletion GalTransl/Name.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ def load_name_table(name_table_path: str) -> Dict[str, str]:
# Skip the header
next(reader)
for row in reader:
name_table[row[0]] = row[1]
if row[1] != "":
name_table[row[0]] = row[1]
return name_table
2 changes: 1 addition & 1 deletion GalTransl/Runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def print_plugin_list(plugin_manager: PluginManager):

# proxyPool初始化
proxyPool = CProxyPool(cfg) if cfg.getKey("internals.enableProxy") else None
if proxyPool and translator != "Rebuild":
if proxyPool and translator not in ["rebuildr","rebuilda","dump-name","showplugs"]:
await proxyPool.checkAvailablity()
if not proxyPool.proxies:
raise Exception("没有可用的代理,请检查代理设置")
Expand Down
1 change: 1 addition & 0 deletions GalTransl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"sakura-009": "为翻译轻小说/视觉小说开展大规模训练的本地模型。适用v0.9版prompt,不支持GPT字典",
"rebuildr": "重建结果 用译前译后字典通过缓存刷写结果json -- 跳过翻译和写缓存",
"rebuilda": "重建缓存和结果 用译前译后字典刷写缓存+结果json -- 跳过翻译",
"dump-name": "导出name字段,生成人名替换表,用于翻译name字段",
"showplugs": "显示全部插件列表",
}
LANG_SUPPORTED = {
Expand Down

0 comments on commit f401f39

Please sign in to comment.