File size: 5,187 Bytes
6a855ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# speech.py — Analyse du discours : mots parasites, structure, clarté

from .utils import (
    word_count,
    sentence_count,
    avg_words_per_sentence,
    lexical_diversity,
    find_filler_words,
    tokenize_sentences,
    tokenize_words,
)

# ---------------------------------------------------------------------------
# Seuils de clarté
# ---------------------------------------------------------------------------

# Nombre de mots par phrase au-delà duquel la phrase est considérée longue
LONG_SENTENCE_THRESHOLD = 25

# Ratio de mots parasites / total mots au-delà duquel c'est problématique
FILLER_RATIO_WARNING = 0.05


# ---------------------------------------------------------------------------
# Structure du discours
# ---------------------------------------------------------------------------

def detect_structure(text: str) -> dict:
    """
    Évalue la présence d'une introduction, d'un développement et d'une conclusion
    en cherchant des marqueurs discursifs typiques.
    """
    INTRO_MARKERS = {
        "fr": ["tout d'abord", "premièrement", "pour commencer", "je vais",
               "dans cet", "dans cette", "aujourd'hui", "nous allons", "je souhaite"],
        "en": ["first", "firstly", "to begin", "in this", "today", "i will",
               "we will", "i would like", "let me"],
    }
    CONCLUSION_MARKERS = {
        "fr": ["en conclusion", "pour conclure", "en résumé", "ainsi",
               "en définitive", "finalement", "pour terminer", "en somme"],
        "en": ["in conclusion", "to conclude", "in summary", "finally",
               "to sum up", "therefore", "in short", "overall"],
    }

    text_lower = text.lower()
    sentences = tokenize_sentences(text)
    n = len(sentences)

    # Détecte la langue dominante des marqueurs
    fr_intro = sum(1 for m in INTRO_MARKERS["fr"] if m in text_lower)
    en_intro = sum(1 for m in INTRO_MARKERS["en"] if m in text_lower)
    lang_hint = "fr" if fr_intro >= en_intro else "en"

    intro_markers = INTRO_MARKERS[lang_hint]
    conclusion_markers = CONCLUSION_MARKERS[lang_hint]

    has_intro = any(m in text_lower for m in intro_markers)
    has_conclusion = any(m in text_lower for m in conclusion_markers)
    # Le développement est présent si le texte a au moins 3 phrases
    has_body = n >= 3

    score = sum([has_intro, has_body, has_conclusion])  # 0-3

    return {
        "has_intro": has_intro,
        "has_body": has_body,
        "has_conclusion": has_conclusion,
        "structure_score": score,          # /3
        "structure_label": _structure_label(score),
    }


def _structure_label(score: int) -> str:
    if score == 3:
        return "complète"
    if score == 2:
        return "partielle"
    return "insuffisante"


# ---------------------------------------------------------------------------
# Clarté
# ---------------------------------------------------------------------------

def analyze_clarity(text: str, lang: str = "fr") -> dict:
    """
    Mesure la clarté du discours :
    - longueur moyenne des phrases
    - proportion de phrases longues
    - richesse lexicale (TTR)
    - score de clarté global [0-100]
    """
    sentences = tokenize_sentences(text)
    if not sentences:
        return {"avg_words_per_sentence": 0, "long_sentence_ratio": 0.0,
                "lexical_diversity": 0.0, "clarity_score": 0}

    word_counts = [len(tokenize_words(s)) for s in sentences]
    avg_wps = round(sum(word_counts) / len(word_counts), 2)
    long_ratio = round(
        sum(1 for c in word_counts if c > LONG_SENTENCE_THRESHOLD) / len(word_counts), 4
    )
    ttr = lexical_diversity(text)

    # Score clarté : pénalise les phrases trop longues et la faible diversité
    clarity = 100
    clarity -= min(40, long_ratio * 100)          # jusqu'à -40 pour phrases longues
    clarity -= max(0, (0.4 - ttr) * 50)           # jusqu'à -20 pour faible TTR
    clarity = max(0, round(clarity))

    return {
        "avg_words_per_sentence": avg_wps,
        "long_sentence_ratio": long_ratio,
        "lexical_diversity": ttr,
        "clarity_score": clarity,
    }


# ---------------------------------------------------------------------------
# Point d'entrée principal
# ---------------------------------------------------------------------------

def analyze(text: str, lang: str = "fr") -> dict:
    """
    Analyse complète du discours.

    Retourne :
        {
            "word_count": int,
            "sentence_count": int,
            "filler_words": dict[str, int],
            "filler_ratio": float,
            "structure": dict,
            "clarity": dict,
        }
    """
    wc = word_count(text)
    sc = sentence_count(text)
    fillers = find_filler_words(text, lang=lang)
    filler_total = sum(fillers.values())
    filler_ratio = round(filler_total / wc, 4) if wc else 0.0

    return {
        "word_count": wc,
        "sentence_count": sc,
        "filler_words": fillers,
        "filler_ratio": filler_ratio,
        "filler_warning": filler_ratio > FILLER_RATIO_WARNING,
        "structure": detect_structure(text),
        "clarity": analyze_clarity(text, lang=lang),
    }