本文共 1136 字,大约阅读时间需要 3 分钟。
使用 pip 安装 pygtransTranslate:
pip install pygtrans
from pygtrans import Translate
client = Translate()text = client.detect('Answer the question.')# text.language 将返回检测到的语言 text = client.translate('Look at these pictures and answer the questions.')# text.translatedText 将返回翻译后的文本 texts = [ 'Good morning. What can I do for you?', 'Read aloud and underline the sentences about booking a flight.', 'May I have your name and telephone number?']translated_texts = client.translate(texts)# translated_texts 是一个包含所有翻译结果的列表
# 翻译到中文text = client.translate('请多多指教')# text.translatedText -> '请多多指教'# 翻译到日语text = client.translate('请多多指教', target='ja')# text.translatedText -> 'お知らせ下さい'# 翻译到韩语text = client.translate('请多多指教', target='ko')# text.translatedText -> '조언 부탁드립니다' tts = client.tts('やめて', target='ja')# 生成语音文件,保存为 'やめて.mp3'with open('やめて.mp3', 'wb') as f: f.write(tts) pygtransTranslate 可以处理长列表的翻译任务,支持超过 10 万个请求,适合大规模自动化任务。
请参考 pygtrans API 文档 获取详细信息。
此外,pygtransTranslate 是一个开源项目,旨在为开发者提供简单易用的翻译 API 接口。
转载地址:http://krafk.baihongyu.com/