Update app.py
Browse files
app.py
CHANGED
|
@@ -425,33 +425,52 @@ def execute_unified_search(base_std, base_ver, base_cat, comp_std, comp_ver, com
|
|
| 425 |
|
| 426 |
def clean_key_val(v):
|
| 427 |
s = str(v).strip()
|
| 428 |
-
if s.endswith('.0'): s = s[:-2]
|
| 429 |
return s.replace(" ", "")
|
| 430 |
|
| 431 |
df_base['merge_key'] = df_base[real_anchors_b].apply(lambda row: '-'.join([clean_key_val(x) for x in row]), axis=1)
|
| 432 |
df_comp['merge_key'] = df_comp[real_anchors_c].apply(lambda row: '-'.join([clean_key_val(x) for x in row]), axis=1)
|
| 433 |
|
| 434 |
conn_map = sqlite3.connect(os.path.join(UPLOAD_DIR, "mapping.db"))
|
| 435 |
-
map_cols = pd.read_sql("PRAGMA table_info(Mapping_table)", conn_map)['name'].tolist()
|
| 436 |
-
map_cols_lower = [c.lower() for c in map_cols]
|
| 437 |
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
|
| 442 |
-
|
| 443 |
-
df_fw =
|
|
|
|
|
|
|
| 444 |
|
| 445 |
-
|
| 446 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
else:
|
| 448 |
-
|
| 449 |
-
df_fw = pd.read_sql(q, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 450 |
-
q_rv = "SELECT Comp_section AS Base_section, Base_section AS Comp_section FROM Mapping_table WHERE TRIM(Comp_std)=? AND TRIM(Comp_ver)=? AND TRIM(Base_std)=? AND TRIM(Base_ver)=?"
|
| 451 |
-
df_rv = pd.read_sql(q_rv, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 452 |
-
conn_map.close()
|
| 453 |
|
| 454 |
df_mapping = pd.concat([df_fw, df_rv], ignore_index=True)
|
|
|
|
| 455 |
if not df_mapping.empty:
|
| 456 |
df_mapping['Base_section'] = df_mapping['Base_section'].astype(str).str.replace('\n', ',').str.split(',')
|
| 457 |
df_mapping['Comp_section'] = df_mapping['Comp_section'].astype(str).str.replace('\n', ',').str.split(',')
|
|
@@ -463,7 +482,8 @@ def execute_unified_search(base_std, base_ver, base_cat, comp_std, comp_ver, com
|
|
| 463 |
else:
|
| 464 |
df_mapping = pd.DataFrame(columns=['Base_section', 'Comp_section'])
|
| 465 |
|
| 466 |
-
|
|
|
|
| 467 |
implicit = pd.DataFrame({'Base_section': list(set(df_base['merge_key']) & set(df_comp['merge_key'])),
|
| 468 |
'Comp_section': list(set(df_base['merge_key']) & set(df_comp['merge_key']))})
|
| 469 |
bridge = pd.concat([df_mapping, implicit], ignore_index=True).drop_duplicates()
|
|
|
|
| 425 |
|
| 426 |
def clean_key_val(v):
|
| 427 |
s = str(v).strip()
|
| 428 |
+
if s.endswith('.0'): s = s[:-2]
|
| 429 |
return s.replace(" ", "")
|
| 430 |
|
| 431 |
df_base['merge_key'] = df_base[real_anchors_b].apply(lambda row: '-'.join([clean_key_val(x) for x in row]), axis=1)
|
| 432 |
df_comp['merge_key'] = df_comp[real_anchors_c].apply(lambda row: '-'.join([clean_key_val(x) for x in row]), axis=1)
|
| 433 |
|
| 434 |
conn_map = sqlite3.connect(os.path.join(UPLOAD_DIR, "mapping.db"))
|
|
|
|
|
|
|
| 435 |
|
| 436 |
+
# 1. ์์ ์ฝ๋๋ก ๋กค๋ฐฑ: ์ผ๋จ ๋งคํ ๋ฐ์ดํฐ๋ฅผ ์ ๋ถ ๋ค ๊ฐ์ ธ์ต๋๋ค. (DB ์กฐ๊ฑด ์ค๋ฅ ์์ฒ ์ฐจ๋จ)
|
| 437 |
+
q_fw = "SELECT * FROM Mapping_table WHERE TRIM(Base_std)=? AND TRIM(Base_ver)=? AND TRIM(Comp_std)=? AND TRIM(Comp_ver)=?"
|
| 438 |
+
df_fw = pd.read_sql(q_fw, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 439 |
+
|
| 440 |
+
q_rv = "SELECT * FROM Mapping_table WHERE TRIM(Comp_std)=? AND TRIM(Comp_ver)=? AND TRIM(Base_std)=? AND TRIM(Base_ver)=?"
|
| 441 |
+
df_rv = pd.read_sql(q_rv, conn_map, params=[base_std.strip(), base_ver.strip(), comp_std.strip(), comp_ver.strip()])
|
| 442 |
+
conn_map.close()
|
| 443 |
+
|
| 444 |
+
# 2. ํ์ด์ฌ(Pandas)์์ Type์ ์ ํํ๊ณ ์์ ํ๊ฒ ํํฐ๋งํฉ๋๋ค.
|
| 445 |
+
cols_fw_lower = {c.lower(): c for c in df_fw.columns}
|
| 446 |
+
|
| 447 |
+
if 'base_type' in cols_fw_lower and 'comp_type' in cols_fw_lower:
|
| 448 |
+
b_col = cols_fw_lower['base_type']
|
| 449 |
+
c_col = cols_fw_lower['comp_type']
|
| 450 |
|
| 451 |
+
# ์ ๋ฐฉํฅ ํํฐ
|
| 452 |
+
df_fw[b_col] = df_fw[b_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 453 |
+
df_fw[c_col] = df_fw[c_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 454 |
+
df_fw = df_fw[(df_fw[b_col] == type_b.strip().upper()) & (df_fw[c_col] == type_c.strip().upper())]
|
| 455 |
|
| 456 |
+
# ์ญ๋ฐฉํฅ ํํฐ (DB์ Comp๊ฐ ๋ด Base, DB์ Base๊ฐ ๋ด Comp)
|
| 457 |
+
if not df_rv.empty:
|
| 458 |
+
df_rv[b_col] = df_rv[b_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 459 |
+
df_rv[c_col] = df_rv[c_col].fillna('Main').astype(str).str.strip().str.upper()
|
| 460 |
+
df_rv = df_rv[(df_rv[c_col] == type_b.strip().upper()) & (df_rv[b_col] == type_c.strip().upper())]
|
| 461 |
+
|
| 462 |
+
# 3. ํ์ํ ์ปฌ๋ผ๋ง ์ถ์ถํด์ ๋ณํฉ
|
| 463 |
+
b_sec = cols_fw_lower.get('base_section', 'Base_section')
|
| 464 |
+
c_sec = cols_fw_lower.get('comp_section', 'Comp_section')
|
| 465 |
+
|
| 466 |
+
df_fw = df_fw[[b_sec, c_sec]].rename(columns={b_sec: 'Base_section', c_sec: 'Comp_section'})
|
| 467 |
+
if not df_rv.empty:
|
| 468 |
+
df_rv = df_rv[[b_sec, c_sec]].rename(columns={b_sec: 'Comp_section', c_sec: 'Base_section'})
|
| 469 |
else:
|
| 470 |
+
df_rv = pd.DataFrame(columns=['Base_section', 'Comp_section'])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 471 |
|
| 472 |
df_mapping = pd.concat([df_fw, df_rv], ignore_index=True)
|
| 473 |
+
|
| 474 |
if not df_mapping.empty:
|
| 475 |
df_mapping['Base_section'] = df_mapping['Base_section'].astype(str).str.replace('\n', ',').str.split(',')
|
| 476 |
df_mapping['Comp_section'] = df_mapping['Comp_section'].astype(str).str.replace('\n', ',').str.split(',')
|
|
|
|
| 482 |
else:
|
| 483 |
df_mapping = pd.DataFrame(columns=['Base_section', 'Comp_section'])
|
| 484 |
|
| 485 |
+
# 4. ๋ค๋ฅธ ๋ฒ๊ท/๋ค๋ฅธ ํ
์ด๋ธ์ผ ๋๋ ๊ฐ์ ์๋ ๋งคํ ๊ธ์ง (์ด ์์น์ ์ ์ง)
|
| 486 |
+
if base_std.strip().upper() == comp_std.strip().upper() and type_b.strip().upper() == type_c.strip().upper():
|
| 487 |
implicit = pd.DataFrame({'Base_section': list(set(df_base['merge_key']) & set(df_comp['merge_key'])),
|
| 488 |
'Comp_section': list(set(df_base['merge_key']) & set(df_comp['merge_key']))})
|
| 489 |
bridge = pd.concat([df_mapping, implicit], ignore_index=True).drop_duplicates()
|