File size: 4,757 Bytes
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4552666
 
 
63bcd5a
 
 
 
 
 
4552666
 
 
63bcd5a
 
 
 
 
 
 
 
 
 
 
4552666
 
 
63bcd5a
 
 
 
 
4552666
63bcd5a
 
 
 
 
4552666
63bcd5a
 
 
 
 
4552666
63bcd5a
 
 
 
 
 
 
 
 
 
 
4552666
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4552666
63bcd5a
 
 
 
 
 
4552666
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4552666
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
276
277
import logging
import re

from typing import List
from functools import lru_cache

from src.similarity_model import (
    compare_two_ideas,
    find_similar_projects
)

from src.recommendation_engine.config import (
    IDEA_DUPLICATE_THRESHOLD,
    FEATURE_DUPLICATE_THRESHOLD
)

logger = logging.getLogger(__name__)

GENERIC_PATTERNS = [
    "dashboard",
    "platform",
    "system",
    "application",
    "website",
    "ai module",
    "analytics module",
    "smart system",
    "management system"
]

def normalize(text: str) -> str:

    text = str(text).lower().strip()

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

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

    return text

def is_generic(text: str) -> bool:

    low = normalize(text)

    for pattern in GENERIC_PATTERNS:

        if pattern in low:
            return True

    return False

def token_overlap_score(a: str, b: str) -> float:

    a_tokens = set(normalize(a).split())

    b_tokens = set(normalize(b).split())

    if not a_tokens or not b_tokens:
        return 0.0

    overlap = len(a_tokens & b_tokens)

    union = len(a_tokens | b_tokens)

    return overlap / union

def is_feature_novel(
    feature: str,
    existing_features: List[str]
) -> bool:

    feature = normalize(feature)

    if not feature:
        return False

    
    
    
    if is_generic(feature):

        logger.debug(f"[GENERIC FEATURE] {feature}")

        return False

    
    
    
    existing_norm = [
        normalize(f)
        for f in existing_features
    ]

    if feature in existing_norm:

        logger.debug(f"[EXACT DUP] {feature}")

        return False

    
    
    
    for old in existing_norm:

        if not old:
            continue

        
        semantic_score = compare_two_ideas(
            feature,
            old
        )

        
        overlap_score = token_overlap_score(
            feature,
            old
        )

        
        final_score = max(
            semantic_score,
            overlap_score
        )

        logger.debug(
            f"[COMPARE] {feature} ~ {old} "
            f"(semantic={semantic_score:.2f}, "
            f"overlap={overlap_score:.2f})"
        )

        
        if final_score >= (
            FEATURE_DUPLICATE_THRESHOLD + 0.08
        ):

            logger.debug(
                f"[FEATURE DUPLICATE] "
                f"{feature} ~ {old}"
            )

            return False

    return True

def filter_duplicate_features(
    generated_features: List[str],
    existing_features: List[str]
) -> List[str]:

    final = []

    seen = set()

    for feat in generated_features:

        clean = str(feat).strip()

        norm = normalize(clean)

        if not clean:
            continue

        if norm in seen:
            continue

        if not is_feature_novel(
            norm,
            existing_features + final
        ):
            continue

        seen.add(norm)

        final.append(clean)

    return final

@lru_cache(maxsize=256)
def _cached_db_check(idea: str) -> float:

    try:

        results = find_similar_projects(
            title=idea,
            description=idea,
            top_k=3
        )

        if (
            hasattr(results, "iloc")
            and len(results) > 0
        ):

            scores = []

            for _, row in results.iterrows():

                score = float(
                    row.get("hybrid_score", 0)
                )

                scores.append(score)

            if scores:

                
                return max(scores)

    except Exception as e:

        logger.warning(f"[DB ERROR] {e}")

    
    return 0.0

def is_idea_novel(idea_title: str) -> bool:

    idea_title = normalize(idea_title)

    if not idea_title:
        return False

    if is_generic(idea_title):

        logger.info(
            f"[GENERIC IDEA REJECTED] "
            f"{idea_title}"
        )

        return False

    score = _cached_db_check(idea_title)

    logger.info(f"[DB CHECK] {idea_title}")
    logger.info(f"[SIMILARITY SCORE] {score:.4f}")

    return score < IDEA_DUPLICATE_THRESHOLD

def score_feature_novelty(
    feature: str,
    existing_features: List[str]
) -> float:

    feature = normalize(feature)

    if not existing_features:
        return 1.0

    scores = []

    for old in existing_features:

        old_norm = normalize(old)

        semantic = compare_two_ideas(
            feature,
            old_norm
        )

        overlap = token_overlap_score(
            feature,
            old_norm
        )

        scores.append(
            max(semantic, overlap)
        )

    if not scores:
        return 1.0

    return round(
        1.0 - max(scores),
        4
    )