? ? ? ? 欸,最近想做一些nlp的項目,做完了中文的想做做英文的,但是呢,國內(nèi)爬蟲爬取的肯定都是中文 ,爬取外網(wǎng)的技術(shù)我沒有嘗試過,沒有把握。所以我決定啟用翻譯,在這期間chatGPT給了我非常多的方法,但是都因為各種各樣的原因一一無效。ps:大騙子GPT!
? ? ? ? 下面我會列出一些我失敗過的例子,大家可以嘗試一下,說不定有新的發(fā)現(xiàn):
youdaoapi sample:??
from youdaoapi import YoudaoApi
def translate_to_english(chinese_text):
? ? youdao = YoudaoApi()
? ? english_translation = youdao.translate(chinese_text, to_lang='en')
? ? return english_translation
chinese_string = "你好,世界!"
english_translation = translate_to_english(chinese_string)
print("English Translation:", english_translation)
translate? sample:??
from translate import Translator
def translate_to_english(chinese_text):
? ? translator = Translator(to_lang="en")
? ? english_translation = translator.translate(chinese_text)
? ? return english_translationchinese_string = "你好,世界!"
english_translation = translate_to_english(chinese_string)
print("English Translation:", english_translation)
googletrans? sample:??
from googletrans import Translator
def translate_to_english(chinese_text):
? ? translator = Translator()
? ? translated = translator.translate(chinese_text, src='zh-cn', dest='en')
? ? return translated.textchinese_string = "你好,世界!"
english_translation = translate_to_english(chinese_string)
print("English Translation:", english_translation)
百度api,yyds!非常nice!??
Step1、申請??
百度文本翻譯api申請!整體流程頁面https://console.bce.baidu.com/ai/?_=1652768945367&fromai=1#/ai/machinetranslation/overview/index
?Step2、第二步完成直接看我的代碼,只需要API Key和Secret Key便可以使用。??
# -*- coding: utf-8 -*-
# This code shows an example of text translation from English to Simplified-Chinese.
# This code runs on Python 2.7.x and Python 3.x.
# You may install `requests` to run this code: pip install requests
# Please refer to `https://api.fanyi.baidu.com/doc/21` for complete api document
import requests
import random
import json
def get_access_token():
"""
使用 AK,SK 生成鑒權(quán)簽名(Access Token)
client_id:API Key
client_secret:Secret Key
:return: access_token,或是None(如果錯誤)
"""
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {"grant_type": "client_credentials", "client_id": '5UHGfQaGLKlINhXRv1lA0tl3', "client_secret": 'evGZuz1r14MRElOt638D8GMdheQ9gKZj'}
return str(requests.post(url, params=params).json().get("access_token"))
def baidu_translate(q):
token = get_access_token()
url = 'https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=' + token
# For list of language codes, please refer to `https://ai.baidu.com/ai-doc/MT/4kqryjku9#語種列表`
from_lang = 'zh' # example: en
to_lang = 'en' # example: zh
term_ids = '' # 術(shù)語庫id,多個逗號隔開
# Build request
headers = {'Content-Type': 'application/json'}
payload = {'q': q, 'from': from_lang, 'to': to_lang, 'termIds' : term_ids}
# Send request
r = requests.post(url, params=payload, headers=headers)
result = r.json()
# Show response
# print(json.dumps(result, indent=4, ensure_ascii=False))
return result['result']['trans_result'][0]['dst']
效果截圖:
文章來源:http://www.zghlxwxcb.cn/news/detail-641578.html
耶耶耶!,盡情享用吧,爬取數(shù)據(jù)的同時加一個這個函數(shù)轉(zhuǎn)成英文再儲存為csv等,完美解決這個小小的難題。值得記錄一下勒!--<-<-<@??文章來源地址http://www.zghlxwxcb.cn/news/detail-641578.html
到了這里,關(guān)于【百度翻譯api】中文自動翻譯為英文的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!