-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
104 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
# Yatranslator簡繁中文本轉換工具 | ||
# Yatranslator繁簡中文本轉換工具 | ||
|
||
## 簡介 | ||
本項目基於OpenCC開放中文轉換<br> | ||
此程式可以把YAT翻譯文本每行的後段(即譯文),進行繁簡互轉,且OpenCC支援大陸台灣慣用語轉換,所以也一並做了進去。<br> | ||
YAT翻譯文本的格式:TAB分隔日中文本、換行區隔翻譯項目。<br> | ||
此程式可以把Yatranslator翻譯文本每行的後段(即譯文),進行繁簡互轉,且OpenCC支援大陸台灣慣用語轉換,所以也一並做了進去。<br> | ||
Yatranslator翻譯文本的格式:TAB分隔日中文本、換行區隔翻譯項目。<br> | ||
<br> | ||
## 使用說明 | ||
使用時將要轉換的文本置於與「converter.exe」同一目錄的「before」資料夾下(沒有就創建一個),可包含資料夾,但不支援zip,請先解壓縮。<br> | ||
轉換完的文本會自動置於「after」資料夾下,若該資料夾已存在,則會存於往後編號的的資料夾。<br> | ||
轉換錯誤的那行文本會直接照抄並提示,不進行轉換;不是txt檔的話會跳過並提示。<br> | ||
<br> | ||
這程式是用python寫、PyInstaller打包的,執行效率比較差,尤其是面對COM海量的文本顯得非常吃力,使用時建議把文本拆成幾份,多開幾個程式一起轉換。<br> | ||
有試過用Nuitka打包提升執行效率,但搞了一整天,打包出來的東西都無法正確執行,也就放棄了。<br> | ||
轉換錯誤的那行文本會直接照抄並提示,不進行轉換;若檔案不是txt檔的話會跳過並提示。<br> | ||
這程式面對COM3D2海量的文本,執行效率顯得十分低落,實際使用時建議把文本拆成幾份,多開幾個程式一起轉換。<br> | ||
Releases那邊exe檔的編譯器:<br> | ||
- v1.0.0:PyInstaller | ||
- v1.2.0以後:Nuitka<br> | ||
<br> | ||
## 使用規範 | ||
此程式允許任何形式的使用、修改、打包、轉發,不需經本人同意,也可以加進自己的專案中。轉換它人的文本,除個人使用外之用途請經文本主人同意。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,80 @@ | ||
import os | ||
import msvcrt | ||
import json | ||
from alive_progress import alive_bar | ||
from alive_progress.animations import scrolling_spinner_factory | ||
from opencc import OpenCC | ||
print('本程式基於OpenCC開放中文轉換 v1.1.1 https://github.com/BYVoid/OpenCC\n請將要轉換的文本置於「before」資料夾下(沒有就創建一個),可包含資料夾,但不支援zip,請先解壓縮\n轉換完成的文本會存於「after」資料夾,若有同名資料夾則存於往後編號的資料夾\n') | ||
|
||
with open('strings.json' , 'r' , encoding = 'utf8') as json_file: | ||
json_data = json.load(json_file) | ||
with open('settings.json' , 'r' , encoding = 'utf8') as language: | ||
language = json.load(language) | ||
language = language['defaultLanguage'] | ||
|
||
print(json_data[language]['intro']+'\n') | ||
mode = '' | ||
while mode not in ('1','2','3','4'): | ||
mode = input('請輸入轉換模式\n1:簡轉繁 2:繁轉簡 3:簡轉繁(包含慣用語) 4:繁轉簡(包含慣用語)') | ||
if mode == '1': | ||
mode = 's2t' | ||
elif mode == '2': | ||
mode = 't2s' | ||
elif mode == '3': | ||
mode = 's2twp' | ||
else: | ||
mode = 'tw2sp' | ||
targetFolder = 'after' #決定after資料夾編號 | ||
if targetFolder in os.listdir(): | ||
targetFolder = 'after_2' | ||
while targetFolder in os.listdir(): | ||
targetFolder = f'after_{int((targetFolder.partition("_"))[2])+1}' | ||
print() #換行 | ||
spinner = scrolling_spinner_factory('轉換行數', length=20 , background='-', right=True, hide=True, wrap=True, overlay=False) #自訂進度條動畫 | ||
fileSum = 0 | ||
for dirs , subdirs , files in os.walk('before' , topdown=True): | ||
if files: | ||
mode = input(json_data[language]['inputMessage']+'\n') | ||
if mode == '5': | ||
language = 'CHT' | ||
with open('settings.json' , 'w' , encoding = 'utf8') as f: | ||
json.dump({'defaultLanguage': 'CHT'}, f, ensure_ascii=False) | ||
elif mode == '6': | ||
language = 'CHS' | ||
with open('settings.json' , 'w' , encoding = 'utf8') as f: | ||
json.dump({'defaultLanguage': 'CHS'}, f, ensure_ascii=False) | ||
|
||
if 'before' in os.listdir(): | ||
if mode == '1': | ||
mode = 's2t' | ||
elif mode == '2': | ||
mode = 't2s' | ||
elif mode == '3': | ||
mode = 's2twp' | ||
else: | ||
mode = 'tw2sp' | ||
targetFolder = 'after' #決定after資料夾編號 | ||
if targetFolder in os.listdir(): | ||
targetFolder = 'after_2' | ||
while targetFolder in os.listdir(): | ||
targetFolder = f'after_{int((targetFolder.partition("_"))[2])+1}' | ||
|
||
print() #換行 | ||
spinner = scrolling_spinner_factory(json_data[language]['convertedLines'], length=20 , background='-', right=True, hide=True, wrap=True, overlay=False) #自訂進度條動畫 | ||
fileSum = 0 | ||
for dirs , subdirs , files in os.walk('before' , topdown=True): | ||
if files: | ||
for file in files: | ||
if file.endswith('.txt'): | ||
fileSum += 1 #數檔案總數 | ||
|
||
fileNum = 0 | ||
for dirs , subdirs , files in os.walk('before' , topdown=True): | ||
if not os.path.isdir(targetFolder+dirs.lstrip('before')): | ||
os.makedirs(targetFolder+dirs.lstrip('before')) #產生目標資料夾 | ||
for file in files: | ||
if file.endswith('.txt'): | ||
fileSum += 1 #數檔案總數 | ||
fileNum = 0 | ||
for dirs , subdirs , files in os.walk('before' , topdown=True): | ||
if not os.path.isdir(targetFolder+dirs.lstrip('before')): | ||
os.makedirs(targetFolder+dirs.lstrip('before')) #產生目標資料夾 | ||
for file in files: | ||
if file.endswith('.txt'): | ||
fileNum += 1 | ||
print('目前檔案:'+(dirs+'\\'+file).lstrip('before\\')) | ||
with open(dirs+'\\'+file , 'r' , encoding='UTF-8') as input_text_file: | ||
lineSum = list(enumerate(open(dirs+'\\'+file , 'r' , encoding='UTF-8') , start=1))[-1][0] #數此txt檔的行數 | ||
with alive_bar(lineSum , title=f'檔案{fileNum}/{fileSum}' , spinner=spinner , bar='smooth') as bar: | ||
for line in input_text_file: | ||
with open(targetFolder+((dirs+'\\'+file).lstrip('before')) , 'a' , encoding='UTF-8') as output_text_file: | ||
try: | ||
output_text_file.write((line.partition('\t'))[0]+'\t'+OpenCC(mode).convert(line.partition('\t')[2])) | ||
except: | ||
output_text_file.write(line) | ||
print('\t轉換錯誤「'+line+'」,將直接整段複製不轉換') | ||
bar() | ||
else: | ||
print('非txt檔,跳過:'+(dirs+'\\'+file).lstrip('before\\')) | ||
print('轉換完成 請按任意鍵退出...') | ||
while True: | ||
if msvcrt.getch(): | ||
break | ||
fileNum += 1 | ||
print('目前'+json_data[language]['file']+':'+(dirs+'\\'+file).lstrip('before\\')) | ||
with open(dirs+'\\'+file , 'r' , encoding='UTF-8') as input_text_file: | ||
lineSum = list(enumerate(open(dirs+'\\'+file , 'r' , encoding='UTF-8') , start=1))[-1][0] #數此txt檔的行數 | ||
with alive_bar(lineSum , title=f'{json_data[language]["file"]}{fileNum}/{fileSum}' , spinner=spinner , bar='smooth') as bar: | ||
for line in input_text_file: | ||
with open(targetFolder+((dirs+'\\'+file).lstrip('before')) , 'a' , encoding='UTF-8') as output_text_file: | ||
try: | ||
output_text_file.write((line.split('\t'))[0]+'\t'+OpenCC(mode).convert(line.split('\t')[1])) | ||
except: | ||
output_text_file.write(line) | ||
print('\t'+json_data[language]['convertErrorFormer']+'「'+line+'」,'+json_data[language]['convertErrorLatter']) | ||
bar() | ||
else: | ||
print(json_data[language]['nonTxtError']+':'+(dirs+'\\'+file).lstrip('before\\')) | ||
print(json_data[language]['completed']) | ||
while True: | ||
if msvcrt.getch(): | ||
break | ||
else: | ||
print(json_data[language]['nonBeforeFolderError']) | ||
while True: | ||
if msvcrt.getch(): | ||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"defaultLanguage": "CHT"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"CHT": { | ||
"intro": "歡迎使用Yatranslator繁簡中文本轉換工具 v1.2.0\nGitHub頁面:https://github.com/DeePingXian/Yatranslator_CHS_CHT_converter\n\n本程式基於OpenCC開放中文轉換 v1.1.1 https://github.com/BYVoid/OpenCC\n請將要轉換的文本置於「before」資料夾下(沒有就創建一個),可包含資料夾,但不支援zip,請先解壓縮\n轉換完成的文本會存於「after」資料夾,若有同名資料夾則存於往後編號的資料夾", | ||
"inputMessage": "請輸入轉換模式,或切換介面語言\n1:簡轉繁 2:繁轉簡 3:簡轉繁(包含慣用語) 4:繁轉簡(包含慣用語) 5:切換介面為繁中 6:切换介面为简中", | ||
"convertedLines": "轉換行數", | ||
"file": "檔案", | ||
"convertErrorFormer": "轉換錯誤", | ||
"convertErrorLatter": "將直接整段複製不轉換", | ||
"nonTxtError": "非txt檔,跳過", | ||
"nonBeforeFolderError": "未找到「before」資料夾,請按任意鍵退出...", | ||
"completed": "轉換完成,請按任意鍵退出..." | ||
}, | ||
"CHS": { | ||
"intro": "欢迎使用Yatranslator繁简中文本转换工具 v1.2.0\nGitHub页面:https://github.com/DeePingXian/Yatranslator_CHS_CHT_converter\n\n本程式基于OpenCC开放中文转换 v1.1.1 https://github.com/BYVoid/OpenCC\n请将要转换的文本置于「before」文件夹下(没有就创建一个),可包含文件夹,但不支援zip,请先解压缩\n转换完成的文本会存于「after」文件夹,若有同名文件夹则存于往后编号的文件夹", | ||
"inputMessage": "请输入转换模式,或切换介面语言\n1:简转繁 2:繁转简 3:简转繁(包含惯用语) 4:繁转简(包含惯用语) 5:切換介面為繁中 6:切换介面为简中", | ||
"convertedLines": "转换行数", | ||
"file": "档案", | ||
"convertErrorFormer": "转换错误", | ||
"convertErrorLatter": "将直接整段复制不转换", | ||
"nonTxtError": "非txt档,跳过", | ||
"nonBeforeFolderError": "未找到「before」文件夹,请按任意键退出...", | ||
"completed": "转换完成,请按任意键退出..." | ||
} | ||
} |