新聞專欄

本文內容DeepL 翻譯如何使用 API第 2 部分:關於文件翻譯我想向您介紹一下。

(首次點擊此處)

 

檔案翻譯需要三個 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=zh-tw

document_iddocument_key
它會以 JSON 格式從伺服器傳回。
如果您在 Python 中編寫直到發送的進程,它看起來像這樣:

 

網址 = 'https://api.deepl.com/v2/document'
檔案 = dict()
檔案['檔案'] = 開啟(fn, 'rb')
檔案['auth_key'] = (無,get_key())
檔案['target_lang'] = (無, 'en-us')
res = requests.post(網址,檔案=檔案)

auth_key  指定target_lang 相當棘手。
有幾種方法可以指定檔案中的項目,在兩項元組的情況下,
一個欄位是檔案名稱,兩個欄位是物件。
如果您指定一個字串,它將寫成這樣:
以另一種方式,auth_key  target_lang 
與第一個字串翻譯一樣,將其儲存在資料(字典格式)中,並
您也可以將資料和檔案傳遞給 requests.post()。

 

步驟 2:取得翻譯處理狀態

查詢正在翻譯的檔案的翻譯狀態:

 

$ document_id=[文件編號]
$ document_key=[文件索引鍵]
$ 捲曲 https://api.deepl.com/v2/document/${document_id} \
$ -d auth_key=${auth_key} \
$ -d document_key=${document_key}

 

狀態為排隊(已接受),翻譯(翻譯),
(發生翻譯錯誤),(翻譯完)
翻譯時,系統也會傳回流程剩餘時間的估計值。
不定的document_iddocument_key如果您已完成替換,
如果您在 Python 中編寫直到發送的進程,它看起來像這樣:

 

網址 = f'https://api.deepl.com/v2/document/{document_id}'
資料 = dict()
data['auth_key'] = get_key()
數據['document_key'] = document_key
res = requests.post(網址,資料=資料)

步驟 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()
數據['document_key'] = document_key
res = requests.post(網址,資料=資料)

檔案翻譯處理

最後,連接上述進程。
步驟 2 只會傳回翻譯進度,因此
需要等待處理才能收到結果。 基本上:
・翻譯 ,剩餘時間 seconds_remaining 
睡幾秒鐘,然後再次檢查你的進度。
如果您完成或  錯誤 ,請退出循環。
如果你用 Python 寫上面的內容,它看起來像這樣:

 

匯入要求
匯入JSON
從時間匯入睡眠

定義get_key():
傳回 open('key.txt').read().rstrip()

def upload_src(fn):
”’
文件翻譯步驟 1:上傳原始語言文件
”’
網址 = 'https://api.deepl.com/v2/document'
檔案 = dict()
檔案['檔案'] = 開啟(fn, 'rb')
檔案['auth_key'] = (無,get_key())
檔案['target_lang'] = (無, 'en-us')

res = requests.post(網址,檔案=檔案)
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()
數據['document_key'] = document_key

res = requests.post(網址,資料=資料)
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 步:等待翻譯處理
”’
而 True:
res = get_trans_status(document_id, document_key)
狀態 = res['狀態']
列印(f'status: {status}', flush=True)
seconds_remaining = 0
如果狀態 == '完成' 或 狀態 == '錯誤':

elif 狀態 == '翻譯':
如果 res 中的 'seconds_remaining':
seconds_remaining = int(res['seconds_remaining'])
# 避免錯誤
如果seconds_remaining <= 0:
seconds_remaining = 10
else:#queued 等。

print(f'... 等待(另一個){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()
數據['document_key'] = document_key

res = requests.post(網址,資料=資料)
res_status = res.status_code # 200 如果成功(現在未使用)
tgt_bin = res._content
以 open(fn, 'w', encoding='utf-8') 為 f:
列印(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}')
列印(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':
列印(f'下載:{fn_tgt}')
download_tgt(fn_tgt、document_id、document_key)

if __name__ == '__main__':
主要()

這三種類型的 API 呼叫的處理方式類似,但
對於「用Python編寫curl指令」的主題
我不敢概括,但寫得啰嗦。

 

就這樣,在本文中:

- 我使用 DeepL 的 API 來翻譯檔案。
・我透過用 Python 表達 curl 命令的內容來創建一個程式。

感謝您的閱讀。

查詢、報價請求和
在這裡免費試用

招募經銷商!

通過電話聯繫我們

[東日本] 03-6705-5720

[西日本] 06-6628-8880

接待時間:週一~週五 9:00~18:00(節假日除外)