From 6af9d635de9ed48e1056c8e4f7eed97572c88883 Mon Sep 17 00:00:00 2001 From: Tykayn Date: Fri, 3 Jan 2025 23:19:57 +0100 Subject: [PATCH] up format typo --- __pycache__/format_typo.cpython-311.pyc | Bin 0 -> 4769 bytes config/books_path.txt | 1 + format_typo.py | 67 ++++++++++++++++++------ test_format_typo.py | 43 +++++++++++++++ 4 files changed, 95 insertions(+), 16 deletions(-) create mode 100644 __pycache__/format_typo.cpython-311.pyc create mode 100644 test_format_typo.py diff --git a/__pycache__/format_typo.cpython-311.pyc b/__pycache__/format_typo.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..417db398c861f13d96728bee8d98ebaa452f8d3e GIT binary patch literal 4769 zcmbtXZ%i9U7N51(f5A3}LM|lNEFl#bh#eqsgd?<&1OhE2m!u6i4%Xsbz{LO9U1Qi7 zomKTiT18i1lqivU(OLH)N1FS9PC6+)-Jj&r=<=bJcB-w4EJccRs&tX=1E=()h)?%s z4aN?o;V!c?Z)e|o^XAQ;{k@sr+H6(?X>NROvaJN6e~^#$WL&E}`wlAC5r;S`guF3X_X9FAM`X;;6d>1; zR3Mwq*I~4}4^R@_0?j-58Cuv%ThGzk)-`a(ZR?sia}r75=X#pXuio!0#dEL#-kwG- z>#$r^KbG{lePUzM7x9S=uKIKAe)+tsXcEZORX}-`1acjkX5hU!!IHz2Y%J53db(o0 zOnxE@Z5D4T8ARZ)e)uU-eo6-B(q#>kLNp%B8sf1SFSw{IH761=v+DIsMZ>%&7LA8^ z4<88#^D&9%Jfaj0Two{p{o;Hiz>b3~kw|zrewGhKV_`lbd8VXr$TJ>|Uhq%y5nf=W zsNk813Sn0AOY^a)J2t;+?h3KtagObl_XMJX5S-+NoUAJp4X`1x+r6$}2VcZSASraW z=C5^sth-n9S;L*xy zvd(Iio%QG3O+W3ddGkCh&X`X;?~)C^X2#bdJI3m3eU0aO_Iv&38(p1@toJo{!Za8r zLhPjI(r0x7pVf=;@vLR&-FMCmyfZL-48*2DG$Qelcy6wQQs^*iD;kPW2|U6Vr+}o; zxAwBj6H299IWS7{xp8a{sP;gr|360La9*BBk7kUECTu^T+7INJN~LSDW9clmA6D&$ z3%GTQRZBY_>OL=3>_gZ-q}qp4{ZC5DQ^Nw8uRwm!!iO1(_V=v-E<Y{C|ef#y;mL z_*=lj25uE^wZi{*OA9X`SFr^d@N_}94LiD5_C%L_xhMKXLc_<4KxO@V#ua?@0sEO41WFfW32AAOn_dV;d+3AZIVYL&o){Y^!Gg7}p`P zC~y%hX@&Bv8pzvl)*!*_ym5iz(0k}QoIoy{$XVdFP9S?__%_tCI`@Gr6&6WThS9)r z)zU?R!CXw}miGpt;TS6r$mi!IUIeUP2mN)Oz0gu5-eoDzZ&Hk7sHzSs54k#WCBMwhih4IJcQ47s}IvzRpk zAEs6$=TJsVxp)m-7VSf!2^4li5x;>G5JYT8`L&j-oi~Duuj7giwW0$%4yleqe?9_% z*ZSy?@?HSH#i?&`xQkc2caTLo(CR&Cu6D{{N`>yxL{ljjuQ2ZGtSf+bxjcXQ@P-M1Y=nAP$ICXqT9U zUw+Mm;L;0kkOp)##z(S-gaD}GG6A6hvm4)=eV)fF@r+XInXdC_~*xaF-I}~#V(bIl}^hk!f z;!FE7-ldw~jAh0&y@S}?rkdLnbK7H^Gu?PY#I{=1R=a{I%Nv=Q^DSkX0PY>R%0iwMBQjc3XD&6SMQqrm z8g{{us2w+EmhFy@CqA0^aO(0@s-HC4p$Rks(?{)q=_}-INH=P^ncJ*ov{4l+C{LI& z>fj2>lLjmK%#&(FmtK;;((hFCJD00!)8R}%u4+=Nno=j0J>82xznggEz@8D+GlJ=+ zRNtlJ>FG?vGF`3E)i>T;>{~j1cN)_JDm|dA=M@tIv3{pW&ceT}Y|;OoZZ~~KwVVD6 D`QJL4 literal 0 HcmV?d00001 diff --git a/config/books_path.txt b/config/books_path.txt index 8b13789..d1e5abc 100644 --- a/config/books_path.txt +++ b/config/books_path.txt @@ -1 +1,2 @@ +/home/poule/encrypted/stockage-syncable/www/development/html/book_generator/récits_jeanine diff --git a/format_typo.py b/format_typo.py index ea374bc..7190e38 100644 --- a/format_typo.py +++ b/format_typo.py @@ -9,17 +9,40 @@ import re with open('livre.org', 'r') as f: contenu = f.read() -# Corriger les phrases sans majuscules -contenu_corrige = re.sub(r"^([a-z])", lambda m: m.group(1).upper(), contenu, flags=re.MULTILINE) -nb_maj = sum(1 for m in re.finditer(r"^([a-z])", contenu)) - sum(1 for m in re.finditer(r"^([a-z])", contenu_corrige)) -if nb_maj > 0: - print(f" Ajout de majuscules aux débuts de phrases : {nb_maj} modification(s)") +def corriger_typo(contenu): + # Corriger les majuscules au début des phrases + contenu = re.sub(r'(^|\.\s+|\n\s*)([a-z])', lambda x: x.group(1) + x.group(2).upper(), contenu) + + # Supprimer les sauts de ligne multiples + contenu = re.sub(r'\n{3,}', '\n\n', contenu) + + # Ajouter des espaces autour de la ponctuation + contenu = re.sub(r'([.,;:!?])([^\s])', r'\1 \2', contenu) + + # Corriger les entêtes orgmode (remplacer ": " par ":") + contenu = re.sub(r"^(#\+[A-Z_]+): ", r"\1:", contenu, flags=re.MULTILINE) + + return contenu + + +def comparer_textes(texte_original, texte_corrige): + # Compare les deux textes et retourne le nombre de différences + nb_sauts = len(re.findall(r'\n{3,}', texte_original)) - len(re.findall(r'\n{3,}', texte_corrige)) + nb_entetes = len(re.findall(r"^#\+[A-Z_]+: ", texte_original)) - len(re.findall(r"^#\+[A-Z_]+: ", texte_corrige)) + nb_espaces = len(re.findall(r"([.,;:!?])( )?", texte_original)) - len(re.findall(r"([.,;:!?])( )?", texte_corrige)) + nb_espaces += len(re.findall(r"( )([.,;:!?])", texte_original)) - len(re.findall(r"( )([.,;:!?])", texte_corrige)) + nb_maj = sum(1 for m in re.finditer(r"^([a-z])", contenu)) - sum(1 for m in re.finditer(r"^([a-z])", contenu_corrige)) + if nb_maj > 0: + print(f" Ajout de majuscules aux débuts de phrases : {nb_maj} modification(s)") + # Afficher le rapport des modifications + print("\nRapport des modifications :") + print(f"- Sauts de ligne supprimés : {nb_sauts}") + print(f"- Entêtes corrigées : {nb_entetes}") + print(f"- Espacements corrigés : {nb_espaces}") + print(f"Total des modifications : {nb_sauts + nb_entetes + nb_espaces}\n") + + return nb_maj, nb_sauts, nb_entetes, nb_espaces -# Corriger les sauts de ligne consécutifs -contenu_corrige = re.sub(r'\n{3,}', '\n\n', contenu_corrige) -nb_sauts = len(re.findall(r'\n{3,}', contenu)) - len(re.findall(r'\n{3,}', contenu_corrige)) -if nb_sauts > 0: - print(f" Suppression des sauts de ligne consécutifs : {nb_sauts} modification(s)") # Corriger les espaces manquants autour de certaines ponctuations contenu_corrige = re.sub(r"([.,;:!?])( )?", r"\1 ", contenu_corrige) @@ -36,10 +59,22 @@ nb_espaces = len(re.findall(r"([.,;:!?])( )?", contenu)) - len(re.findall(r"([., if nb_espaces > 0: print(f" Ajout d'espaces autour de certaines ponctuations : {nb_espaces} modification(s)") -# Sauvegarder le fichier livre.org corrigé -with open('livre.org', 'w') as f: - f.write(contenu_corrige) -# Faire un rapport des modifications apportées -if nb_maj == 0 and nb_sauts == 0 and nb_espaces == 0: - print("🎉 Pas de modifications à apporter !") +def sauvegarder_si_modifie(contenu_corrige, nb_maj, nb_sauts, nb_espaces): + # Faire un rapport des modifications apportées + if nb_maj == 0 and nb_sauts == 0 and nb_espaces == 0: + print("🎉 Pas de modifications à apporter !") + # Ne sauvegarder que s'il y a des modifications + if nb_maj > 0 or nb_sauts > 0 or nb_espaces > 0: + print("💾 Sauvegarde des modifications...") + with open('livre.org', 'w') as f: + f.write(contenu_corrige) + else: + print("Aucune modification à sauvegarder") + + +if __name__ == "__main__": + contenu_corrige = corriger_typo(contenu) + nb_maj, nb_sauts, nb_entetes, nb_espaces = comparer_textes(contenu, contenu_corrige) + sauvegarder_si_modifie(contenu_corrige, nb_maj, nb_sauts, nb_espaces) + diff --git a/test_format_typo.py b/test_format_typo.py new file mode 100644 index 0000000..e9bb991 --- /dev/null +++ b/test_format_typo.py @@ -0,0 +1,43 @@ +import unittest +import re + +# Supposons que format_typo.py soit dans le même répertoire +from format_typo import corriger_typo + +def test_correction_entetes_org(self): + texte_avec_entetes = """* chapitre 1 :titre: +* chapitre 2 : titre :""" + texte_corrige = corriger_typo(texte_avec_entetes) + self.assertEqual(texte_corrige, """* chapitre 1 :titre: +* chapitre 2 :titre:""") + + +class TestFormatTypo(unittest.TestCase): + def setUp(self): + # Texte Org-mode fictif pour les tests + self.texte_org = """ + * chapitre 1 + ceci est un texte sans majuscule au début de la phrase. + + * chapitre 2 + il y a trop de sauts de ligne ici. + + + * chapitre 3 + il manque des espaces autour de la ponctuation:voici un exemple. + """ + + def test_correction_majuscules(self): + texte_corrige = corriger_typo(self.texte_org) + self.assertIn("Ceci est un texte sans majuscule", texte_corrige) + + def test_correction_sauts_de_ligne(self): + texte_corrige = corriger_typo(self.texte_org) + self.assertNotIn("\n\n\n", texte_corrige) + + def test_correction_espaces_ponctuation(self): + texte_corrige = corriger_typo(self.texte_org) + self.assertIn(": voici un exemple.", texte_corrige) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file