Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > Non-English Discussions > Deutsches Forum > Software

Notices

Reply
 
Thread Tools Search this Thread
Old Today, 06:41 AM   #1
Leonatus
Wizard
Leonatus ought to be getting tired of karma fortunes by now.Leonatus ought to be getting tired of karma fortunes by now.Leonatus ought to be getting tired of karma fortunes by now.Leonatus ought to be getting tired of karma fortunes by now.Leonatus ought to be getting tired of karma fortunes by now.Leonatus ought to be getting tired of karma fortunes by now.Leonatus ought to be getting tired of karma fortunes by now.Leonatus ought to be getting tired of karma fortunes by now.Leonatus ought to be getting tired of karma fortunes by now.Leonatus ought to be getting tired of karma fortunes by now.Leonatus ought to be getting tired of karma fortunes by now.
 
Leonatus's Avatar
 
Posts: 1,120
Karma: 11934619
Join Date: Mar 2013
Location: Guben, Brandenburg, Germany
Device: Kobo Clara 2E, Tolino Shine 3
Aktualisierung der Rechtschreibung unter Linux durch Python-GUI

Um epubs von der alten deutschen Rechtschreibung auf die neue zu aktualisieren, habe ich die KI meines Vertrauens zur Erstellung eines entsprechenden Skripts aufgefordert (Betriebssystem: Linux Mint 22.3 Cinnamon 64 bit). Das Ergebnis lässt sich sehen: Es funktioniert blitzschnell und ist relativ effektiv, allerdings bei Weitem nicht perfekt (zahlreiche ss-ß-Fehler usw.). Man kann es aber manuell verbessern, was ich im Lauf der Zeit vorhabe. Für Interessenten hier ein kurzer Abriss über die einzelnen Schritte, wie sie mir von der KI (da ich kein Techniker bin) zusammengefasst wurden:

Ziel:
-----
Automatisierte Modernisierung historischer EPUB-Dateien (19. Jh.)
über eine Python-GUI, die ein Shell-Skript und ein Modernisierungs-
modul aufruft.

Dateien:
--------
/home/xxxxxxx/modernize.py
/home/xxxxxxx/modernize_epub.sh
/home/xxxxxxx/modernize_gui.py

##Diese Dateien sind mit einem Texteditor zu erstellen. Man achte auf korrekte Endungen! Der Speicherort ist ein Vorschlag; ich habe sie wie angegeben gespeichert.
##Zusätzlich ist ebendort als Arbeitsverzeichnis ein Ordner namens „epub_work“ anzulegen.

Inhalt von modernize.py:
Code:
import re
import sys

# ============================================================
#   1) WÖRTERBÜCHER FÜR DAS 19. JAHRHUNDERT
# ============================================================

# 1.1 ß → ss (klassische Reformfälle)
DICT_SCHARF = {
    "daß": "dass", "Daß": "Dass",
    "muß": "muss", "Muß": "Muss",
    "Fluß": "Fluss", "fluß": "fluss",
    "Schloß": "Schloss", "schloß": "schloss",
    "Haß": "Hass", "haß": "hass",
    "naß": "nass", "Naß": "Nass",
    "faß": "fass", "Faß": "Fass",
}

# 1.2 -niß → -nis
DICT_NISS = {
    "Ergebniß": "Ergebnis",
    "Verhältniß": "Verhältnis",
    "Verhältniße": "Verhältnisse",
    "Verhältnißen": "Verhältnissen",
    "Zeugniß": "Zeugnis",
    "Kenntniß": "Kenntnis",
    "Fleißniß": "Fleißnis",
    "Bedürfniß": "Bedürfnis",
    "Ereigniß": "Ereignis",
}

# 1.3 Th → T (19. Jh.)
DICT_TH = {
    "Thal": "Tal", "Thale": "Tale", "Thales": "Tales",
    "Thäler": "Täler", "Thalweg": "Talweg",
    "Theil": "Teil", "Theile": "Teile", "Theilen": "Teilen", "Theils": "Teils",
    "Thräne": "Träne", "Thränen": "Tränen",
    "Thür": "Tür", "Thüre": "Türe", "Thüren": "Türen",
    "Thon": "Ton", "Thons": "Tons", "Thonerde": "Tonerde",
    "Thurm": "Turm", "Thier": "Tier",
    "Thätigkeit": "Tätigkeit",
}

