Last active
January 25, 2025 08:13
-
-
Save scillidan/e95773454d79dc047aeed016fb00daef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Modify from https://github.com/LexsionLee/tencent-translate-for-goldendict | |
# For translate multi-language to Chinese, and translate Chinese to English. I hope so. | |
# See supported languages list on https://cloud.tencent.com/document/api/551/15620. | |
# Write by GPT-3.5 🧙, scillidan 🤡 | |
# How to use | |
# pip install tencentcloud-sdk-python langid | |
# python this.py <input> | |
import json | |
import unicodedata | |
from tencentcloud.common import credential | |
from tencentcloud.common.profile.client_profile import ClientProfile | |
from tencentcloud.common.profile.http_profile import HttpProfile | |
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException | |
from tencentcloud.tmt.v20180321 import tmt_client, models | |
import argparse | |
import textwrap | |
import langid | |
# Add your api-key here. | |
SecretId = "" | |
SecretKey = "" | |
def get_args(): | |
parser = argparse.ArgumentParser( | |
formatter_class=argparse.RawDescriptionHelpFormatter, | |
description=textwrap.dedent('''\ | |
''')) | |
parser.add_argument('qText', metavar='Text', type=str, default='', help='Original text for query.') | |
return parser.parse_args() | |
def get_language_code(text): | |
try: | |
detected_lang = langid.classify(text) | |
return detected_lang[0] | |
except: | |
return None | |
def translate_text(source_text, source_lang, target_lang): | |
try: | |
cred = credential.Credential(SecretId, SecretKey) | |
httpProfile = HttpProfile() | |
httpProfile.endpoint = "tmt.tencentcloudapi.com" | |
clientProfile = ClientProfile() | |
clientProfile.httpProfile = httpProfile | |
client = tmt_client.TmtClient(cred, "ap-shanghai", clientProfile) | |
req = models.TextTranslateRequest() | |
params = { | |
"SourceText": source_text, | |
"Source": source_lang, | |
"Target": target_lang, | |
"ProjectId": 0 | |
} | |
req.from_json_string(json.dumps(params)) | |
resp = client.TextTranslate(req) | |
dictResp = json.loads(resp.to_json_string()) | |
return dictResp['TargetText'], target_lang | |
except TencentCloudSDKException as err: | |
print(err) | |
return None, None | |
args = get_args() | |
source_text = args.qText | |
source_lang = get_language_code(source_text) | |
if source_lang == 'zh': | |
# Input is Chinese | |
translated_text, target_lang = translate_text(source_text, "zh", "en") | |
else: | |
# Input is not Chinese | |
translated_text, target_lang = translate_text(source_text, source_lang, "zh") | |
if translated_text is not None: | |
print("") | |
print(translated_text) | |
print("") | |
print("") | |
print(source_text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment