Files
Brancheneinstufung2/duplicate_checker.py

159 lines
6.4 KiB
Python

import os
import re
import logging
import pandas as pd
import recordlinkage
from rapidfuzz import fuzz
from google_sheet_handler import GoogleSheetHandler
# --- Konfiguration ---
CRM_SHEET_NAME = "CRM_Accounts"
MATCHING_SHEET_NAME = "Matching_Accounts"
SCORE_THRESHOLD = 0.8
WEIGHTS = {
'domain': 0.5,
'name': 0.4,
'city': 0.1,
}
LOG_DIR = '/log'
LOG_FILENAME = 'duplicate_check.log'
# --- Logging Setup ---
if not os.path.exists(LOG_DIR):
try:
os.makedirs(LOG_DIR)
except Exception as e:
print(f"Warnung: Konnte Log-Ordner nicht anlegen: {e}")
log_path = os.path.join(LOG_DIR, LOG_FILENAME)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(levelname)-8s - %(name)s - %(message)s')
# Console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
# File handler
try:
file_handler = logging.FileHandler(log_path, mode='a', encoding='utf-8')
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.info(f"Logging auch in Datei: {log_path}")
except Exception as e:
logger.warning(f"Konnte keine Log-Datei schreiben: {e}")
# --- Hilfsfunktionen ---
def normalize_company_name(name: str) -> str:
s = str(name).casefold()
for src, dst in [('ä','ae'), ('ö','oe'), ('ü','ue'), ('ß','ss')]:
s = s.replace(src, dst)
s = re.sub(r'[^a-z0-9\s]', ' ', s)
stops = ['gmbh','ag','kg','ug','ohg','holding','group','international']
tokens = [t for t in s.split() if t and t not in stops]
return ' '.join(tokens)
def normalize_domain(url: str) -> str:
s = str(url).casefold().strip()
s = re.sub(r'^https?://', '', s)
s = s.split('/')[0]
if s.startswith('www.'):
s = s[4:]
return s
def main():
logger.info("Starte den Duplikats-Check (v2.0 mit Kandidaten-Logging)...")
try:
sheet_handler = GoogleSheetHandler()
logger.info("GoogleSheetHandler initialisiert")
except Exception as e:
logger.critical(f"FEHLER bei Initialisierung des GoogleSheetHandler: {e}")
return
# Daten laden
crm_df = sheet_handler.get_sheet_as_dataframe(CRM_SHEET_NAME)
match_df = sheet_handler.get_sheet_as_dataframe(MATCHING_SHEET_NAME)
if crm_df is None or crm_df.empty or match_df is None or match_df.empty:
logger.critical("CRM- oder Matching-Daten leer. Abbruch.")
return
logger.info(f"{len(crm_df)} CRM-Zeilen, {len(match_df)} Matching-Zeilen geladen")
# Normalisierung
for df, label in [(crm_df, 'CRM'), (match_df, 'Matching')]:
df['norm_name'] = df['CRM Name'].fillna('').apply(normalize_company_name)
df['norm_domain'] = df['CRM Website'].fillna('').apply(normalize_domain)
df['city'] = df['CRM Ort'].fillna('').apply(lambda x: str(x).casefold().strip())
logger.debug(f"{label}-Daten normalisiert: Beispiel: {df.iloc[0][['norm_name','norm_domain','city']].to_dict()}")
# Blocking
indexer = recordlinkage.Index()
indexer.block('norm_domain')
candidate_pairs = indexer.index(crm_df, match_df)
logger.info(f"Blocking abgeschlossen: {len(candidate_pairs)} Kandidatenpaare")
# Compare
compare = recordlinkage.Compare()
compare.exact('norm_domain', 'norm_domain', label='domain')
compare.string('norm_name', 'norm_name', method='jarowinkler', label='name_sim')
compare.exact('city', 'city', label='city')
features = compare.compute(candidate_pairs, crm_df, match_df)
logger.debug(f"Features berechnet: {features.head()}\n...")
# Score
features['score'] = (WEIGHTS['domain']*features['domain'] +
WEIGHTS['name']*features['name_sim'] +
WEIGHTS['city']*features['city'])
logger.info("Scores berechnet")
# Per Match Logging
results = []
crm_df_idx = crm_df.reset_index()
for match_idx, group in features.reset_index().groupby('level_1'):
logger.info(f"--- Prüfe Matching-Zeile {match_idx} ---")
df_block = group.sort_values('score', ascending=False).copy()
# Enrich with CRM fields
df_block['CRM Name'] = df_block['level_0'].map(crm_df_idx.set_index('index')['CRM Name'])
df_block['CRM Website'] = df_block['level_0'].map(crm_df_idx.set_index('index')['CRM Website'])
df_block['CRM Ort'] = df_block['level_0'].map(crm_df_idx.set_index('index')['CRM Ort'])
# Log top candidates
logger.debug("Kandidaten (CRM_Index, Score, Domain, Name_sim, City, CRM Name):")
for _, row in df_block.iterrows():
logger.debug(f" [{int(row['level_0'])}] score={row['score']:.3f} dom={row['domain']} name_sim={row['name_sim']:.3f} city={row['city']} => {row['CRM Name']}")
top = df_block.iloc[0]
crm_idx = top['level_0'] if top['score'] >= SCORE_THRESHOLD else None
if crm_idx is not None:
logger.info(f" --> Match: CRM-Index {int(crm_idx)} ({top['CRM Name']}) mit Score {top['score']:.2f}")
else:
logger.info(f" --> Kein Match (höchster Score {top['score']:.2f})")
results.append((crm_idx, match_idx, top['score']))
# Prepare output
match_df_idx = match_df.reset_index()
output = match_df_idx[['CRM Name','CRM Website','CRM Ort','CRM Land']].copy()
output['Matched CRM Name'] = ''
output['Matched CRM Website'] = ''
output['Matched CRM Ort'] = ''
output['Matched CRM Land'] = ''
output['Score'] = 0.0
for crm_idx, match_idx, score in results:
if crm_idx is not None:
crm_row = crm_df_idx[crm_df_idx['index']==crm_idx].iloc[0]
output.at[match_idx, 'Matched CRM Name'] = crm_row['CRM Name']
output.at[match_idx, 'Matched CRM Website'] = crm_row['CRM Website']
output.at[match_idx, 'Matched CRM Ort'] = crm_row['CRM Ort']
output.at[match_idx, 'Matched CRM Land'] = crm_row['CRM Land']
output.at[match_idx, 'Score'] = round(score,3)
# Write back
data = [output.columns.tolist()] + output.values.tolist()
success = sheet_handler.clear_and_write_data(MATCHING_SHEET_NAME, data)
if success:
logger.info(f"Erfolgreich geschrieben: {len([r for r in results if r[0] is not None])} Matches")
else:
logger.error("Fehler beim Schreiben ins Google Sheet.")
if __name__ == '__main__':
main()