# 1.4 Fremdwörter (Ph → F, Cultur → Kultur)
DICT_FREMDE = {
    "Phantasie": "Fantasie",
    "Phantast": "Fantast",
    "Phantasten": "Fantasten",
    "Photographie": "Fotografie",
    "Photograph": "Fotograf",
    "Photographen": "Fotografen",
    "Photographisches": "Fotografisches",
    "Cultur": "Kultur",
    "Culturgeschichte": "Kulturgeschichte",
}

# Gesamtes Wörterbuch
MASTER_DICT = {}
MASTER_DICT.update(DICT_SCHARF)
MASTER_DICT.update(DICT_NISS)
MASTER_DICT.update(DICT_TH)
MASTER_DICT.update(DICT_FREMDE)

# ============================================================
#   2) REGEX-REGELN (vorsichtig eingesetzt)
# ============================================================

REGEX_RULES = [
    # -niß generisch → -nis
    (r"niß\b", "nis"),

    # Th → T am Wortanfang
    (r"\bTh", "T"),

    # Ae/Oe/Ue → Ä/Ö/Ü am Wortanfang
    (r"\bAe", "Ä"), (r"\bae", "ä"),
    (r"\bOe", "Ö"), (r"\boe", "ö"),
    (r"\bUe", "Ü"), (r"\bue", "ü"),

    # Ph → F (nur am Wortanfang)
    (r"\bPh", "F"),
]

# ============================================================
#   3) TOKENISIERUNG
# ============================================================

WORD_RE = re.compile(r"(\w+|\W+)", re.UNICODE)

def modernize_token(token: str) -> str:
    """Modernisiert ein einzelnes Wort."""
    if token in MASTER_DICT:
        return MASTER_DICT[token]
    return token

def modernize_text_wordwise(text: str) -> str:
    parts = WORD_RE.findall(text)
    out = []
    for p in parts:
        if any(ch.isalpha() for ch in p):
            out.append(modernize_token(p))
        else:
            out.append(p)
    return "".join(out)

def modernize_text_regex(text: str) -> str:
    for pattern, repl in REGEX_RULES:
        text = re.sub(pattern, repl, text)
return text

# ============================================================
#   4) HAUPTFUNKTION
# ============================================================

def modernize(text: str) -> str:
    text = modernize_text_wordwise(text)
    text = modernize_text_regex(text)
    return text

# ============================================================
#   5) CLI
# ============================================================

def main():
    if len(sys.argv) != 3:
        print("Usage: python3 modernize.py input.txt output.txt")
        sys.exit(1)

    infile, outfile = sys.argv[1], sys.argv[2]

    with open(infile, "r", encoding="utf-8") as f:
        text = f.read()

    new = modernize(text)

    with open(outfile, "w", encoding="utf-8") as f:
        f.write(new)

if __name__ == "__main__":
main()
Inhalt von modernize_epub.sh:
Code:
#!/bin/bash

# Nutzung:
# ./modernize_epub.sh input.epub output.epub

INPUT="$1"
OUTPUT="$2"

if [ -z "$INPUT" ] || [ -z "$OUTPUT" ]; then
    echo "Usage: $0 input.epub output.epub"
    exit 1
fi

# Arbeitsverzeichnis
WORKDIR="epub_work"

# Aufräumen, falls vorhanden
rm -rf "$WORKDIR"
mkdir "$WORKDIR"

# EPUB entpacken
unzip -qq "$INPUT" -d "$WORKDIR"

# Alle XHTML-Dateien modernisieren
find "$WORKDIR" -type f -name "*.xhtml" | while read -r file; do
    echo "Modernisiere: $file"
    python3 /home/xxxxx/modernize.py "$file" "$file.tmp"
    mv "$file.tmp" "$file"
done

# Neues EPUB erzeugen
cd "$WORKDIR" || exit
zip -qr "$OUTPUT" .
cd ..

echo "Fertig! Neues EPUB: $OUTPUT"
Inhalt von modernize_gui.py:
Code:
import tkinter as tk
from tkinter import filedialog, messagebox
import subprocess
import os
import threading

# Pfad zu deinem Shell-Skript
EPUB_SCRIPT = "/home/juergen/modernize_epub.sh"


def run_modernization(epub_path, output_path, log_widget, progress_var, button):
    try:
        progress_var.set(10)
        log_widget.insert(tk.END, f"Starte Modernisierung...\n")
        log_widget.insert(tk.END, f"Eingabe: {epub_path}\n")
        log_widget.insert(tk.END, f"Ausgabe: {output_path}\n\n")
        log_widget.update()

        button.config(state=tk.DISABLED)

        process = subprocess.Popen(
            [EPUB_SCRIPT, epub_path, output_path],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True
        )

        for line in process.stdout:
            log_widget.insert(tk.END, line)
            log_widget.see(tk.END)
            log_widget.update()

        stderr_output = process.stderr.read()
        if stderr_output:
            log_widget.insert(tk.END, "\n[Fehlerausgabe]\n")
            log_widget.insert(tk.END, stderr_output + "\n")

        process.wait()

        if process.returncode == 0:
            progress_var.set(100)
            log_widget.insert(tk.END, "\nFertig! Neues EPUB wurde erzeugt.\n")
            log_widget.see(tk.END)
            messagebox.showinfo("Erfolg", f"Modernisiertes EPUB:\n{output_path}")
        else:
            progress_var.set(0)
            log_widget.insert(tk.END, "\nFehler bei der Modernisierung.\n")
            log_widget.see(tk.END)
            messagebox.showerror(
                "Fehler",
                "Es ist ein Fehler beim Erzeugen des EPUB aufgetreten.\nDetails siehe Log."
            )

    except Exception as e:
        progress_var.set(0)
        log_widget.insert(tk.END, f"\nAusnahmefehler: {e}\n")
        log_widget.see(tk.END)
        messagebox.showerror("Fehler", str(e))
    finally:
        button.config(state=tk.NORMAL)


def start_modernization(epub_path_var, outdir_var, log_widget, progress_var, button):
    epub_path = epub_path_var.get().strip()
    outdir = outdir_var.get().strip()

    if not epub_path:
        messagebox.showwarning("Fehlende Eingabe", "Bitte ein EPUB auswählen.")
        return

    if not os.path.isfile(epub_path):
        messagebox.showerror("Fehler", "Die ausgewählte EPUB-Datei existiert nicht.")
        return

    if not outdir:
        outdir = os.path.dirname(epub_path) or os.getcwd()

    if not os.path.isdir(outdir):
        messagebox.showerror("Fehler", "Der gewählte Zielordner existiert nicht.")
        return

    base_name = os.path.splitext(os.path.basename(epub_path))[0]
    output_path = os.path.join(outdir, base_name + "_modern.epub")

    log_widget.delete("1.0", tk.END)
    progress_var.set(5)
    log_widget.insert(tk.END, "Vorbereitung...\n")
    log_widget.update()

    thread = threading.Thread(
        target=run_modernization,
        args=(epub_path, output_path, log_widget, progress_var, button),
        daemon=True
    )
    thread.start()


def choose_file(epub_path_var):
    filename = filedialog.askopenfilename(
        title="EPUB auswählen",
        filetypes=[("EPUB-Dateien", "*.epub")]
    )
    if filename:
        epub_path_var.set(filename)


def choose_outdir(outdir_var):
    dirname = filedialog.askdirectory(title="Zielordner auswählen")
    if dirname:
        outdir_var.set(dirname)


def main():
    print("main() wurde gestartet")

    root = tk.Tk()
    root.title("EPUB Rechtschreibreform – GUI")
    root.geometry("700x450")

    # --- Fenster zuverlässig schließen ---
    def on_close():
        root.quit()
        root.destroy()

    root.protocol("WM_DELETE_WINDOW", on_close)
    # -------------------------------------

    epub_path_var = tk.StringVar()
    outdir_var = tk.StringVar()

    frame_in = tk.Frame(root)
    frame_in.pack(pady=10, fill=tk.X, padx=10)

    tk.Label(frame_in, text="EPUB-Datei:").pack(side=tk.LEFT)
    tk.Entry(frame_in, textvariable=epub_path_var, width=50).pack(side=tk.LEFT, padx=5)
    tk.Button(frame_in, text="Durchsuchen",
              command=lambda: choose_file(epub_path_var)).pack(side=tk.LEFT)

    frame_out = tk.Frame(root)
    frame_out.pack(pady=5, fill=tk.X, padx=10)

    tk.Label(frame_out, text="Zielordner:").pack(side=tk.LEFT)
    tk.Entry(frame_out, textvariable=outdir_var, width=50).pack(side=tk.LEFT, padx=5)
    tk.Button(frame_out, text="Wählen",
              command=lambda: choose_outdir(outdir_var)).pack(side=tk.LEFT)

    progress_var = tk.IntVar(value=0)
    frame_prog = tk.Frame(root)
    frame_prog.pack(pady=5, fill=tk.X, padx=10)

    tk.Label(frame_prog, text="Fortschritt:").pack(side=tk.LEFT)
    progress_bar = tk.Scale(
        frame_prog,
        from_=0,
        to=100,
        orient=tk.HORIZONTAL,
        variable=progress_var,
        length=400,
        showvalue=True,
        state=tk.DISABLED
    )
    progress_bar.pack(side=tk.LEFT, padx=5)

    log_widget = tk.Text(root, height=15, width=80)
    log_widget.pack(pady=10, padx=10, fill=tk.BOTH, expand=True)

    start_button = tk.Button(
        root,
        text="Modernisierung starten",
        bg="#4CAF50",
        fg="white",
        command=lambda: start_modernization(
            epub_path_var, outdir_var, log_widget, progress_var, start_button
        )
    )
    start_button.pack(pady=5)

    root.mainloop()


if __name__ == "__main__":
    main()
1. Voraussetzungen
------------------
• Linux-System (Ubuntu empfohlen)
• Python 3 (systemweit, keine venv)
• Tkinter installiert
• zip / unzip installiert
• modernize_epub.sh ausführbar:
Code:
chmod +x modernize_epub.sh

2. GUI starten
--------------
WICHTIG: Nicht in einer venv starten.

deactivate (falls nötig)

Befehl zu Starten der Rechtschreib-Modernisierung aus dem Terminal:
Code:
python3 /home/xxxxxxx/modernize_gui.py

3. GUI-Funktion
---------------
• EPUB-Datei auswählen
• Zielordner wählen
• Modernisierung starten
• Log-Ausgabe + Fortschrittsanzeige
• Ergebnis: <name>_modern.epub

4. Erweiterungsmöglichkeiten
----------------------------
• Abbrechen-Button
• Vorher/Nachher-Vorschau
• Drag & Drop
• Dark Mode
• Regel-Editor
• Wörterbuch-Autolernen

Hört sich kompliziert an, aber selbst ich als kompletter Laie habe es hinbekommen. Bei Schwierigkeiten kann man die KI befragen.

Viel Erfolg!

Last edited by Leonatus; Today at 06:45 AM.
Leonatus is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Other Fiction Hoefer, Edmund: Unter der Fremdherrschaft [German] V.1 1.11.2016 Leonatus ePub Books 0 11-01-2016 04:14 PM
Calibre unter Linux (SUSE 11.2) lahnschnitzer PocketBook 10 02-08-2011 10:12 PM
Emulator unter Linux reader42 PocketBook 2 05-04-2010 12:05 PM
Adobe DRM unter Linux Ubuntu violetta Software 5 01-15-2010 11:48 AM
Adobe DE unter Linux? eibix Software 1 01-06-2010 04:40 PM


All times are GMT -4. The time now is 08:22 AM.


MobileRead.com is a privately owned, operated and funded community.