從 Python 使用 DeepL 翻譯器(第 2 部分)

在本文中,我們將介紹如何使用 DeepL Translation API 的第二部分,即文件翻譯。 (點擊這裡查看第一期)
* 假定您具備 Python 和 Linux 的基本知識。
翻譯文字檔
現在我們已經預熱了,讓我們做一些文件轉換。
這需要三個 API 調用的組合。
準備一個包含相應日語的文本檔 test.txt 。
我們將考慮一種將其翻譯成英語 (US) 並將其保存為 test.trans.txt 的機制。
步驟 1:上傳源語言檔
當你向 DeepL 的伺服器發出翻譯請求時,你需要指定源語言檔。
$ curl https://api.deepl.com/v2/document \
$ -F file=@test.txt \
$ -F auth_key=${auth_key} \
$ -F target_lang=en-us
接受翻譯請求的檔的 document_id 和 document_key 存儲在
畢竟,它將以 JSON 格式從伺服器返回。
如果你用 Python 編寫發送到發送的過程,它將看起來像這樣:
url = 'https://api.deepl.com/v2/document'
檔案 = dict()
檔['文件'] = 打開(fn, 'rb')
files['auth_key'] = (無, get_key())
files['target_lang'] = (無, 'en-us')
res = requests.post(url, files=檔)
指定 auth_key 和 target_lang 非常棘手。
有幾種方法可以指定檔項,對於二進位元組,
一項是檔名,第二項是物件。
如果您指定了一個字串,它將像這樣寫。
或者,auth_key 和 target_lang 可以使用
與第一個字串翻譯一樣,將其存儲在 data (dict 格式) 和
您還可以將數據和文件傳遞給 requests.post()。
第 2 步:獲取翻譯狀態
查詢正在翻譯的檔案的翻譯狀態:
$ document_id=[文檔 ID]
$ document_key=[documentKey]
$ curl https://api.deepl.com/v2/document/${document_id} \
$ -d auth_key=${auth_key} \
$ -d document_key=${document_key}
狀態可以是 queued、translating 或
其中有四個:error (發生翻譯錯誤) 和 done (翻譯完成)。
在翻譯的情況下,還會返回剩餘處理時間的估計值。
假設我們已經為變數分配了 document_id 和 document_key ,
如果你用 Python 編寫發送到發送的過程,它將看起來像這樣:
網址 = f'https://api.deepl.com/v2/document/{document_id}'
資料 = dict()
data['auth_key'] = get_key()
data['document_key'] = document_key
res = requests.post(url, data=data)
步驟 3:下載目標語言檔
翻譯狀態 完成後 ,下載檔:
$ curl https://api.deepl.com/v2/document/${document_id}/結果 \
$ -d auth_key=${auth_key} \
$ -d document_key=${document_key} > test.trans.txt
您只能下載一次。
翻譯結果正在重定向(保存)到 test.trans.txt 。
如果你用 Python 編寫發送到發送的過程,它將看起來像這樣:
網址 = f'https://api.deepl.com/v2/document/{document_id}/result'
資料 = dict()
data['auth_key'] = get_key()
data['document_key'] = document_key
res = requests.post(url, data=data)
檔翻譯過程
最後,我們將連接上述流程。
步驟 2 僅返回翻譯的進度,因此
您需要等待結果。 基本上:
在翻譯的情況下,剩餘時間為 seconds_remaining
休眠幾秒鐘,然後再次檢查進度。
如果完成或發出錯誤,則退出迴圈。
如果你用 Python 編寫上述過程,它將看起來像這樣:
import requests
import json
from time import sleep
def get_key() 的:
返回 open('key.txt').read().rstrip()
def upload_src(fn):
”’
檔翻譯步驟 1:上傳源語言檔
”’
url = 'https://api.deepl.com/v2/document'
檔案 = dict()
檔['文件'] = 打開(fn, 'rb')
files['auth_key'] = (無, get_key())
files['target_lang'] = (無, 'en-us')
res = requests.post(url, files=檔)
res_status = res.status_code # 200 如果成功(現在未使用)
res_text = res.text
res_data = json.loads(res_text)
document_id = res_data['document_id']
document_key = res_data['document_key']
返回 document_id、document_key
def get_trans_status(document_id, document_key):
”’
第 2 步:獲取翻譯處理狀態
”’
網址 = f'https://api.deepl.com/v2/document/{document_id}'
資料 = dict()
data['auth_key'] = get_key()
data['document_key'] = document_key
res = requests.post(url, data=data)
res_status = res.status_code # 200 如果成功(現在未使用)
res_text = res.text
res_data = json.loads(res_text)
返回 res_data
def check_proceeding(document_id, document_key):
”’
檔翻譯步驟 2:等待翻譯
”’
while True:
回復 = get_trans_status(document_id, document_key)
狀態 = res['狀態']
print(f'status: {status}', flush=True)
seconds_remaining = 0
如果 status == 'done' 或 status == 'error':
破
elif status == '正在翻譯':
如果 Res 中的 'seconds_remaining':
seconds_remaining = int(res['seconds_remaining'])
# 避免錯誤
如果 seconds_remaining <= 0:
seconds_remaining = 10
else: # 排隊,等等。
通過
print(f'...waiting for (another) {seconds_remaining}s', flush=True)
休眠(seconds_remaining)
退貨狀態
def download_tgt(fn, document_id, document_key):
”’
檔翻譯步驟 3:下載目標語言檔
”’
網址 = f'https://api.deepl.com/v2/document/{document_id}/result'
資料 = dict()
data['auth_key'] = get_key()
data['document_key'] = document_key
res = requests.post(url, data=data)
res_status = res.status_code # 200 如果成功(現在未使用)
tgt_bin = res._content
將 open(fn, 'w', encoding='utf-8′) 作為 f:
print(tgt_bin.decode('utf-8'), end=“, file=f)
def main() 的
fn_src = 'test.txt'
fn_tgt = fn_src.replace('.txt', '.trans.txt')
列印(f'fn_src: {fn_src}')
print(f'fn_tgt: {fn_tgt}')
print(f'上傳: {fn_src}')
document_id,document_key = upload_src(fn_src)
狀態 = check_proceeding(document_id, document_key)
如果 status == 'done':
print(f'下載: {fn_tgt}')
download_tgt(fn_tgt、document_id、document_key)
if __name__ == '__main__':
主()
這三種類型的 API 調用類似。
主題為 “用 Python 編寫 curl 命令”
我不敢總結它,也不敢用冗長的方式寫它。
這就是本文的全部內容:
- 我使用 DeepL 的 API 來翻譯檔。
- 我通過在 Python 中表達 curl 命令的內容來創建一個程式。
感謝您的閱讀。