bat-6 commited on
Commit
cf51ebc
·
1 Parent(s): 334beca

feat: implement similarity_engine with hybrid scoring and database-backed project comparison logic

Browse files
src/similarity_model/__pycache__/similarity_engine.cpython-313.pyc CHANGED
Binary files a/src/similarity_model/__pycache__/similarity_engine.cpython-313.pyc and b/src/similarity_model/__pycache__/similarity_engine.cpython-313.pyc differ
 
src/similarity_model/similarity_engine.py CHANGED
@@ -291,22 +291,67 @@ def find_similar_projects(
291
  )
292
 
293
  if not exclude_self:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294
  existing_project = df[
295
  df["project_title"].str.lower().str.strip()
296
  ==
297
  title.lower().strip()
298
  ]
299
 
300
- if len(existing_project) > 0:
301
-
302
- logger.info("Exact title match found — returning originality = 0")
303
-
304
- matched_row = existing_project.iloc[0]
305
- stored_features = safe_feature_list(matched_row["features"])
306
-
 
 
 
 
 
 
 
 
 
307
  return pd.DataFrame([{
308
- "project_id": int(matched_row.name),
309
- "project_title": matched_row["project_title"],
310
  "semantic_score": 1.0,
311
  "feature_score": 1.0,
312
  "coverage": 1.0,
@@ -319,7 +364,10 @@ def find_similar_projects(
319
  "unique_query_features": [],
320
  "unique_candidate_features": [],
321
  "query_features_used": stored_features,
322
- "query_clean_text": title
 
 
 
323
  }])
324
 
325
  logger.info("Extracting new features")
 
291
  )
292
 
293
  if not exclude_self:
294
+ # Check database table 'preprocess' directly to see if this project title already exists
295
+ db_match_found = False
296
+ db_project_id = None
297
+ db_features = []
298
+ db_abstract = ""
299
+ db_description = ""
300
+ matched_title = title
301
+
302
+ try:
303
+ import json
304
+ from Data.database.sql_connector import engine
305
+ from sqlalchemy import text
306
+
307
+ with engine.connect() as conn:
308
+ db_row = conn.execute(text("""
309
+ SELECT id, project_title, features, abstract, description
310
+ FROM preprocess
311
+ WHERE LOWER(LTRIM(RTRIM(project_title))) = LOWER(:title)
312
+ """), {"title": title.strip()}).fetchone()
313
+
314
+ if db_row:
315
+ db_match_found = True
316
+ db_project_id, matched_title, features_json, db_abstract, db_description = db_row
317
+ # Parse features
318
+ try:
319
+ db_features = json.loads(features_json) if isinstance(features_json, str) else features_json
320
+ if isinstance(db_features, str):
321
+ db_features = json.loads(db_features)
322
+ except Exception:
323
+ db_features = []
324
+ if not isinstance(db_features, list):
325
+ db_features = []
326
+ except Exception as db_exc:
327
+ logger.warning(f"Direct database check of 'preprocess' table failed: {db_exc}")
328
+
329
+ # Check local/cached metadata dataframe as fallback
330
  existing_project = df[
331
  df["project_title"].str.lower().str.strip()
332
  ==
333
  title.lower().strip()
334
  ]
335
 
336
+ if len(existing_project) > 0 or db_match_found:
337
+ logger.info("Exact title match found in preprocess table — returning originality = 0")
338
+
339
+ if len(existing_project) > 0:
340
+ matched_row = existing_project.iloc[0]
341
+ project_id = int(matched_row.name)
342
+ stored_features = safe_feature_list(matched_row["features"])
343
+ matched_title = matched_row["project_title"]
344
+ matched_abstract = matched_row.get("abstract", "")
345
+ matched_desc = matched_row.get("description", "")
346
+ else:
347
+ project_id = int(db_project_id)
348
+ stored_features = db_features
349
+ matched_abstract = db_abstract
350
+ matched_desc = db_description
351
+
352
  return pd.DataFrame([{
353
+ "project_id": project_id,
354
+ "project_title": matched_title,
355
  "semantic_score": 1.0,
356
  "feature_score": 1.0,
357
  "coverage": 1.0,
 
364
  "unique_query_features": [],
365
  "unique_candidate_features": [],
366
  "query_features_used": stored_features,
367
+ "query_clean_text": title,
368
+ "candidate_features": stored_features,
369
+ "abstract": matched_abstract,
370
+ "description": matched_desc
371
  }])
372
 
373
  logger.info("Extracting new features")