-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_hybrid.py
More file actions
197 lines (170 loc) · 7.89 KB
/
Copy pathrun_hybrid.py
File metadata and controls
197 lines (170 loc) · 7.89 KB
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
"""
하이브리드 버전 실행 스크립트: Blocking + 의미 기반 임베딩
"""
from src.data_loader import DataLoader
from src.duplicate_detector_hybrid import DuplicateDetectorHybrid
from src.merger import DataMerger
import pandas as pd
from datetime import datetime
import sys
import time
import io
import os
OUTPUT_DIR = 'output'
os.makedirs(OUTPUT_DIR, exist_ok=True)
class Logger:
"""로그를 파일과 콘솔에 동시에 출력"""
def __init__(self, log_file=None):
if log_file is None:
log_file = os.path.join(OUTPUT_DIR, 'process_log_hybrid.txt')
self.log_file = log_file
self.start_time = time.time()
# UTF-8로 파일 열기 및 인코딩 설정
with open(log_file, 'w', encoding='utf-8', errors='replace') as f:
f.write("="*60 + "\n")
f.write("협력사 중복 탐지 및 병합 프로세스 로그 (하이브리드 버전)\n")
f.write("="*60 + "\n\n")
def log(self, message):
"""로그 메시지 출력"""
elapsed = time.time() - self.start_time
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# 메시지를 안전하게 문자열로 변환
try:
safe_message = str(message)
except Exception:
try:
safe_message = repr(message)
except Exception:
safe_message = "[인코딩 오류: 메시지를 표시할 수 없음]"
# UTF-8로 안전하게 인코딩
try:
safe_message = safe_message.encode('utf-8', errors='replace').decode('utf-8', errors='replace')
except Exception:
safe_message = "[인코딩 오류]"
log_message = f"[{timestamp}] [{elapsed:.1f}초] {safe_message}"
try:
print(log_message)
except UnicodeEncodeError:
# 인코딩 오류 시 안전하게 출력
try:
print(log_message.encode('utf-8', errors='replace').decode('utf-8', errors='replace'))
except:
print(f"[{timestamp}] [{elapsed:.1f}초] [로그 출력 오류]")
try:
with open(self.log_file, 'a', encoding='utf-8', errors='replace') as f:
f.write(log_message + "\n")
f.flush() # 즉시 파일에 쓰기
except Exception:
# 로그 파일 쓰기 실패해도 계속 진행
pass
sys.stdout.flush()
def section(self, title):
"""섹션 헤더 출력"""
self.log("="*60)
self.log(title)
self.log("="*60)
logger = Logger()
print("="*60)
print("협력사 중복 탐지 및 병합 - 하이브리드 버전")
print("="*60)
print("\n핵심 기능:")
print(" [OK] 다국어 중복 탐지: 같은 업체의 다른 언어 표기도 탐지")
print(" [OK] 다중 Blocking 키: 비교 횟수 대폭 감소")
print(" [OK] 의미 기반 임베딩: 정확도 최대화 (92-96%)")
print(" [OK] RapidFuzz: 빠른 문자열 비교")
print("="*60)
logger.section("프로세스 시작")
# 파일 로드 (기준정보: data/ 우선, 없으면 루트)
INPUT_FILE = os.path.join('data', 'bio_vendor.csv') if os.path.exists(os.path.join('data', 'bio_vendor.csv')) else 'bio_vendor.csv'
logger.section("1단계: 파일 로딩")
logger.log(f"파일 로딩 시작: {INPUT_FILE}")
loader = DataLoader(INPUT_FILE)
df = loader.load()
logger.log(f"로드 완료: {len(df):,}행, {len(df.columns)}열")
# 중복 탐지
logger.section("2단계: 하이브리드 중복 탐지")
logger.log("중복 탐지 시작 (유사도 임계값: 0.85)")
logger.log("하이브리드 방식: Blocking + 의미 기반 임베딩")
logger.log("하이브리드 탐지기 초기화 중...")
try:
detector = DuplicateDetectorHybrid(similarity_threshold=0.85, use_embedding=True, logger=logger)
logger.log("초기화 완료")
# 체크포인트 파일 경로 설정 (재개 기능)
checkpoint_file = os.path.join(OUTPUT_DIR, 'checkpoint_ann.pkl')
if os.path.exists(checkpoint_file):
logger.log(f"[체크포인트 발견] {checkpoint_file} 파일이 있습니다. 이전 진행 상황부터 재개합니다.")
candidate_groups, final_groups, match_info = detector.detect_duplicates(
df,
show_progress=True,
save_intermediate_at=10,
candidate_mode='ann',
checkpoint_file=checkpoint_file,
checkpoint_interval=1000, # 1000행마다 체크포인트 저장
output_dir=OUTPUT_DIR
)
except Exception as e:
logger.log(f"[오류] 중복 탐지 중 오류 발생: {e}")
logger.log("로마자화 방식으로 재시도합니다...")
import traceback
logger.log(traceback.format_exc())
# 로마자화 방식으로 폴백 (기존 방식)
from duplicate_detector_final import DuplicateDetectorFinal
detector_final = DuplicateDetectorFinal(similarity_threshold=0.85)
duplicate_groups_old = detector_final.detect_duplicates(df, show_progress=True)
# 기존 방식 호환을 위해 변환
candidate_groups = []
final_groups = duplicate_groups_old
match_info = {} # 기존 방식에서는 match_info 없음
logger.section("중복 탐지 완료")
logger.log(f"STEP1 후보 그룹: {len(candidate_groups)}개")
logger.log(f"STEP2 최종 중복 그룹: {len(final_groups)}개")
if final_groups:
total_duplicates = sum(len(g) for g in final_groups)
logger.log(f"총 중복 행 수: {total_duplicates}개")
logger.log("\n최종 중복 그룹 예시 (처음 5개):")
for i, group in enumerate(final_groups[:5], 1):
logger.log(f"\n 그룹 {i} ({len(group)}개 행):")
for idx in group[:3]:
row = df.iloc[idx]
name = row.get('공급업체명', 'N/A')
code = row.get('공급업체코드', 'N/A')
land = row.get('Land', 'N/A')
city = row.get('CITY1', 'N/A')
logger.log(f" - [{code}] {name} ({land}, {city})")
if len(group) > 3:
logger.log(f" ... 외 {len(group)-3}개")
# 데이터 병합
logger.section("3단계: 데이터 병합")
logger.log("데이터 병합 시작 (2단계 구조)")
merger = DataMerger(merge_strategy='best')
df_merged = merger.merge_duplicates_2step(df, candidate_groups, final_groups, match_info)
logger.log("데이터 병합 완료")
logger.section("4단계: 그룹번호 부여 결과")
logger.log(f"원본 행 수: {len(df):,}")
logger.log(f"결과 행 수: {len(df_merged):,} (모든 행 유지)")
logger.log(f"STEP1 후보 그룹 수: {len(candidate_groups)}개")
logger.log(f"STEP2 최종 중복 그룹 수: {len(final_groups)}개")
logger.log(f"STEP1 총 그룹 수: {df_merged['그룹번호_STEP1'].nunique()}개")
logger.log(f"STEP2 총 그룹 수: {df_merged['그룹번호_STEP2'].nunique()}개")
# _merged_from_indices 컬럼 제거 (사용자 요청)
if '_merged_from_indices' in df_merged.columns:
df_merged = df_merged.drop(columns=['_merged_from_indices'])
logger.log("_merged_from_indices 컬럼 제거됨")
# 결과 저장
logger.section("5단계: 결과 저장")
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
output_file = os.path.join(OUTPUT_DIR, f'bio_vendor_merged_hybrid_{timestamp}.xlsx')
logger.log(f"결과 파일 저장 중: {output_file}")
df_merged.to_excel(output_file, index=False)
logger.log(f"저장 완료: {output_file}")
logger.log(f"그룹번호로 필터링하여 같은 그룹의 업체들을 확인할 수 있습니다.")
else:
logger.log("중복이 발견되지 않았습니다.")
total_time = time.time() - logger.start_time
logger.section("프로세스 완료")
logger.log(f"총 소요 시간: {total_time:.1f}초 ({total_time/60:.1f}분)")
logger.log("모든 작업이 완료되었습니다!")
print("\n" + "="*60)
print("처리 완료!")
print(f"로그 파일: {logger.log_file}")
print("="*60)