-
Notifications
You must be signed in to change notification settings - Fork 34
/
translate.py
90 lines (79 loc) · 2.74 KB
/
translate.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
# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
from alibabacloud_alimt20181012.client import Client as alimt20181012Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_alimt20181012 import models as alimt_20181012_models
from load_config import get_yaml_config
config = get_yaml_config()
access_key = config["potential"]["access_key_id"]
secret = config["potential"]["access_key_secret"]
language = config["book"]["language"]
class Sample:
def __init__(self):
pass
@staticmethod
def create_client(
access_key_id: str,
access_key_secret: str,
region_id: str,
) -> alimt20181012Client:
config = open_api_models.Config()
config.access_key_id = access_key_id
config.access_key_secret = access_key_secret
config.region_id = region_id
config.connect_timeout = 5000
config.read_timeout = 10000
return alimt20181012Client(config)
@staticmethod
async def create_client_async(
access_key_id: str,
access_key_secret: str,
region_id: str,
) -> alimt20181012Client:
config = open_api_models.Config()
config.access_key_id = access_key_id
config.access_key_secret = access_key_secret
config.region_id = region_id
return alimt20181012Client(config)
@staticmethod
def main(
text,
region="cn-hangzhou",
format_type="text",
source_language=language,
target_language="en",
) -> None:
if language == "en":
return text
client = Sample.create_client(
access_key, secret, region
)
request = alimt_20181012_models.TranslateGeneralRequest(
format_type=format_type,
source_language=source_language,
target_language=target_language,
source_text=text,
)
response = client.translate_general(request)
return response.body.data.translated
@staticmethod
async def main_async(
text,
region="cn-hangzhou",
format_type="text",
source_language="zh",
target_language="en",
) -> None:
client = await Sample.create_client_async(
access_key, secret, region
)
request = alimt_20181012_models.TranslateGeneralRequest(
format_type=format_type,
source_language=source_language,
target_language=target_language,
source_text=text,
)
response = await client.translate_general_async(request)
return response.body.data.translated
if __name__ == "__main__":
print(Sample.main("你好"))