import re from collections import defaultdict import os regex_chapitre = r'\*\* (.+)' regex_target = r':target_(\d+):' fichier_livre = 'livre.org' # Ouvrir le fichier livre.org with open("livre.org", "r") as livre: content = livre.read() # Définir la fonction pour séparer les mots d'une ligne def split_words(line): return re.split(r'[\s]+', line) # Initialisation des dictionnaires pour stocker les informations des chapitres chapters_word_count = defaultdict(int) chapters_target = {} # Parcours des lignes du fichier # Parcourir chaque ligne du fichier livre.org chapitre = '(chapitre not found)' for ligne in content.strip().split("\n"): # Rechercher le titre du chapitre match_chapitre = re.search(regex_chapitre, ligne) if match_chapitre: chapitre_title = match_chapitre.group(1) # Nettoyer le titre du chapitre chapitre = re.sub(r":title:|:target_\d+:", "", chapitre_title).strip() # Rechercher une cible de mots dans le titre du chapitre match_target = re.search(regex_target, chapitre_title) if match_target: target_words = int(match_target.group(1)) chapters_target[chapitre] = target_words print(chapitre) words = split_words(ligne) chapters_word_count[chapitre] += len(words) def draw_progress_bar(percent: float, target: int) -> str: # Calcul du nombre total de caractères nécessaire pour représenter 100% max_length = len("█") * (target / 100) # Calcul de la longueur de la barre de progression length = int((max_length * percent) // 100) # Création de la chaîne représentant la barre de progression progress_bar = "[" + ("█" * int(length)) + ("_" * (int(max_length) - int(length))) + "]" # Affichage de la barre de progression # print(f"\rProgression : {percent}%, {progress_bar}", end="\r") return progress_bar objectif_mots=500 sum_mots=0 # Afficher le résultat print("Nombre de mots par chapitre : ") count_chapitres=0 def nombre_de_pages(nombre_de_mots): return round(nombre_de_mots / 250) def format_with_spaces(n): return '{:,}'.format(n).replace(',', ' ') def genre_de_livre(nombre_de_mots): if nombre_de_mots < 5000: return "Nouvelle" elif 5000 <= nombre_de_mots < 30000: return "Récit" elif 30000 <= nombre_de_mots < 50000: return "Nouvelles" elif 50000 <= nombre_de_mots < 100000: return "Roman court" elif 100000 <= nombre_de_mots < 200000: return "Roman" elif 200000 <= nombre_de_mots < 500000: return "Roman épais" else: return "Autre" # temps de lecture en minutes def temps_de_lecture(nombre_de_signes): return round(nombre_de_signes / 80 * 60) def temps_de_redaction(nombre_de_signes, mots_par_minute): return round(nombre_de_signes / mots_par_minute * 60) def format_time(seconds): minutes, seconds = divmod(seconds, 60) hours, minutes = divmod(minutes, 60) return "{:02d} heures {:02d} minutes {:02d} secondes.".format(hours, minutes, seconds) for count, value in sorted(chapters_word_count.items(), key=lambda item: item[0]): sum_mots+=value count_chapitres+=1 print(f"{value} mots \t\t \t{count} \t\t{draw_progress_bar(value, objectif_mots)} \t\t ") sec_count = sum_mots*6 print(f"\n Total : \n\t {format_with_spaces(sum_mots)} mots.\n\t {format_with_spaces(sec_count)} signes espaces compris.\n\t {format_with_spaces(count_chapitres)} chapitres.") print(f"Estimation de pages A4: {nombre_de_pages(sum_mots)} ") print(f"Genre de livre: {genre_de_livre(sum_mots)}") print(f"Estimation du temps de lecture: {format_time(temps_de_lecture(sum_mots))}") print(f"Estimation du temps de rédaction à 30 mots/minute: {format_time(temps_de_redaction(sum_mots, 30))}") print(f"Estimation du temps de rédaction à 70 mots/minute: {format_time(temps_de_redaction(sum_mots, 70))}") # Générer un rapport en markdown sur l'atteinte des cibles de mots def generate_target_report(): # Compter les chapitres sans cible chapters_without_target = sum(1 for chapitre in chapters_word_count.keys() if chapitre != '(chapitre not found)' and chapitre not in chapters_target) # Créer le rapport markdown report = "# Rapport d'atteinte des cibles de mots\n\n" # Ajouter un résumé report += f"## Résumé\n\n" report += f"- **Chapitres avec cible définie**: {len(chapters_target)}\n" report += f"- **Chapitres sans cible définie**: {chapters_without_target}\n" report += f"- **Total des chapitres**: {len(chapters_target) + chapters_without_target}\n\n" # Ajouter les détails pour chaque chapitre avec une cible if chapters_target: report += "## Détails par chapitre\n\n" report += "| Chapitre | Mots actuels | Cible | Statut | Progression |\n" report += "|----------|--------------|-------|--------|-------------|\n" for chapitre, target in sorted(chapters_target.items()): word_count = chapters_word_count[chapitre] percentage = (word_count / target) * 100 status = "✅ Atteint" if word_count >= target else "❌ Non atteint" # Create a more visually informative progress bar for markdown bar_length = 20 filled_length = int(bar_length * percentage / 100) bar = '█' * filled_length + '░' * (bar_length - filled_length) progress_bar = f"[{bar}] {percentage:.1f}%" report += f"| {chapitre} | {word_count} | {target} | {status} | {progress_bar} |\n" # Écrire le rapport dans un fichier with open("rapport_cibles_mots.md", "w") as f: f.write(report) print(f"\nRapport d'atteinte des cibles de mots généré: rapport_cibles_mots.md") return report # Générer le rapport generate_target_report()