# 論文翻譯-使用word斷句-中英文對照 結果比較圖 ![](https://i.imgur.com/uQu8C29.png) 在PDF格式下,翻譯PDF複製的文件會有斷句不完整的問題 <!-- <img src="img/2022-04-18-03-04-59-image.png" title="" alt="" width="460"> --> <!-- ![img](img/2022-04-18-03-04-59-image.png) --> ![](https://i.imgur.com/63UvNMt.png) 使用word開啟論文PDF檔案可以解決此斷句問題。 ![](https://i.imgur.com/iYOQjxq.png) <!-- <img src="img/2022-04-18-03-31-25-image.png" title="" alt="" width="457"> --> <!-- ![img](img/2022-04-18-03-31-25-image.png) --> 使用web 瀏覽模式,斷句自動處理,現在複製段落也有正確的斷句了 <!-- <img title="" src="img/2022-04-18-03-33-06-image.png" alt="" width="362" data-align="inline"> --> <!-- ![img](img/2022-04-18-03-33-06-image.png) --> ![](https://i.imgur.com/AIC1cba.png) 用Html格式儲存文件,開啟Html網頁檔案,使用google翻譯能夠正常的翻譯整份文件。 ![](https://i.imgur.com/ttxn1id.png) <!-- <img src="img/2022-04-18-03-37-28-image.png" title="" alt="" width="604"> --> <!-- ![img](img/2022-04-18-03-37-28-image.png) --> 但我的閱讀習慣是中文接續英文段落,因此換一個方法。 手動刪除word文件圖片,公式,避免影響翻譯後排版,再複製文字到文件檔上,命名paper.txt <!-- ![img](img/2022-04-18-03-41-09-image.png) --> ![](https://i.imgur.com/zJPqahk.png) 使用python 腳本逐行翻譯 `pip install googletrans` 安裝google 翻譯API ```python # trans.py """ 先把要翻譯的文字列表壓縮(Pack),傳給API翻譯,將翻譯回來的文字解壓縮(unpack),這個動作能加快翻譯速度。 """ ### Translate function from sys import argv from googletrans import Translator t = Translator() def translate(text): return t.translate( text, dest="zh-TW" ).text ### iterator paragraphs def para_iter( content ): for i in content: yield i def paragraphs_iter( content, text_lim = 3000 ): ps = [] # paragraphs list pl = 0 # paragraphs length for p in para_iter( content): ps.append(p) pl += len(p) if pl > text_lim: yield ps pl = 0 ps = [] if pl > 0: yield ps ### write file def writefile( pl,csl , file): pl = [ e.strip() for e in pl if e.strip()!=''] csl = [e.strip() for e in csl if e.strip() != ''] assert len(pl) == len(csl) for i,j in zip( pl, csl): line = f"{i.strip()} ({j.strip()})\n\n" file.write( line ) print( line ) ### def main(): f = open( outfile, 'w') with open( infile, 'r') as contentFile: pls = list( paragraphs_iter( contentFile.readlines() ) ) for pl in pls: csl = translate( "".join( pl ) ).split("\n") writefile(pl,csl,f) print( "translate completed" ) sleep(2) f.close() ### if __name__ == "__main__": from time import sleep from sys import argv infile = "./paper.txt" if len(argv) < 2 else argv[1] outfile = "./translated.txt" if len(argv) <3 else argv[2] main() ``` 執行腳本: `python trans.py`,程式會把段落(paragraph)打包成一個大段落,用google api來翻譯,翻譯完後的大段落解包回段落,對齊英文接續中文輸出。預設每二秒暫停一次,避免太過頻繁的請求而被Google鎖IP。打包段落翻譯較快。 最終結果: <!-- <img src="img/2022-04-18-03-45-24-image.png" title="" alt="" width="387"> --> <!-- ![img](img/2022-04-18-03-45-24-image.png) --> ![](https://i.imgur.com/HEZUvdP.png) 將文字檔案複製貼到喜歡的閱讀工具上(notion, hackmd, onenote, everynote),可以手機上面閱讀,或是列印紙本閱讀。