QIDNLF commited on
Commit
a6e231f
ยท
verified ยท
1 Parent(s): 97f2ee5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -114
app.py CHANGED
@@ -333,139 +333,87 @@ def unified_search(bs, bv, bc, cs, cv, cc):
333
  })
334
 
335
  # --------------------------
336
- # ๋น„๊ต
337
  # --------------------------
338
  if bs and bv and bc and cs and cv and cc:
339
  df_base = display_data(bs, bv, bc)
340
  df_comp = display_data(cs, cv, cc)
341
 
342
  if 'section' not in df_base.columns or 'section' not in df_comp.columns:
343
- return pd.DataFrame({"Error": ["section ์—†์Œ"]})
344
-
345
- df_base = df_base.rename(columns={
346
- "section": f"Section_{bv}",
347
- "description": f"Description_{bv}"
348
- })
349
-
350
- df_comp = df_comp.rename(columns={
351
- "section": f"Section_{cv}",
352
- "description": f"Description_{cv}"
353
- })
354
-
355
- base_sec = f"Section_{bv}"
356
- comp_sec = f"Section_{cv}"
357
- base_col = f"Description_{bv}"
358
- comp_col = f"Description_{cv}"
 
 
 
 
 
 
 
 
 
 
 
359
 
360
  result_rows = []
361
- used_comp = set()
362
- threshold = 0.6
363
-
364
- def similarity(a, b):
365
- return difflib.SequenceMatcher(None, a, b).ratio()
366
-
367
- # 1๏ธโƒฃ base ๊ธฐ์ค€ ๋งค์นญ
368
- for _, b_row in df_base.iterrows():
369
- b_sec = b_row[base_sec]
370
- b_desc = str(b_row[base_col])
371
-
372
- best_score = 0
373
- best_j = None
374
 
375
- for j, c_row in df_comp.iterrows():
376
- if j in used_comp:
377
- continue
378
-
379
- c_desc = str(c_row[comp_col])
380
-
381
- if "<img" in b_desc or "<img" in c_desc:
382
- continue
383
-
384
- score = similarity(b_desc, c_desc)
385
-
386
- if score > best_score:
387
- best_score = score
388
- best_j = j
389
-
390
- if best_score >= threshold and best_j is not None:
391
- c_row = df_comp.loc[best_j]
392
-
393
- b_diff, c_diff = highlight_diff(
394
- b_desc, str(c_row[comp_col])
395
- )
396
 
 
 
397
  result_rows.append({
398
- base_sec: b_sec,
399
- base_col: b_diff,
400
- comp_sec: c_row[comp_sec],
401
- comp_col: c_diff
402
  })
403
-
404
- used_comp.add(best_j)
405
-
406
- else:
407
  result_rows.append({
408
- base_sec: b_sec,
409
- base_col: b_desc,
410
- comp_sec: "",
411
- comp_col: ""
412
  })
 
 
 
 
 
 
 
413
 
414
- # 2๏ธโƒฃ ๋‚จ์€ comp ์‚ฝ์ž…
415
- inserts = {}
416
-
417
- for j, c_row in df_comp.iterrows():
418
- if j in used_comp:
419
- continue
420
-
421
- c_desc = str(c_row[comp_col])
422
-
423
- best_pos = None
424
- best_score = 0
425
-
426
- for idx, r in enumerate(result_rows):
427
- b_desc = str(r.get(base_col, ""))
428
-
429
- if not b_desc or "<img" in b_desc or "<img" in c_desc:
430
- continue
431
-
432
- score = similarity(b_desc, c_desc)
433
-
434
- if score > best_score:
435
- best_score = score
436
- best_pos = idx
437
-
438
- new_row = {
439
- base_sec: "",
440
- base_col: "",
441
- comp_sec: c_row[comp_sec],
442
- comp_col: c_desc
443
- }
444
-
445
- if best_pos is not None:
446
- inserts.setdefault(best_pos + 1, []).append(new_row)
447
- else:
448
- inserts.setdefault(len(result_rows), []).append(new_row)
449
-
450
- # 3๏ธโƒฃ insert ์ ์šฉ
451
- final_rows = []
452
-
453
- for idx, row in enumerate(result_rows):
454
- final_rows.append(row)
455
- if idx in inserts:
456
- final_rows.extend(inserts[idx])
457
-
458
- if len(result_rows) in inserts:
459
- final_rows.extend(inserts[len(result_rows)])
460
-
461
- final_df = pd.DataFrame(final_rows)
462
 
463
- return final_df[[base_sec, base_col, comp_sec, comp_col]]
464
 
465
  # --------------------------
466
- # โ— ๋ชจ๋“  ์กฐ๊ฑด ๋ฏธ์ถฉ์กฑ โ†’ ๋ฌด์กฐ๊ฑด return
467
  # --------------------------
468
- return pd.DataFrame({"Info": ["์กฐํšŒ ์กฐ๊ฑด์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค"]})
469
 
470
  except Exception as e:
471
  traceback.print_exc()
 
333
  })
334
 
335
  # --------------------------
336
+ # ๋น„๊ต (Section ๋งค์นญ ๊ธฐ๋ฐ˜์œผ๋กœ ์ „๋ฉด ๊ฐœํŽธ)
337
  # --------------------------
338
  if bs and bv and bc and cs and cv and cc:
339
  df_base = display_data(bs, bv, bc)
340
  df_comp = display_data(cs, cv, cc)
341
 
342
  if 'section' not in df_base.columns or 'section' not in df_comp.columns:
