-
Notifications
You must be signed in to change notification settings - Fork 0
/
translation_helper.py
38 lines (27 loc) · 1.14 KB
/
translation_helper.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
from deep_translator import GoogleTranslator
from constants import *
def is_language_supported(source: str, target: str) -> bool:
langs_dict = GoogleTranslator().get_supported_languages(as_dict=True)
langs = set(langs_dict.keys()).union(set(langs_dict.values()))
return {source, target}.issubset(langs)
def text_partition(texts: list[str], max_chars=MAX_CHARS_GOOGLE_TRANSLATOR) -> list[list[str]]:
partitions = []
current_partition = []
current_length = 0
limit = max_chars - len(texts) + 1
for text in texts:
text_length = len(text)
if current_length + text_length <= limit:
current_partition.append(text)
current_length += text_length
else:
partitions.append(current_partition)
current_partition = [text]
current_length = text_length
if current_partition:
partitions.append(current_partition)
return partitions
def translate_partition(partition: list[str], source: str, target: str) -> list[str]:
translation = GoogleTranslator(
source, target).translate('\n'.join(partition))
return translation.split('\n')