Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: translate2english.py
- # Author: Jeoi Reqi
- """
- Chinese To English Translator
- This Python script translates Chinese text into English.
- Usage:
- 1. Ensure you have Python 3 installed.
- 2. Replace 'input_cn.txt' with the name of your file containing Chinese text.
- 3. Replace 'output_en.txt' with the desired name for the translated English text output file.
- 4. Run the script, and the translated text will be saved in the specified output file.
- Requirements:
- - Python 3
- """
- import os
- from deep_translator import GoogleTranslator
- import re
- def detect_language(text):
- # Use a simple regex to detect if the text contains Chinese characters
- if re.search(r'[\u4e00-\u9fff]', text):
- return 'zh'
- else:
- return 'en'
- def translate_text(text, target_language='en'):
- translator = GoogleTranslator(source='auto', target=target_language)
- translated_text = translator.translate(text)
- return translated_text
- def split_text_into_sentences(text):
- # Split the text into sentences based on punctuation
- sentences = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s', text)
- return sentences
- def main(input_file, output_file):
- with open(input_file, 'r', encoding='utf-8') as file:
- mixed_text = file.read()
- sentences = split_text_into_sentences(mixed_text)
- translated_sentences = []
- for sentence in sentences:
- source_language = detect_language(sentence)
- if source_language == 'zh':
- translated_sentence = translate_text(sentence)
- translated_sentences.append(translated_sentence)
- else:
- translated_sentences.append(sentence)
- translated_text = ' '.join(translated_sentences)
- with open(output_file, 'w', encoding='utf-8') as output_file:
- output_file.write(translated_text)
- if __name__ == "__main__":
- current_directory = os.getcwd()
- input_file_path = os.path.join(current_directory, 'input_cn.txt') # Replace 'input_cn.txt' with the text file containing the Chinese text.
- output_file_path = os.path.join(current_directory, 'output_en.txt') # Replace 'outpu_en.txt' with the desired name for the translated text.
- main(input_file_path, output_file_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement