Skip to content

Commit

Permalink
update Translate.py
Browse files Browse the repository at this point in the history
  • Loading branch information
lemisky committed Apr 19, 2022
1 parent c56e997 commit 35a1975
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
8 changes: 4 additions & 4 deletions pygtrans/ApiKeyTranslate.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ def detect(self, q: Union[str, List[str]]) -> Union[DetectResponse, List[DetectR
ll = []
for ql in split_list(q):
for qli in split_list_by_content_size(ql):
for _ in range(3):
for i in range(1, 4):
response = self.session.post(self._DETECT_URL, params={
'key': self.api_key
}, data={
'q': qli
})
if response.status_code == 429:
time.sleep(5)
time.sleep(5 * i)
continue
break
# noinspection PyUnboundLocalVariable
Expand Down Expand Up @@ -220,12 +220,12 @@ def translate(
ll = []
for ql in split_list(q):
for qli in split_list_by_content_size(ql):
for _ in range(3):
for i in range(1, 4):
response = self.session.post(self._BASE_URL, params={
'key': self.api_key, 'target': target, 'source': source, 'format': fmt, 'model': model
}, data={'q': qli})
if response.status_code == 429:
time.sleep(5)
time.sleep(5 * i)
continue
break
# noinspection PyUnboundLocalVariable
Expand Down
16 changes: 8 additions & 8 deletions pygtrans/Translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ def detect(self, q: str) -> Union[DetectResponse, Null]:
>>> d = client.detect('こんにちは')
>>> assert d.language == 'ja'
"""
for _ in range(3):
for i in range(1, 4):
response = self.session.post(
self.DETECT_URL,
params={'dj': 1, 'sl': 'auto', 'ie': 'UTF-8', 'oe': 'UTF-8', 'client': 'at'},
data={'q': q}
)
if response.status_code == 429:
time.sleep(5)
time.sleep(5 * i)
continue
break
# noinspection PyUnboundLocalVariable
Expand Down Expand Up @@ -149,10 +149,10 @@ def translate(
if q == '':
return TranslateResponse('')

for _ in range(3):
for i in range(1, 4):
response = self.__translate(q=q, target=target, source=source, fmt=fmt, v='1.0')
if response.status_code == 429:
time.sleep(5)
time.sleep(5 * i)
continue
break
# noinspection PyUnboundLocalVariable
Expand All @@ -173,15 +173,15 @@ def __translate(
source = self.source
if fmt is None:
fmt = self.fmt
for _ in range(3):
for i in range(1, 4):
response = self.session.post(
self.TRANSLATE_URL,
params={'tl': target, 'sl': source, 'ie': 'UTF-8', 'oe': 'UTF-8', 'client': 'at', 'dj': '1',
'format': fmt, 'v': v},
data={'q': q}
)
if response.status_code == 429:
time.sleep(5)
time.sleep(5 * i)
continue
break
# noinspection PyUnboundLocalVariable
Expand All @@ -197,7 +197,7 @@ def tts(self, q: str, target: str = None) -> Union[bytes, Null]:
if target is None:
target = self.target

for _ in range(3):
for i in range(1, 4):
response = self.session.get(
self.TTS_URL,
params={
Expand All @@ -207,7 +207,7 @@ def tts(self, q: str, target: str = None) -> Union[bytes, Null]:
'q': q
})
if response.status_code == 429:
time.sleep(5)
time.sleep(5 * i)
continue
break
# noinspection PyUnboundLocalVariable
Expand Down
8 changes: 6 additions & 2 deletions pygtrans/TranslateResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ def __init__(self, translatedText: str, detectedSourceLanguage: str = None, mode
:param detectedSourceLanguage: 如果初始请求中没有传递源语言,则自动检测初始请求的源语言。如果通过了源语言,则不会自动检测语言,并且将省略此字段。
:param model: 翻译模型。可以是基于短语的机器翻译 (PBMT) 模型的基础,也可以是神经机器翻译 (NMT) 模型的 nmt。如果您的请求中没有包含模型参数,则该字段不会包含在响应中。
"""
if isinstance(translatedText, list):
self.translatedText = translatedText[0]
self.detectedSourceLanguage = translatedText[1]
else:
self.translatedText = translatedText
self.detectedSourceLanguage = detectedSourceLanguage

self.translatedText = translatedText
self.detectedSourceLanguage = detectedSourceLanguage
self.model = model

def __repr__(self):
Expand Down

0 comments on commit 35a1975

Please sign in to comment.