raena-crawler-engine/hasaki_crawler_engine/Util.py

24 lines
714 B
Python

from deep_translator import GoogleTranslator
# def translate_text_to_english(text):
# if text:
# translated = GoogleTranslator(source='auto', target='en').translate(text)
# return translated
# return text
#
def translate_text_to_english(text):
if text:
chunk_size = 4800
text_chunks = [text[i:i + chunk_size] for i in range(0, len(text), chunk_size)]
translated_chunks = []
for chunk in text_chunks:
translated_chunk = GoogleTranslator(source='auto', target='en').translate(chunk)
translated_chunks.append(translated_chunk)
translated_text = ' '.join(translated_chunks)
return translated_text
return text