add analyse fréquence mots tagcloud
This commit is contained in:
parent
cffb31c1ef
commit
5acbfd8461
1 changed files with 44 additions and 23 deletions
|
|
@ -15,7 +15,8 @@ import re
|
||||||
import os
|
import os
|
||||||
import csv
|
import csv
|
||||||
import argparse
|
import argparse
|
||||||
|
from pyspellchecker import SpellChecker
|
||||||
|
import language_tool_python
|
||||||
# Vérifier si les modules nécessaires sont disponibles
|
# Vérifier si les modules nécessaires sont disponibles
|
||||||
try:
|
try:
|
||||||
import language_tool_python
|
import language_tool_python
|
||||||
|
|
@ -83,7 +84,6 @@ def clean_chapter_content(content):
|
||||||
content = re.sub(r'\n\s*\n', '\n\n', content)
|
content = re.sub(r'\n\s*\n', '\n\n', content)
|
||||||
|
|
||||||
return content.strip()
|
return content.strip()
|
||||||
|
|
||||||
def load_custom_dictionary(file_path):
|
def load_custom_dictionary(file_path):
|
||||||
"""
|
"""
|
||||||
Charge le dictionnaire personnalisé à partir d'un fichier texte.
|
Charge le dictionnaire personnalisé à partir d'un fichier texte.
|
||||||
|
|
@ -102,7 +102,9 @@ def load_custom_dictionary(file_path):
|
||||||
|
|
||||||
return custom_words
|
return custom_words
|
||||||
|
|
||||||
|
|
||||||
def check_spelling(text, lang='fr', custom_dict_path='dictionnaire_personnalise.txt'):
|
def check_spelling(text, lang='fr', custom_dict_path='dictionnaire_personnalise.txt'):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Vérifie l'orthographe d'un texte et retourne les mots mal orthographiés.
|
Vérifie l'orthographe d'un texte et retourne les mots mal orthographiés.
|
||||||
Utilise un dictionnaire personnalisé pour exclure certains mots de la vérification.
|
Utilise un dictionnaire personnalisé pour exclure certains mots de la vérification.
|
||||||
|
|
@ -111,7 +113,7 @@ def check_spelling(text, lang='fr', custom_dict_path='dictionnaire_personnalise.
|
||||||
if not SPELLCHECKER_AVAILABLE:
|
if not SPELLCHECKER_AVAILABLE:
|
||||||
print("Vérification orthographique désactivée car le module spellchecker n'est pas disponible.")
|
print("Vérification orthographique désactivée car le module spellchecker n'est pas disponible.")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
try:
|
try:
|
||||||
spell = SpellChecker(language=lang)
|
spell = SpellChecker(language=lang)
|
||||||
|
|
||||||
|
|
@ -140,29 +142,29 @@ def check_spelling(text, lang='fr', custom_dict_path='dictionnaire_personnalise.
|
||||||
# Limiter à 5 suggestions maximum
|
# Limiter à 5 suggestions maximum
|
||||||
suggestions_list = list(suggestions) if suggestions is not None else []
|
suggestions_list = list(suggestions) if suggestions is not None else []
|
||||||
suggestions_list = suggestions_list[:5]
|
suggestions_list = suggestions_list[:5]
|
||||||
|
|
||||||
# Trouver toutes les occurrences du mot dans le texte original
|
# Trouver toutes les occurrences du mot dans le texte original
|
||||||
for match in re.finditer(r'\b' + re.escape(word) + r'\b', text, re.IGNORECASE):
|
for match in re.finditer(r'\b' + re.escape(word) + r'\b', text, re.IGNORECASE):
|
||||||
# Extraire le contexte autour du mot
|
# Extraire le contexte autour du mot
|
||||||
word_start = match.start()
|
word_start = match.start()
|
||||||
word_end = match.end()
|
word_end = match.end()
|
||||||
|
|
||||||
# Trouver les limites des lignes contenant le mot
|
# Trouver les limites des lignes contenant le mot
|
||||||
line_start = text.rfind('\n', 0, word_start) + 1 if text.rfind('\n', 0, word_start) >= 0 else 0
|
line_start = text.rfind('\n', 0, word_start) + 1 if text.rfind('\n', 0, word_start) >= 0 else 0
|
||||||
line_end = text.find('\n', word_end) if text.find('\n', word_end) >= 0 else len(text)
|
line_end = text.find('\n', word_end) if text.find('\n', word_end) >= 0 else len(text)
|
||||||
|
|
||||||
# Extraire les lignes précédentes et suivantes pour plus de contexte
|
# Extraire les lignes précédentes et suivantes pour plus de contexte
|
||||||
prev_line_start = text.rfind('\n', 0, line_start - 1) + 1 if text.rfind('\n', 0, line_start - 1) >= 0 else 0
|
prev_line_start = text.rfind('\n', 0, line_start - 1) + 1 if text.rfind('\n', 0, line_start - 1) >= 0 else 0
|
||||||
next_line_end = text.find('\n', line_end + 1) if text.find('\n', line_end + 1) >= 0 else len(text)
|
next_line_end = text.find('\n', line_end + 1) if text.find('\n', line_end + 1) >= 0 else len(text)
|
||||||
|
|
||||||
# Créer le contexte standard et étendu
|
# Créer le contexte standard et étendu
|
||||||
context = text[line_start:line_end]
|
context = text[line_start:line_end]
|
||||||
extended_context = text[prev_line_start:next_line_end]
|
extended_context = text[prev_line_start:next_line_end]
|
||||||
|
|
||||||
# Calculer les offsets pour les contextes
|
# Calculer les offsets pour les contextes
|
||||||
context_offset = word_start - line_start
|
context_offset = word_start - line_start
|
||||||
extended_offset = word_start - prev_line_start
|
extended_offset = word_start - prev_line_start
|
||||||
|
|
||||||
spelling_errors.append({
|
spelling_errors.append({
|
||||||
'word': word,
|
'word': word,
|
||||||
'context': context,
|
'context': context,
|
||||||
|
|
@ -176,6 +178,24 @@ def check_spelling(text, lang='fr', custom_dict_path='dictionnaire_personnalise.
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Erreur lors de la vérification orthographique: {str(e)}")
|
print(f"Erreur lors de la vérification orthographique: {str(e)}")
|
||||||
return []
|
return []
|
||||||
|
spell = SpellChecker(language=lang)
|
||||||
|
|
||||||
|
# Diviser le texte en mots
|
||||||
|
words = re.findall(r'\b\w+\b', text.lower())
|
||||||
|
|
||||||
|
# Trouver les mots mal orthographiés
|
||||||
|
misspelled = spell.unknown(words)
|
||||||
|
|
||||||
|
# Créer un dictionnaire avec les mots mal orthographiés et leurs suggestions
|
||||||
|
spelling_errors = {}
|
||||||
|
for word in misspelled:
|
||||||
|
# Obtenir les suggestions de correction
|
||||||
|
suggestions = spell.candidates(word)
|
||||||
|
# Limiter à 5 suggestions maximum
|
||||||
|
suggestions_list = list(suggestions)[:5]
|
||||||
|
spelling_errors[word] = suggestions_list
|
||||||
|
|
||||||
|
return spelling_errors
|
||||||
|
|
||||||
def check_grammar(text, lang='fr'):
|
def check_grammar(text, lang='fr'):
|
||||||
"""
|
"""
|
||||||
|
|
@ -185,7 +205,7 @@ def check_grammar(text, lang='fr'):
|
||||||
if not LANGUAGE_TOOL_AVAILABLE:
|
if not LANGUAGE_TOOL_AVAILABLE:
|
||||||
print("Vérification grammaticale désactivée car le module language_tool_python n'est pas disponible.")
|
print("Vérification grammaticale désactivée car le module language_tool_python n'est pas disponible.")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Initialiser l'outil de vérification grammaticale
|
# Initialiser l'outil de vérification grammaticale
|
||||||
tool = language_tool_python.LanguageTool(lang)
|
tool = language_tool_python.LanguageTool(lang)
|
||||||
|
|
@ -203,21 +223,21 @@ def check_grammar(text, lang='fr'):
|
||||||
# Extraire plus de contexte autour de l'erreur
|
# Extraire plus de contexte autour de l'erreur
|
||||||
error_start = match.offset
|
error_start = match.offset
|
||||||
error_end = match.offset + match.errorLength
|
error_end = match.offset + match.errorLength
|
||||||
|
|
||||||
# Trouver les limites des lignes contenant l'erreur
|
# Trouver les limites des lignes contenant l'erreur
|
||||||
line_start = text.rfind('\n', 0, error_start) + 1 if text.rfind('\n', 0, error_start) >= 0 else 0
|
line_start = text.rfind('\n', 0, error_start) + 1 if text.rfind('\n', 0, error_start) >= 0 else 0
|
||||||
line_end = text.find('\n', error_end) if text.find('\n', error_end) >= 0 else len(text)
|
line_end = text.find('\n', error_end) if text.find('\n', error_end) >= 0 else len(text)
|
||||||
|
|
||||||
# Extraire les lignes précédentes et suivantes pour plus de contexte
|
# Extraire les lignes précédentes et suivantes pour plus de contexte
|
||||||
prev_line_start = text.rfind('\n', 0, line_start - 1) + 1 if text.rfind('\n', 0, line_start - 1) >= 0 else 0
|
prev_line_start = text.rfind('\n', 0, line_start - 1) + 1 if text.rfind('\n', 0, line_start - 1) >= 0 else 0
|
||||||
next_line_end = text.find('\n', line_end + 1) if text.find('\n', line_end + 1) >= 0 else len(text)
|
next_line_end = text.find('\n', line_end + 1) if text.find('\n', line_end + 1) >= 0 else len(text)
|
||||||
|
|
||||||
# Créer le contexte étendu
|
# Créer le contexte étendu
|
||||||
extended_context = text[prev_line_start:next_line_end]
|
extended_context = text[prev_line_start:next_line_end]
|
||||||
|
|
||||||
# Ajuster les offsets pour le contexte étendu
|
# Ajuster les offsets pour le contexte étendu
|
||||||
extended_offset = error_start - prev_line_start
|
extended_offset = error_start - prev_line_start
|
||||||
|
|
||||||
error = {
|
error = {
|
||||||
'message': match.message,
|
'message': match.message,
|
||||||
'context': match.context,
|
'context': match.context,
|
||||||
|
|
@ -279,19 +299,19 @@ def generate_error_report(chapters, output_path):
|
||||||
'context_offset': error['context_offset'],
|
'context_offset': error['context_offset'],
|
||||||
'extended_offset': error['extended_offset']
|
'extended_offset': error['extended_offset']
|
||||||
})
|
})
|
||||||
|
|
||||||
# Écrire les erreurs regroupées par mot
|
# Écrire les erreurs regroupées par mot
|
||||||
for word, data in errors_by_word.items():
|
for word, data in errors_by_word.items():
|
||||||
suggestions_str = ", ".join(data['suggestions']) if data['suggestions'] else "Aucune suggestion"
|
suggestions_str = ", ".join(data['suggestions']) if data['suggestions'] else "Aucune suggestion"
|
||||||
report_file.write(f"- **{word}**: {suggestions_str}\n")
|
report_file.write(f"- **{word}**: {suggestions_str}\n")
|
||||||
|
|
||||||
# Ajouter les contextes pour chaque occurrence
|
# Ajouter les contextes pour chaque occurrence
|
||||||
for i, occurrence in enumerate(data['occurrences']):
|
for i, occurrence in enumerate(data['occurrences']):
|
||||||
# Mettre en évidence le mot dans le contexte
|
# Mettre en évidence le mot dans le contexte
|
||||||
context = occurrence['context']
|
context = occurrence['context']
|
||||||
offset = occurrence['context_offset']
|
offset = occurrence['context_offset']
|
||||||
highlighted_context = context[:offset] + f"**{word}**" + context[offset+len(word):]
|
highlighted_context = context[:offset] + f"**{word}**" + context[offset+len(word):]
|
||||||
|
|
||||||
# Ajouter le contexte étendu
|
# Ajouter le contexte étendu
|
||||||
extended_context = occurrence['extended_context']
|
extended_context = occurrence['extended_context']
|
||||||
report_file.write(f" - **Occurrence {i+1}**:\n")
|
report_file.write(f" - **Occurrence {i+1}**:\n")
|
||||||
|
|
@ -307,18 +327,19 @@ def generate_error_report(chapters, output_path):
|
||||||
if grammar_errors:
|
if grammar_errors:
|
||||||
for i, error in enumerate(grammar_errors):
|
for i, error in enumerate(grammar_errors):
|
||||||
suggestions_str = ", ".join(error['suggestions'][:5]) if error['suggestions'] else "Aucune suggestion"
|
suggestions_str = ", ".join(error['suggestions'][:5]) if error['suggestions'] else "Aucune suggestion"
|
||||||
|
|
||||||
# Mettre en évidence l'erreur dans le contexte
|
# Mettre en évidence l'erreur dans le contexte
|
||||||
context = error['context'].replace(error['context'][error['offset']:error['offset']+error['length']],
|
context = error['context'].replace(error['context'][error['offset']:error['offset']+error['length']],
|
||||||
f"**{error['context'][error['offset']:error['offset']+error['length']]}**")
|
f"**{error['context'][error['offset']:error['offset']+error['length']]}**")
|
||||||
|
report_file.write(f"- **Erreur**: {error['message']}\n")
|
||||||
|
|
||||||
report_file.write(f"- **Erreur {i+1}**: {error['message']}\n")
|
report_file.write(f"- **Erreur {i+1}**: {error['message']}\n")
|
||||||
report_file.write(f" - **Contexte**: {context}\n")
|
report_file.write(f" - **Contexte**: {context}\n")
|
||||||
|
|
||||||
# Ajouter le contexte étendu
|
# Ajouter le contexte étendu
|
||||||
if 'extended_context' in error:
|
if 'extended_context' in error:
|
||||||
report_file.write(f" - **Contexte étendu**: ```\n{error['extended_context']}\n```\n")
|
report_file.write(f" - **Contexte étendu**: ```\n{error['extended_context']}\n```\n")
|
||||||
|
|
||||||
report_file.write(f" - **Suggestions**: {suggestions_str}\n\n")
|
report_file.write(f" - **Suggestions**: {suggestions_str}\n\n")
|
||||||
else:
|
else:
|
||||||
report_file.write("Aucune erreur grammaticale détectée.\n")
|
report_file.write("Aucune erreur grammaticale détectée.\n")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue