-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate_ipynb.py
230 lines (196 loc) · 8.26 KB
/
translate_ipynb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import os
import json
import requests
import time
import hashlib
import hmac
import nbformat
from tqdm import tqdm
import re
# 腾讯翻译API配置信息
SECRET_ID = 'AKIDjMqBMSM6h8OUPJ4byEI8rOhlcJtGSSIx'
SECRET_KEY = 'WrCOPDmEcu33ubLzVq01ta2IMEtPl8f2'
REGION = 'ap-guangzhou'
ENDPOINT = 'tmt.tencentcloudapi.com'
SERVICE = 'tmt'
VERSION = '2018-03-21'
ACTION = 'TextTranslate'
# 请求间隔时间(秒)
REQUEST_INTERVAL = 0.5
# 最大重试次数
MAX_RETRIES = 5
# 每次翻译的最大字符数
MAX_CHARS_PER_REQUEST = 2000
def sign_request(secret_id, secret_key, method, endpoint, uri, params):
timestamp = int(time.time())
date = time.strftime('%Y-%m-%d', time.gmtime(timestamp))
# 1. Build Canonical Request String
http_request_method = method
canonical_uri = uri
canonical_querystring = ''
canonical_headers = f'content-type:application/json\nhost:{endpoint}\n'
signed_headers = 'content-type;host'
payload_hash = hashlib.sha256(json.dumps(params).encode('utf-8')).hexdigest()
canonical_request = (http_request_method + '\n' +
canonical_uri + '\n' +
canonical_querystring + '\n' +
canonical_headers + '\n' +
signed_headers + '\n' +
payload_hash)
# 2. Build String to Sign
algorithm = 'TC3-HMAC-SHA256'
credential_scope = f"{date}/{SERVICE}/tc3_request"
string_to_sign = (algorithm + '\n' +
str(timestamp) + '\n' +
credential_scope + '\n' +
hashlib.sha256(canonical_request.encode('utf-8')).hexdigest())
# 3. Sign String
def sign(key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
secret_date = sign(('TC3' + secret_key).encode('utf-8'), date)
secret_service = sign(secret_date, SERVICE)
secret_signing = sign(secret_service, 'tc3_request')
signature = hmac.new(secret_signing, string_to_sign.encode('utf-8'), hashlib.sha256).hexdigest()
# 4. Build Authorization Header
authorization = (f"{algorithm} "
f"Credential={secret_id}/{credential_scope}, "
f"SignedHeaders={signed_headers}, "
f"Signature={signature}")
return authorization, timestamp
def translate_to_chinese(text):
# 确保text是字符串类型
if not isinstance(text, str):
text = str(text)
# 如果文本过长,分段翻译
if len(text) > MAX_CHARS_PER_REQUEST:
chunks = [text[i:i+MAX_CHARS_PER_REQUEST] for i in range(0, len(text), MAX_CHARS_PER_REQUEST)]
translated_chunks = []
for chunk in chunks:
translated_chunks.append(_translate_chunk(chunk))
time.sleep(REQUEST_INTERVAL) # 控制请求频率
return ''.join(translated_chunks)
else:
return _translate_chunk(text)
def _translate_chunk(text):
params = {
"SourceText": text,
"Source": "en",
"Target": "zh",
"ProjectId": 0
}
method = 'POST'
uri = '/'
for attempt in range(MAX_RETRIES):
try:
authorization, timestamp = sign_request(SECRET_ID, SECRET_KEY, method, ENDPOINT, uri, params)
headers = {
'Content-Type': 'application/json',
'Host': ENDPOINT,
'X-TC-Action': ACTION,
'X-TC-Timestamp': str(timestamp),
'X-TC-Version': VERSION,
'X-TC-Region': REGION,
'Authorization': authorization
}
response = requests.post(f'https://{ENDPOINT}{uri}', headers=headers, data=json.dumps(params))
result = response.json()
if 'Response' in result and 'TargetText' in result['Response']:
return result['Response']['TargetText']
else:
print(f"翻译API响应错误: {result}")
if attempt < MAX_RETRIES - 1:
time.sleep(REQUEST_INTERVAL * 2) # 重试前等待更长时间
continue
return text # 如果翻译失败,返回原文
except Exception as e:
print(f"翻译请求失败: {str(e)}")
if attempt < MAX_RETRIES - 1:
time.sleep(REQUEST_INTERVAL * 2)
continue
return text
return text
def fix_markdown_format(text):
# 修复Markdown标题格式
text = re.sub(r'^(#{1,6})([^#\s])', r'\1 \2', text, flags=re.MULTILINE)
# 修复Markdown链接格式
text = re.sub(r'\[([^\]]+)\]\s*(([^)]+))', r'[\1] (\2)', text)
# 修复列表格式
text = re.sub(r'^\s*([*+-])\s*([^\s])', r'\1 \2', text, flags=re.MULTILINE)
# 修复代码块格式
text = re.sub(r'```\s*([^\n]+)', r'```\1', text)
# 修复引用格式
text = re.sub(r'^>\s*([^\s])', r'> \1', text, flags=re.MULTILINE)
# 修复图片链接格式
text = re.sub(r'!\[([^\]]*)\]\s*(([^)]+))', r'![\1] (\2)', text)
# 修复表格格式
text = re.sub(r'\|([^|]+)\|', lambda m: '|' + '|'.join(cell.strip() for cell in m.group(1).split('|')) + '|', text)
# 修复多余的逗号
text = text.replace(',,,', '')
# 修复JSON拼写错误
text = text.replace('SON', 'JSON')
# 删除多余的空格
text = re.sub(r'\s+', ' ', text)
return text
def translate_markdown_file(md_path):
"""翻译Markdown文件"""
with open(md_path, 'r', encoding='utf-8') as f:
content = f.read()
# 分段翻译
sections = content.split('\n\n')
translated_sections = []
for section in tqdm(sections, desc=f"翻译 {os.path.basename(md_path)}"):
translated = translate_to_chinese(section)
translated = fix_markdown_format(translated)
translated_sections.append(translated)
time.sleep(REQUEST_INTERVAL)
# 保存翻译后的文件
output_path = md_path.replace('.md', '_zh.md')
with open(output_path, 'w', encoding='utf-8') as f:
f.write('\n\n'.join(translated_sections))
print(f"已翻译并保存: {output_path}")
def translate_notebook(notebook_path):
# 使用nbformat读取原始notebook
notebook = nbformat.read(notebook_path, as_version=4)
total_cells = len(notebook.cells)
print(f"开始翻译: {notebook_path} ({total_cells}个单元格)")
with tqdm(total=total_cells, desc="翻译进度") as pbar:
for cell in notebook.cells:
if cell.cell_type == 'markdown' or cell.cell_type == 'code':
# 翻译markdown内容和代码注释
if cell.cell_type == 'code':
# 翻译代码注释
source_lines = cell.source.splitlines()
translated_lines = []
for line in source_lines:
if line.strip().startswith('#'):
translated_line = translate_to_chinese(line)
time.sleep(REQUEST_INTERVAL) # 控制请求频率
translated_lines.append(translated_line)
else:
translated_lines.append(line)
cell.source = '\n'.join(translated_lines)
else:
# 翻译markdown内容
cell.source = translate_to_chinese(cell.source)
# 修复Markdown格式
cell.source = fix_markdown_format(cell.source)
time.sleep(REQUEST_INTERVAL)
pbar.update(1)
# 保存翻译后的notebook
output_path = notebook_path.replace('.ipynb', '_zh.ipynb')
nbformat.write(notebook, output_path)
print(f"\n已翻译并保存: {output_path}")
def main():
base_dir = 'teachopencadd/talktorials'
# 获取所有教程目录
tutorial_dirs = [d for d in os.listdir(base_dir)
if os.path.isdir(os.path.join(base_dir, d))
and d.startswith('T')]
for tutorial_dir in tutorial_dirs:
dir_path = os.path.join(base_dir, tutorial_dir)
# 翻译README.md
readme_path = os.path.join(dir_path, 'README.md')
if os.path.exists(readme_path):
translate_markdown_file(readme_path)
if __name__ == '__main__':
main()