新聞專欄
從 Python 使用 DeepL 翻譯
文字檔案的第二次翻譯

在本文中DeepL 翻譯如何使用 API第二部分:關於檔案翻譯我想介紹你認識。
(首次點擊此處)
檔案轉譯需要三種 API 呼叫的組合。
請準備一個包含適當日語的文字檔 test.txt 。
我們會思考一個機制,將它翻譯成英文(美國),並 保存為test.trans.txt 。
步驟 1:上傳原始語言檔案
當你向 DeepL 伺服器提出翻譯請求時,你必須指定原始語言檔案。
$ 捲 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()
檔案['file'] = open(fn, 'rb')
檔案['auth_key'] = (無,get_key())
檔案['target_lang'] = (無,'en-us')
res = requests.post(URL, files=files)
auth_key 要明確指出target_lang 相當 棘手。
有多種方式可以指定檔案中的項目,對於兩項元組而言,
其中一個欄位是檔案名稱,兩個欄位是物件。
如果你指定一個字串,它會寫成這樣:
以另一種方式,auth_key 與 target_lang
與第一個字串轉換相同,將其儲存在資料中(dict 格式),且
你也可以同時把資料和檔案傳給 requests.post()。
步驟 2:取得翻譯處理狀態
查詢被翻譯檔案的翻譯狀態:
$ document_id=[documentID]
$ document_key=[documentKey]
$ 捲曲 https://api.deepl.com/v2/document/${document_id} \
$ -d auth_key=${auth_key} \
$ -d document_key=${document_key}
狀態為排隊(接受),翻譯(翻譯中),
錯誤(發生翻譯錯誤),完成(翻譯結束)
換算時,系統也會回傳過程剩餘時間的估計值。
參數document_id以及document_key如果您已完成替換,
如果你用 Python 寫這個流程直到發送,看起來是這樣:
URL = f'https://api.deepl.com/v2/document/{document_id}'
data = 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 寫這個流程直到發送,看起來是這樣:
URL = f'https://api.deepl.com/v2/document/{document_id}/result'
data = dict()
data['auth_key'] = get_key()
data['document_key'] = document_key
res = requests.post(URL, data=data)
檔案翻譯處理
最後,連接上述流程。
步驟 2 只回傳翻譯進度,因此
需等待處理才能收到結果。 基本上:
·翻譯時剩餘時間為seconds_remaining
睡幾秒鐘,再檢查進度。
·如果有做 錯 或做錯,就退出。
如果你用 Python 寫上面的內容,看起來是這樣:
匯入請求
import JSON
來自 Time import sleep防守 get_key():
回傳 open('key.txt').read().rstrip()
防守upload_src(FN):
”’
檔案翻譯步驟 1:上傳原始語言檔案
”’
URL = 'https://api.deepl.com/v2/document'
檔案 = dict()
檔案['file'] = open(fn, 'rb')
檔案['auth_key'] = (無,get_key())
檔案['target_lang'] = (無,'en-us')res = requests.post(URL, files=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
防守get_trans_status(document_id, document_key):
”’
步驟2:查詢翻譯處理狀態
”’
URL = f'https://api.deepl.com/v2/document/{document_id}'
data = dict()
data['auth_key'] = get_key()
data['document_key'] = document_keyres = 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:等待翻譯處理
”’
而真:
Res = get_trans_status(document_id, document_key)
狀態 = res['status']
print(f'status: {status}', flush=True)
seconds_remaining = 0
if status == 'done' 或 status == 'error':
休息
ELIF 狀態 == 「翻譯中」:
若解析度為「seconds_remaining」:
seconds_remaining = int(res['seconds_remaining'])
# 錯誤避免
若 seconds_remaining <= 0:
seconds_remaining = 10
其他:#queued,等等。
通行
印刷(f'... 等待(另一個) {seconds_remaining}s',flush=True)
睡眠(seconds_remaining)
回歸狀態
def download_tgt(fn, document_id, document_key):
”’
檔案翻譯步驟 3:下載目標語言檔案
”’
URL = f'https://api.deepl.com/v2/document/{document_id}/result'
data = dict()
data['auth_key'] = get_key()
data['document_key'] = document_keyres = 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)
防禦主戰():
fn_src = 「test.txt」
fn_tgt = fn_src.replace('.txt', '.trans.txt')Print(f'fn_src: {fn_src}')
Print(f'fn_tgt: {fn_tgt}')列印(f'上傳中:{fn_src}')
document_id,document_key = upload_src(fn_src)
狀態 = check_proceeding(document_id, document_key)
if status == 'done':
print(f'downloading: {fn_tgt}')
download_tgt(fn_tgt、document_id、document_key)如果__name__ == '__main__':
main()
這三種 API 呼叫的處理方式相似,但
關於「在 Python 中撰寫 curl 指令」的主題
我不敢簡述,但我寫得很冗長。
以上就是本文內容:
- 我用 DeepL 的 API 來翻譯檔案。
·我用 Python 表達 curl 指令的內容,創建了一個程式。
謝謝你閱讀。