import json import os import re from deep_translator import GoogleTranslator def process_json_files(start, end, step, length=0, tlang="en", spaces_include=False, strip_in_braces=True, strip_diacritics=True): base_path = "texts" # Angepasster Pfad zum Verzeichnis der Dateien print(f"\nAnalyzing Books from {start} to {end}, step: {step}, length (0 is inf): {length}, \ninclude spaces: {spaces_include}, strip text in braces: {strip_in_braces}, strip diacritics: {strip_diacritics}") translator = GoogleTranslator(source='auto', target=tlang) # Setze den Übersetzer rc = 0 for i in range(start, end + 1): file_name = f"{base_path}/{i:02}.json" # Format file names, e.g., 01, 02, ..., 39 try: with open(file_name, 'r', encoding='utf-8') as file: data = json.load(file) text_blocks = data["text"] # Combine all lines in a book into a single string full_text = "" for block in text_blocks: for line in block: full_text += line #print(full_text) clean_text = full_text if strip_in_braces: clean_text = re.sub(r"\[.*?\]", "", clean_text, flags=re.DOTALL) else: clean_text = clean_text.replace("[", "").replace("]", "") if strip_diacritics: clean_text = re.sub(r"[^\u05D0-\u05EA ]+", "", clean_text) # Präzisiert auf U+05D0 bis U+05EA und " " if not spaces_include: clean_text = clean_text.replace(" ", "") # Remove spaces if " " in clean_text: print("Error: there are still spaces in the text") else: clean_text = clean_text.replace(" ", " ") # Remove spaces clean_text = clean_text.replace(" ", " ") # Remove spaces clean_text = clean_text.replace(" ", " ") # Remove spaces if " " in clean_text: print("Error: there are still double spaces in the text") #print(clean_text) if length != 0: selected_characters = clean_text[step - 1::step][:length] # Start at n-th character and select every nth character else: selected_characters = clean_text[step - 1::step] # Start at n-th character and select every nth character # Übersetzen des extrahierten Texts translated_text = translator.translate(''.join(selected_characters)) if selected_characters != "": rc += 1 print("\n" + str(rc) + ". Result, " + "Book "+ str(i) + " - " + data["title"] + "\n") print(f"HE:==> {selected_characters}") print(f"{tlang.upper()}:==> {translated_text}") # Output results or further processing except FileNotFoundError: print(f"File {file_name} not found.") except json.JSONDecodeError: print(f"File {file_name} could not be read as JSON.") except KeyError: print(f"Expected key 'text' is missing in {file_name}.") # Beispiel: n = 3, Dateien von 01 bis 39 process_json_files(start=1, end=1, step=6, length=1, tlang="en", spaces_include=False, strip_in_braces=True, strip_diacritics=True) print("\n ^^ Test: this output should be with any parameters: HE:==> ת") process_json_files(start=1, end=39, step=8240, tlang="en", spaces_include=False, strip_in_braces=True, strip_diacritics=True)