Update app.py
Browse files
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 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 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 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
})
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
else:
|
| 407 |
result_rows.append({
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 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
|
| 464 |
|
| 465 |
# --------------------------
|
| 466 |
-
#
|
| 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()
|