import os import pandas as pd import requests def translate_text(text, target_language='en'): url = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl={}&dt=t&q={}'.format(target_language, text) response = requests.get(url) if response.status_code == 200: translated_text = response.json()[0][0][0] return translated_text else: return text # Zwracamy oryginalny tekst w razie problemów z tłumaczeniem data_directory = 'data' translated_data_directory = 'translated_data' os.makedirs(translated_data_directory, exist_ok=True) for file_name in os.listdir(data_directory): if file_name.endswith('.tsv'): file_path = os.path.join(data_directory, file_name) df = pd.read_csv(file_path, sep='\t') # Tłumaczymy kolumnę 'value' na angielski df['value_en'] = df['value'].apply(lambda x: translate_text(x, 'en')) # Zapisujemy przetłumaczone dane do nowego pliku TSV translated_file_path = os.path.join(translated_data_directory, file_name) df.to_csv(translated_file_path, index=False, sep='\t')