File size: 5,440 Bytes
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# src/recommendation_engine/validator.py

import re
from typing import List


# =====================================================
# Generic / weak patterns
# =====================================================
GENERIC_PATTERNS = [
    "dashboard",
    "login",
    "signup",
    "authentication",
    "admin panel",
    "analytics system",
    "analytics platform",
    "management system",
    "tracking system",
    "monitoring system",
    "ai module",
    "smart system",
    "web platform",
    "mobile app",
    "website",
    "reports page",
    "user management"
]


BAD_STARTS = [
    "here are",
    "below are",
    "these are",
    "the following",
    "project ideas",
    "features include"
]


LOW_VALUE_WORDS = [
    "system",
    "platform",
    "application",
    "website",
    "solution"
]


# =====================================================
# CLEAN TEXT
# =====================================================
def clean_text(text: str) -> str:

    if not text:
        return ""

    text = str(text).strip()

    # remove numbering
    text = re.sub(r"^\d+[\)\.\-\s]+", "", text)

    # remove bullets
    text = re.sub(r"^[\-\*\•\→\▪\s]+", "", text)

    # remove markdown
    text = text.replace("**", "")

    # remove quotes
    text = text.replace('"', "").replace("'", "")

    # remove brackets
    text = re.sub(r"\(.*?\)", "", text)

    # remove long explanations after colon
    if ":" in text and len(text.split()) > 6:
        text = text.split(":")[0]

    # remove assistant prefixes
    text = re.sub(r"^(assistant|bot)\s*[:\-]\s*", "", text, flags=re.I)

    # remove trailing punctuation
    text = re.sub(r"[.,\-:;]+$", "", text)

    # normalize spaces
    text = re.sub(r"\s+", " ", text).strip()

    return text


# =====================================================
# NORMALIZATION
# =====================================================
def normalize_key(text: str) -> str:

    text = text.lower()

    text = re.sub(r"[^a-z0-9\s]", "", text)

    text = re.sub(r"\s+", " ", text).strip()

    return text


# =====================================================
# Generic detector
# =====================================================
def is_generic(text: str) -> bool:

    low = normalize_key(text)

    for pattern in GENERIC_PATTERNS:
        if pattern in low:
            return True

    return False


# =====================================================
# Weak phrase detector
# =====================================================
def is_low_quality(text: str) -> bool:

    low = normalize_key(text)

    words = low.split()

    # too short
    if len(words) < 3:
        return True

    # too long
    if len(words) > 12:
        return True

    # starts badly
    if any(low.startswith(x) for x in BAD_STARTS):
        return True

    # mostly weak words
    weak_count = sum(
        1 for w in words
        if w in LOW_VALUE_WORDS
    )

    if weak_count >= len(words) / 2:
        return True

    return False


# =====================================================
# VALIDATION RULES
# =====================================================
def is_valid_item(text: str) -> bool:

    if not text:
        return False

    # reject generic garbage
    if is_generic(text):
        return False

    # reject low-quality phrases
    if is_low_quality(text):
        return False

    return True


# =====================================================
# FILTER ITEMS
# =====================================================
def filter_items(items: List[str]) -> List[str]:

    final = []

    seen = set()

    for item in items:

        text = clean_text(item)

        if not text:
            continue

        if not is_valid_item(text):
            continue

        key = normalize_key(text)

        # exact duplicate
        if key in seen:
            continue

        # semantic-ish duplicate
        duplicate = False

        for old in seen:

            # overlap similarity
            overlap = set(key.split()) & set(old.split())

            if len(overlap) >= max(2, min(len(key.split()), len(old.split())) - 1):
                duplicate = True
                break

        if duplicate:
            continue

        seen.add(key)

        final.append(text)

    return final


# =====================================================
# SMART SPLITTER
# =====================================================
def smart_split(text: str) -> List[str]:

    if not text:
        return []

    text = text.replace("\r", "\n")

    lines = []

    for line in text.split("\n"):

        line = line.strip()

        if not line:
            continue

        # split inline numbering
        parts = re.split(r"\d+[\.\)]\s*", line)

        for p in parts:

            p = p.strip()

            if not p:
                continue

            # split bullets inline
            subparts = re.split(r"\s*[-•▪]\s*", p)

            for sp in subparts:

                sp = sp.strip()

                if sp:
                    lines.append(sp)

    return lines


# =====================================================
# MAIN VALIDATOR
# =====================================================
def validate_generated_list(
    text: str,
    top_k: int = 10
) -> List[str]:

    if not text:
        return []

    raw_items = smart_split(text)

    cleaned = filter_items(raw_items)

    return cleaned[:top_k]