MME-Benchmark-pt / translate_MME.py
LucasLima's picture
Upload 2377 files
d7f5647 verified
import os
from deep_translator import GoogleTranslator
def translate_text(text):
"""Traduz um texto do inglês para o português."""
return GoogleTranslator(source='en', target='pt').translate(text)
def translate_files_in_directory(root_dir):
"""Percorre todas as pastas na raiz e traduz arquivos .txt dentro de 'questions_answers_YN'."""
for subdir in os.listdir(root_dir):
qa_path = os.path.join(root_dir, subdir)
if os.path.exists(qa_path) and os.path.isdir(qa_path):
for file in os.listdir(qa_path):
if file.endswith('.txt'):
file_path = os.path.join(qa_path, file)
translate_txt_file(file_path)
def translate_txt_file(file_path):
"""Traduz um arquivo .txt linha por linha e substitui o original."""
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
translated_lines = []
for line in lines:
parts = line.strip().split('\t') # Divide pergunta e resposta
if len(parts) == 2:
question, answer = parts
translated_question = translate_text(question)
translated_answer = "Sim" if answer.lower() == "yes" else "Não"
translated_lines.append(f"{translated_question}\t{translated_answer}\n")
else:
translated_lines.append(line) # Mantém linhas mal formatadas inalteradas
with open(file_path, 'w', encoding='utf-8') as f:
f.writelines(translated_lines)
print(f"Arquivo traduzido: {file_path}")
# Exemplo de uso
translate_files_in_directory('MME_Benchmark')