====== Arkhalia překladový Python skript ====== Tento skript je použit pro překlad wiki: # Jurass17 2025 # Vstupní parametry CHATGPT_API_KEY = "" INPUT_DIRECTORY_ABSOLUTE = "/Users/jiri/projekty/my_python_scripts/arkhalia_translate/en" OUTPUT_DIRECTORY_ABSOLUTE = "/Users/jiri/projekty/my_python_scripts/arkhalia_translate/cs" INPUT_LANGUAGE_AND_FOLDER_NAME = "en" OUTPUT_LANGUAGE_AND_FOLDER_NAME = "cs" CHATGPT_COMMAND = f""" You are a helpful translator from {INPUT_LANGUAGE_AND_FOLDER_NAME} to {OUTPUT_LANGUAGE_AND_FOLDER_NAME}. The text I will give you to translate is the DokuWiki Article text. It contains some DokuWiki special formatting syntax, like caption "===== Some caption =====", or link to other pages "[[cs:ruleset:touch_attack|Dotykový útok]]", or "". Is is important that during your translation you will not malform the original syntax of the DokuWiki elements. You are smart enough to do it, I know that. Maybe it will be handy for you that the DokuWiki Articles you are going to translate is the community driven DokuWiki related to the game Neverwinter Nights 1 online persistent world. Maybe, if you will be unsure how to translate something, like names of game feats, or spells, it will help you to know it. Also, this is the set of rules I want you to comprehend during the translation: 1. When you encounter a link like "[[cs:craftingguide]]", then I want your output to be the exact original version: "[[cs:craftingguide]]" 2. When you encounter a link containing custom text like "[[cs:craftingguide|This is a custom caption]]", then I want your output to translate the custom caption like this: "[[cs:craftingguide|Tohle je custom popisek]]" 3. The word "Feats" shall be translated as "Odbornosti" 3. The word "Classes" shall be translated as "Povolání" 4. Consider not to translate names of some Classes and keep the name original, like "Acolyte of Skin" sounds weird in Czech to be translated. 5. The word "Skills" shall be translated as "Dovednosti" That's all. """ # Import import os from openai import OpenAI from pathlib import Path # Konfigurace client = OpenAI(api_key=CHATGPT_API_KEY) # Definice funkcí def translate_text(text): response = client.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": f"{CHATGPT_COMMAND}"}, {"role": "user", "content": f"Translate this:\n\n{text}"} ], temperature=0.5, ) return response.choices[0].message.content def find_all_txt_files_in_directory(folder_path): txt_file_paths = [] for subdir, dirs, files in os.walk(folder_path): for file in files: path = os.path.join(subdir, file) if not path.endswith(".txt"): print(f"Přeskakuji soubor: {path}") continue txt_file_paths.append(path) txt_file_paths = sorted(txt_file_paths) return txt_file_paths def load_file_content(path): with open(path, 'r') as f: return f.read() def save_file_content(path, content): file_path = Path(path) file_path.parent.mkdir(parents=True, exist_ok=True) with open(file_path, 'w') as f: return f.write(content) def modify_links_in_dokuwiki_article(content: str): return content.replace(f"[[{INPUT_LANGUAGE_AND_FOLDER_NAME}:", f"[[{OUTPUT_LANGUAGE_AND_FOLDER_NAME}:") # Provedení if __name__ == "__main__": txt_file_paths = find_all_txt_files_in_directory(INPUT_DIRECTORY_ABSOLUTE) try: for file_path in txt_file_paths: # Překlad file_content = load_file_content(file_path) translated = translate_text(file_content) # Úprava odkazů translated = modify_links_in_dokuwiki_article(translated) # Převod vstupní cesty na výstupní cestu relative_file_path = file_path.replace(INPUT_DIRECTORY_ABSOLUTE, '') output_path = OUTPUT_DIRECTORY_ABSOLUTE + relative_file_path # Uložení save_file_content(output_path, translated) print(f'Uloženo ({txt_file_paths.index(file_path)}/{len(txt_file_paths)}): {output_path}') except BaseException as e: print(f"Došlo k chybě. Cesta k vstupnímu souboru: {file_path}, výjimka: {e}")