raena-crawler-engine/hasaki_crawler_engine/Util.py

24 lines
714 B
Python
Raw Permalink Normal View History

2024-03-14 05:16:59 +00:00
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