343
+ return pd.DataFrame({"Error": ["ํ•ด๋‹น ๋ฐ์ดํ„ฐ์— 'section' ์ปฌ๋Ÿผ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."]})
344
+
345
+ # ๊ณต๋ฐฑ ์ฐจ์ด๋กœ ์ธํ•œ ๋งค์นญ ์‹คํŒจ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋„์–ด์“ฐ๊ธฐ๋ฅผ ์ œ๊ฑฐํ•œ merge_key ์ƒ์„ฑ
346
+ df_base['merge_key'] = df_base['section'].astype(str).str.replace(" ", "")
347
+ df_comp['merge_key'] = df_comp['section'].astype(str).str.replace(" ", "")
348
+
349
+ # ์ปฌ๋Ÿผ๋ช…์ด ๊ฒน์น˜์ง€ ์•Š๋„๋ก ์ž„์‹œ ๋ณ€๊ฒฝ
350
+ df_b = df_base[['merge_key', 'section', 'description']].rename(
351
+ columns={'section': 'sec_b', 'description': 'desc_b'}
352
+ )
353
+ df_c = df_comp[['merge_key', 'section', 'description']].rename(
354
+ columns={'section': 'sec_c', 'description': 'desc_c'}
355
+ )
356
+
357
+ # Section(merge_key) ๊ธฐ์ค€์œผ๋กœ ๋ณ‘ํ•ฉ (Full Outer Join)
358
+ # ๊ธฐ์ค€, ๋น„๊ต ์–ด๋А ํ•œ ์ชฝ์—๋งŒ ์žˆ์–ด๋„ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณด์กด๋จ
359
+ merged = pd.merge(df_b, df_c, on='merge_key', how='outer')
360
+
361
+ # Section ๋ฒˆํ˜ธ ์ˆœ์„œ๋Œ€๋กœ ์ž์—ฐ ์ •๋ ฌ (1.1, 1.2, 1.10 ๋“ฑ ์ˆซ์ž๋ฅผ ์ธ์‹ํ•˜์—ฌ ์ •๋ ฌ)
362
+ merged['sort_key'] = merged['merge_key'].apply(natural_sort_key)
363
+ merged = merged.sort_values('sort_key').drop(columns=['sort_key'])
364
+
365
+ # ์ตœ์ข… ์ถœ๋ ฅํ•  ์ปฌ๋Ÿผ๋ช… ์„ธํŒ…
366
+ base_sec_col = f"Section_{bv}"
367
+ base_desc_col = f"Description_{bv}"
368
+ comp_sec_col = f"Section_{cv}"
369
+ comp_desc_col = f"Description_{cv}"
370
 
371
  result_rows = []
 
 
 
 
 
 
 
 
 
 
 
 
 
372
 
373
+ for _, row in merged.iterrows():
374
+ # NaN ๊ฐ’์„ ๋นˆ ๋ฌธ์ž์—ด๋กœ ์•ˆ์ „ํ•˜๊ฒŒ ๋ณ€ํ™˜
375
+ sec_b = "" if pd.isna(row.get('sec_b')) else str(row['sec_b'])
376
+ sec_c = "" if pd.isna(row.get('sec_c')) else str(row['sec_c'])
377
+ desc_b = "" if pd.isna(row.get('desc_b')) else str(row['desc_b'])
378
+ desc_c = "" if pd.isna(row.get('desc_c')) else str(row['desc_c'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
379
 
380
+ # 1. ๊ธฐ์ค€์— ์žˆ๊ณ  ๋น„๊ต์— ์—†๋Š” ๊ฒฝ์šฐ
381
+ if desc_b and not desc_c:
382
  result_rows.append({
383
+ base_sec_col: sec_b,
384
+ base_desc_col: desc_b,
385
+ comp_sec_col: "",
386
+ comp_desc_col: ""
387
  })
388
+ # 2. ๊ธฐ์ค€์— ์—†๊ณ  ๋น„๊ต์—๋งŒ ์žˆ๋Š” ๊ฒฝ์šฐ
389
+ elif not desc_b and desc_c:
 
 
390
  result_rows.append({
391
+ base_sec_col: "",
392
+ base_desc_col: "",
393
+ comp_sec_col: sec_c,
394
+ comp_desc_col: desc_c
395
  })
396
+ # 3. ๋‘˜ ๋‹ค ์žˆ๋Š” ๊ฒฝ์šฐ
397
+ elif desc_b and desc_c:
398
+ # ์ด๋ฏธ์ง€ ํƒœ๊ทธ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๊ฑฐ๋‚˜, ํ…์ŠคํŠธ๊ฐ€ ์™„์ „ํžˆ ๋™์ผํ•˜๋ฉด ๋ฌด๊ฑฐ์šด Diff ์ƒ๋žต
399
+ if "<img" in desc_b or "<img" in desc_c or desc_b == desc_c:
400
+ b_final, c_final = desc_b, desc_c
401
+ else:
402
+ b_final, c_final = highlight_diff(desc_b, desc_c)
403
 
404
+ result_rows.append({
405
+ base_sec_col: sec_b,
406
+ base_desc_col: b_final,
407
+ comp_sec_col: sec_c,
408
+ comp_desc_col: c_final
409
+ })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
410
 
411
+ return pd.DataFrame(result_rows)
412
 
413
  # --------------------------
414
+ # ๋ชจ๋“  ์กฐ๊ฑด ๋ฏธ์ถฉ์กฑ (์˜ˆ์™ธ ์ฒ˜๋ฆฌ)
415
  # --------------------------
416
+ return pd.DataFrame({"Info": ["์กฐํšŒ ์กฐ๊ฑด์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์–‘์ชฝ ๋ฒ„์ „์„ ๋ชจ๋‘ ์„ ํƒํ•ด์ฃผ์„ธ์š”."]})
417
 
418
  except Exception as e:
419
  traceback.print_exc()