Advertisement
Python253

translate2english

Mar 5th, 2024
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: translate2english.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. Chinese To English Translator
  8.  
  9. This Python script translates Chinese text into English.
  10.  
  11. Usage:
  12. 1. Ensure you have Python 3 installed.
  13. 2. Replace 'input_cn.txt' with the name of your file containing Chinese text.
  14. 3. Replace 'output_en.txt' with the desired name for the translated English text output file.
  15. 4. Run the script, and the translated text will be saved in the specified output file.
  16.  
  17. Requirements:
  18. - Python 3
  19. """
  20.  
  21. import os
  22. from deep_translator import GoogleTranslator
  23. import re
  24.  
  25. def detect_language(text):
  26.     # Use a simple regex to detect if the text contains Chinese characters
  27.     if re.search(r'[\u4e00-\u9fff]', text):
  28.         return 'zh'
  29.     else:
  30.         return 'en'
  31.  
  32. def translate_text(text, target_language='en'):
  33.     translator = GoogleTranslator(source='auto', target=target_language)
  34.     translated_text = translator.translate(text)
  35.     return translated_text
  36.  
  37. def split_text_into_sentences(text):
  38.     # Split the text into sentences based on punctuation
  39.     sentences = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s', text)
  40.     return sentences
  41.  
  42. def main(input_file, output_file):
  43.     with open(input_file, 'r', encoding='utf-8') as file:
  44.         mixed_text = file.read()
  45.  
  46.     sentences = split_text_into_sentences(mixed_text)
  47.  
  48.     translated_sentences = []
  49.     for sentence in sentences:
  50.         source_language = detect_language(sentence)
  51.         if source_language == 'zh':
  52.             translated_sentence = translate_text(sentence)
  53.             translated_sentences.append(translated_sentence)
  54.         else:
  55.             translated_sentences.append(sentence)
  56.  
  57.     translated_text = ' '.join(translated_sentences)
  58.  
  59.     with open(output_file, 'w', encoding='utf-8') as output_file:
  60.         output_file.write(translated_text)
  61.  
  62. if __name__ == "__main__":
  63.     current_directory = os.getcwd()
  64.     input_file_path = os.path.join(current_directory, 'input_cn.txt')  # Replace 'input_cn.txt' with the text file containing the Chinese text.
  65.     output_file_path = os.path.join(current_directory, 'output_en.txt')  # Replace 'outpu_en.txt' with the desired name for the translated text.
  66.     main(input_file_path, output_file_path)
  67.  
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement