Per il funzionamento dei programmi seguenti, è necessario installare Python e la libreria tkinterdnd2
py -m pip install tkinterdnd2
# --------- pulizia_gui_candidati(v.2).py --------
import re
import os
import tkinter as tk
from tkinter import messagebox, Listbox, Scrollbar
from tkinterdnd2 import DND_FILES, TkinterDnD
# ---------- PULIZIA ----------
def pulisci_testo_per_speaker(testo, speaker_target):
righe = testo.split('\n')
risultato = []
in_blocco_giusto = False
speaker_target = speaker_target.lower()
for riga in righe:
riga = riga.strip()
if not riga:
continue
# Riconoscere intestazioni tipo "Donald Trump 00:00:00..."
match_speaker = re.match(r'^([A-Z][a-zA-Z.]*\s+[A-Z][a-zA-Z.]+)\s\d{2}:\d{2}:\d{2}', riga)
if match_speaker:
speaker_name = match_speaker.group(1).lower()
# Tolleranza: se contiene la parola "trump" o "harris"
if speaker_target in speaker_name:
in_blocco_giusto = True
else:
in_blocco_giusto = False
continue
# Salta metadati o rifiuti
if re.match(r"^(No Signal|No StressLens|Weak|Note|Audience|Unidentified|[A-Z][a-z]+(?:\s[A-Z][a-z]+)*:)", riga):
continue
# Rimuove [tag tra parentesi quadre]
riga = re.sub(r"\[.*?\]", "", riga).strip()
if re.match(r"^(Strong|Medium|Weak)\s\(\d+\.\d+\)", riga):
continue
if re.match(r"^\d+\s+of\s+\d+\s+paragraphs\s+scored\.$", riga):
continue
if in_blocco_giusto and riga:
risultato.append(riga)
return "\n".join(risultato)
def pulisci_file(file_path, speaker_target, output_dir):
try:
with open(file_path, 'r', encoding='utf-8') as f:
testo = f.read()
pulito = pulisci_testo_per_speaker(testo, speaker_target)
numero = re.search(r'\d+', os.path.basename(file_path))
numero = numero.group() if numero else "000"
output_path = os.path.join(output_dir, f"cleaned_speech{numero}.txt")
with open(output_path, 'w', encoding='utf-8') as out:
out.write(pulito)
return output_path
except Exception as e:
return f"Errore con {os.path.basename(file_path)}: {e}"
# ---------- GUI APP ----------
class SpeechCleanerApp:
def __init__(self, master):
self.master = master
master.title("Speech Cleaner - Trump / Harris")
master.geometry("600x450")
self.label = tk.Label(master, text="Trascina qui i file .txt da pulire", font=("Arial", 14))
self.label.pack(pady=10)
self.listbox = Listbox(master, selectmode=tk.MULTIPLE, width=80)
self.listbox.pack(padx=10, pady=10, expand=True)
scrollbar = Scrollbar(master, orient="vertical")
scrollbar.config(command=self.listbox.yview)
scrollbar.pack(side="right", fill="y")
self.listbox.config(yscrollcommand=scrollbar.set)
# Bottoni pulizia
self.button_trump = tk.Button(master, text="Pulisci Trump", bg="lightblue", command=lambda: self.pulisci_tutti("Trump"))
self.button_trump.pack(pady=5)
self.button_harris = tk.Button(master, text="Pulisci Harris", bg="lightgreen", command=lambda: self.pulisci_tutti("Harris"))
self.button_harris.pack(pady=5)
master.drop_target_register(DND_FILES)
master.dnd_bind('<<Drop>>', self.aggiungi_file)
self.files = []
def aggiungi_file(self, event):
paths = master.tk.splitlist(event.data)
for path in paths:
if path.endswith('.txt') and path not in self.files:
self.files.append(path)
self.listbox.insert(tk.END, os.path.basename(path))
def pulisci_tutti(self, speaker_target):
if not self.files:
messagebox.showwarning("Nessun file", "Trascina almeno un file .txt")
return
output_dir = os.path.join(os.getcwd(), speaker_target)
os.makedirs(output_dir, exist_ok=True)
successi = []
errori = []
for file_path in self.files:
risultato = pulisci_file(file_path, speaker_target, output_dir)
if risultato.startswith("Errore"):
errori.append(risultato)
else:
successi.append(os.path.basename(risultato))
msg = ""
if successi:
msg += f"\nPuliti: {len(successi)} file\n" + "\n".join(successi)
if errori:
msg += f"\nErrori:\n" + "\n".join(errori)
messagebox.showinfo("Operazione completata", msg)
# ---------- AVVIO ----------
if __name__ == "__main__":
master = TkinterDnD.Tk()
app = SpeechCleanerApp(master)
master.mainloop()