![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
一个免费, 高效, 简洁 百度翻译 接口
特点:
本项目已发布到 PYPI,你可以通过以下方法来安装。
$ pip install baidu-translate-api
注意安装包名为
baidu-translate-api
,调用包名为baidu_translate
也可以用以下方式安装最新版:
$ pip install git+https://github.com/17097239132/baidu-translate.git
import baidu_translate as fanyi
result = fanyi.translate_text('Hello, World!')
result_ru = fanyi.translate_text('Hello, World!', to=fanyi.Lang.RU)
print(result, result_ru)
# 你好,世界! Здравствуйте, Мир!
lang = fanyi.detect_language('Vue rapide')
print(lang == fanyi.Lang.FRA)
# True
result = fanyi.translate_text('我们是中国人,我们爱自己的祖国!')
print(result)
# We are Chinese, we love our motherland!
result_common = fanyi.translate_text('年化收益率')
result_domain = fanyi.translate_text('年化收益率', domain=fanyi.Domain.FINANCE) # 金融
print(result_common, '&', result_domain)
# Annualized rate of return & Annualized yield
你也可以使用异步和多线程:
import baidu_translate as fanyi
import asyncio, time
from concurrent.futures import ThreadPoolExecutor
# Make cache
fanyi.translate_text('Hi!')
langs = ['zh', 'en', 'fra', 'ru', 'ara', 'spa']
def test_sync(text):
texts = []
for lang in langs:
texts.append(fanyi.translate_text(text, to=lang))
return ' '.join(texts)
async def test_async(text):
tasks = []
for lang in langs:
tasks.append(fanyi.translate_text_async(text, to=lang))
texts = await asyncio.gather(*tasks)
return ' '.join(texts)
def test_thread(text):
with ThreadPoolExecutor() as pool:
texts = pool.map(
lambda lang: fanyi.translate_text(text, to=lang), langs
)
return ' '.join(texts)
start = time.time()
result_sync = test_sync('Good morning!')
print('Sync Time:', time.time() - start) # 4.8s~
start = time.time()
result_async = asyncio.run(test_async('Good morning!'))
print('Async Time:', time.time() - start) # 2.3s~
start = time.time()
result_thread = test_thread('Good morning!')
print('Thread Time:', time.time() - start) # 0.8s~
print(result_sync == result_async == result_thread)
# True
由于 httpx 请求库的缘故, 多线程模式的速度被显著提升, 而异步模式的效率并不突出。
翻译 content
中的内容, 并返回一个字符串。
from_
和 to
分别是翻译的源语言和目标语言。接受一个 Lang
对象, 也可传入一个字符串, 将自动转化为 Lang
对象。如果未指定或为 Lang.AUTO
, 将自动检测源语言或目标语言。
domain
是垂直翻译领域,请传入一个 Domain
对象。
如果 content
为空或源语言与目标语言相同将不做更改。
检测 content
的语种。返回一个 Lang
对象。如果检测不出来将返回 None
。
枚举对象, 包含百度支持的所有语种的 ID, 请注意是全部大写。
建议结合 IDE 自动补全使用。
class Lang(enum.Enum):
# will be replace with real language
AUTO = 'auto' # 自动检测
# Common Languages
ZH = 'zh' # 中文(简体)
EN = 'en' # 英语
SPA = 'spa' # 西班牙语
ARA = 'ara' # 阿拉伯语
FRA = 'fra' # 法语
RU = 'ru' # 俄语
# More Languages
...
枚举对象, 百度的垂直翻译领域, 请注意是全部大写。
建议结合 IDE 自动补全使用。
class Domain(enum.Enum):
COMMON = 'common' # 通用领域
BM = 'medicine' # 生物医药
ET = 'electronics' # 电子科技
WCM = 'mechanics' # 水利机械
NOVEL = 'novel' # 网络文学
FINANCE = 'finance' # 金融
MILITARY = 'military' # 军事
本项目已被放入公有领域,作者放弃所有权利。任何人都可以自由地使用本项目,而无需注明作者。
但值得注意的是,本项目违反了百度翻译(PC 版)用户使用协议的第三十一条第一款。百度公司随时可以联系本人删除项目,但由此衍生出的任何法律问题本人概不负责(包括但不限于用户使用不当而对百度公司服务器造成的损失)。
特别提醒:本项目仅供学习讨论。建议不要在商业项目中使用,以规避法律风险。
且用且珍惜!
FAQs
一个免费, 高效, 简洁百度翻译接口
We found that baidu-translate-api demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.