File size: 1,669 Bytes
d7f5647
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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')