499 lines
12 KiB
Python
499 lines
12 KiB
Python
from pathlib import Path
|
|
import json
|
|
|
|
SCRIPT_DIR = Path(__file__).resolve().parent
|
|
ROOT_DIR = SCRIPT_DIR
|
|
|
|
PAIR_IDS = []
|
|
|
|
RUN_AD = True
|
|
RUN_CD = True
|
|
|
|
|
|
AD_GOLD_FILE_NAME = "AD_gold_standard.json"
|
|
AD_LLM_FILE_NAME = "AD_llm_result_vllm.json"
|
|
AD_REPORT_FILE_NAME = "AD_evaluation_report.json"
|
|
|
|
CD_GOLD_FILE_NAME = "CD_gold_standard.json"
|
|
CD_LLM_FILE_NAME = "CD_llm_result_vllm.json"
|
|
CD_REPORT_FILE_NAME = "CD_evaluation_report.json"
|
|
|
|
|
|
AD_COMPANY_FIELDS = [
|
|
"name",
|
|
"legal_form",
|
|
"register_court",
|
|
"register_number",
|
|
"city",
|
|
"business_purpose",
|
|
"active",
|
|
]
|
|
|
|
AD_ROLE_FIELDS = [
|
|
"name",
|
|
"title",
|
|
"birth_date",
|
|
"city",
|
|
"role",
|
|
]
|
|
|
|
CD_ROLE_FIELDS = [
|
|
"name",
|
|
"title",
|
|
"birth_date",
|
|
"city",
|
|
"role",
|
|
"start_date",
|
|
"end_date",
|
|
"status",
|
|
"initial_marker",
|
|
]
|
|
|
|
|
|
def get_pair_ids():
|
|
if PAIR_IDS:
|
|
return PAIR_IDS
|
|
|
|
pair_ids = []
|
|
|
|
for item in ROOT_DIR.iterdir():
|
|
ad_pdf = item / "AD.pdf"
|
|
cd_pdf = item / "CD.pdf"
|
|
|
|
if item.is_dir() and (ad_pdf.exists() or cd_pdf.exists()):
|
|
pair_ids.append(item.name)
|
|
|
|
return sorted(pair_ids)
|
|
|
|
|
|
def load_json(path):
|
|
with path.open("r", encoding="utf-8") as file:
|
|
return json.load(file)
|
|
|
|
|
|
def normalize_text(value):
|
|
if value is None:
|
|
return None
|
|
|
|
if isinstance(value, bool):
|
|
return value
|
|
|
|
text = str(value).strip()
|
|
text = " ".join(text.split())
|
|
|
|
return text
|
|
|
|
|
|
def safe_divide(numerator, denominator):
|
|
if denominator == 0:
|
|
return None
|
|
|
|
return numerator / denominator
|
|
|
|
|
|
def calculate_f1(precision, recall):
|
|
if precision is None or recall is None:
|
|
return None
|
|
|
|
if precision + recall == 0:
|
|
return 0
|
|
|
|
return 2 * precision * recall / (precision + recall)
|
|
|
|
|
|
def add_problem(problem_pair_ids, category, pair_id):
|
|
problem_pair_ids[category].append(pair_id)
|
|
|
|
|
|
def make_role_key(role, use_start_date):
|
|
birth_date = normalize_text(role.get("birth_date"))
|
|
role_label = normalize_text(role.get("role"))
|
|
|
|
if birth_date:
|
|
key_parts = ["birth_date", birth_date, role_label]
|
|
else:
|
|
name = normalize_text(role.get("name"))
|
|
key_parts = ["name", name, role_label]
|
|
|
|
if use_start_date:
|
|
start_date = normalize_text(role.get("start_date"))
|
|
key_parts.append(start_date)
|
|
|
|
return tuple(key_parts)
|
|
|
|
|
|
def find_matching_role(gold_role, llm_roles, used_llm_indexes, use_start_date):
|
|
gold_key = make_role_key(gold_role, use_start_date)
|
|
|
|
for index, llm_role in enumerate(llm_roles):
|
|
if index in used_llm_indexes:
|
|
continue
|
|
|
|
llm_key = make_role_key(llm_role, use_start_date)
|
|
|
|
if gold_key == llm_key:
|
|
return index
|
|
|
|
return None
|
|
|
|
|
|
def match_roles(gold_roles, llm_roles, use_start_date):
|
|
matches = []
|
|
missing_roles = []
|
|
used_llm_indexes = set()
|
|
|
|
for gold_role in gold_roles:
|
|
llm_index = find_matching_role(
|
|
gold_role,
|
|
llm_roles,
|
|
used_llm_indexes,
|
|
use_start_date,
|
|
)
|
|
|
|
if llm_index is None:
|
|
missing_roles.append(gold_role)
|
|
else:
|
|
matches.append((gold_role, llm_roles[llm_index]))
|
|
used_llm_indexes.add(llm_index)
|
|
|
|
extra_roles = []
|
|
|
|
for index, llm_role in enumerate(llm_roles):
|
|
if index not in used_llm_indexes:
|
|
extra_roles.append(llm_role)
|
|
|
|
return matches, missing_roles, extra_roles
|
|
|
|
|
|
def compare_fields(matches, fields):
|
|
correct = 0
|
|
total = 0
|
|
has_error = False
|
|
|
|
by_field = {}
|
|
|
|
for field in fields:
|
|
by_field[field] = {
|
|
"correct": 0,
|
|
"total": 0,
|
|
}
|
|
|
|
for gold_item, llm_item in matches:
|
|
for field in fields:
|
|
gold_value = normalize_text(gold_item.get(field))
|
|
llm_value = normalize_text(llm_item.get(field))
|
|
|
|
total += 1
|
|
by_field[field]["total"] += 1
|
|
|
|
if gold_value == llm_value:
|
|
correct += 1
|
|
by_field[field]["correct"] += 1
|
|
else:
|
|
has_error = True
|
|
|
|
return correct, total, by_field, has_error
|
|
|
|
|
|
def evaluate_ad_pair(pair_id, problem_pair_ids):
|
|
pair_dir = ROOT_DIR / pair_id
|
|
gold_path = pair_dir / AD_GOLD_FILE_NAME
|
|
llm_path = pair_dir / AD_LLM_FILE_NAME
|
|
|
|
if not gold_path.exists() or not llm_path.exists():
|
|
add_problem(problem_pair_ids, "missing_files", pair_id)
|
|
return None
|
|
|
|
try:
|
|
gold_data = load_json(gold_path)
|
|
llm_data = load_json(llm_path)
|
|
except json.JSONDecodeError:
|
|
add_problem(problem_pair_ids, "invalid_json", pair_id)
|
|
return None
|
|
|
|
company_pairs = [
|
|
(
|
|
gold_data.get("company", {}),
|
|
llm_data.get("company", {}),
|
|
)
|
|
]
|
|
|
|
company_correct, company_total, company_by_field, company_has_error = (
|
|
compare_fields(
|
|
company_pairs,
|
|
AD_COMPANY_FIELDS,
|
|
)
|
|
)
|
|
|
|
if company_has_error:
|
|
add_problem(problem_pair_ids, "company_mismatch", pair_id)
|
|
|
|
gold_roles = gold_data.get("roles", [])
|
|
llm_roles = llm_data.get("roles", [])
|
|
|
|
matches, missing_roles, extra_roles = match_roles(
|
|
gold_roles,
|
|
llm_roles,
|
|
use_start_date=False,
|
|
)
|
|
|
|
if missing_roles or extra_roles:
|
|
add_problem(problem_pair_ids, "role_matching_mismatch", pair_id)
|
|
|
|
role_correct, role_total, role_by_field, role_has_error = compare_fields(
|
|
matches,
|
|
AD_ROLE_FIELDS,
|
|
)
|
|
|
|
if role_has_error:
|
|
add_problem(problem_pair_ids, "role_field_mismatch", pair_id)
|
|
|
|
return {
|
|
"company_correct": company_correct,
|
|
"company_total": company_total,
|
|
"true_positive_roles": len(matches),
|
|
"false_positive_roles": len(extra_roles),
|
|
"false_negative_roles": len(missing_roles),
|
|
"role_fields_correct": role_correct,
|
|
"role_fields_total": role_total,
|
|
"role_fields_by_field": role_by_field,
|
|
}
|
|
|
|
|
|
def evaluate_cd_pair(pair_id, problem_pair_ids):
|
|
pair_dir = ROOT_DIR / pair_id
|
|
gold_path = pair_dir / CD_GOLD_FILE_NAME
|
|
llm_path = pair_dir / CD_LLM_FILE_NAME
|
|
|
|
if not gold_path.exists() or not llm_path.exists():
|
|
add_problem(problem_pair_ids, "missing_files", pair_id)
|
|
return None
|
|
|
|
try:
|
|
gold_data = load_json(gold_path)
|
|
llm_data = load_json(llm_path)
|
|
except json.JSONDecodeError:
|
|
add_problem(problem_pair_ids, "invalid_json", pair_id)
|
|
return None
|
|
|
|
gold_roles = gold_data.get("roles", [])
|
|
llm_roles = llm_data.get("roles", [])
|
|
|
|
matches, missing_roles, extra_roles = match_roles(
|
|
gold_roles,
|
|
llm_roles,
|
|
use_start_date=True,
|
|
)
|
|
|
|
if missing_roles or extra_roles:
|
|
add_problem(problem_pair_ids, "role_matching_mismatch", pair_id)
|
|
|
|
role_correct, role_total, role_by_field, role_has_error = compare_fields(
|
|
matches,
|
|
CD_ROLE_FIELDS,
|
|
)
|
|
|
|
if role_has_error:
|
|
add_problem(problem_pair_ids, "role_field_mismatch", pair_id)
|
|
|
|
return {
|
|
"true_positive_roles": len(matches),
|
|
"false_positive_roles": len(extra_roles),
|
|
"false_negative_roles": len(missing_roles),
|
|
"role_fields_correct": role_correct,
|
|
"role_fields_total": role_total,
|
|
"role_fields_by_field": role_by_field,
|
|
}
|
|
|
|
|
|
def make_problem_pair_ids(evaluation_type):
|
|
if evaluation_type == "AD":
|
|
return {
|
|
"missing_files": [],
|
|
"invalid_json": [],
|
|
"company_mismatch": [],
|
|
"role_matching_mismatch": [],
|
|
"role_field_mismatch": [],
|
|
}
|
|
|
|
return {
|
|
"missing_files": [],
|
|
"invalid_json": [],
|
|
"role_matching_mismatch": [],
|
|
"role_field_mismatch": [],
|
|
}
|
|
|
|
|
|
def make_report(
|
|
pair_ids,
|
|
results,
|
|
problem_pair_ids,
|
|
evaluation_type,
|
|
gold_file,
|
|
llm_file,
|
|
role_fields,
|
|
):
|
|
folders_total = len(pair_ids)
|
|
folders_evaluated = 0
|
|
|
|
company_correct = 0
|
|
company_total = 0
|
|
|
|
true_positive_roles = 0
|
|
false_positive_roles = 0
|
|
false_negative_roles = 0
|
|
|
|
role_fields_correct = 0
|
|
role_fields_total = 0
|
|
|
|
role_fields_by_field = {}
|
|
|
|
for field in role_fields:
|
|
role_fields_by_field[field] = {
|
|
"correct": 0,
|
|
"total": 0,
|
|
}
|
|
|
|
for result in results:
|
|
if result is None:
|
|
continue
|
|
|
|
folders_evaluated += 1
|
|
|
|
if evaluation_type == "AD":
|
|
company_correct += result["company_correct"]
|
|
company_total += result["company_total"]
|
|
|
|
true_positive_roles += result["true_positive_roles"]
|
|
false_positive_roles += result["false_positive_roles"]
|
|
false_negative_roles += result["false_negative_roles"]
|
|
|
|
role_fields_correct += result["role_fields_correct"]
|
|
role_fields_total += result["role_fields_total"]
|
|
|
|
for field in role_fields:
|
|
role_fields_by_field[field]["correct"] += result["role_fields_by_field"][
|
|
field
|
|
]["correct"]
|
|
role_fields_by_field[field]["total"] += result["role_fields_by_field"][
|
|
field
|
|
]["total"]
|
|
|
|
role_precision = safe_divide(
|
|
true_positive_roles,
|
|
true_positive_roles + false_positive_roles,
|
|
)
|
|
|
|
role_recall = safe_divide(
|
|
true_positive_roles,
|
|
true_positive_roles + false_negative_roles,
|
|
)
|
|
|
|
role_field_accuracy_by_field = {}
|
|
|
|
for field in role_fields:
|
|
role_field_accuracy_by_field[field] = safe_divide(
|
|
role_fields_by_field[field]["correct"],
|
|
role_fields_by_field[field]["total"],
|
|
)
|
|
|
|
summary = {
|
|
"folders_total": folders_total,
|
|
"folders_evaluated": folders_evaluated,
|
|
"role_precision": role_precision,
|
|
"role_recall": role_recall,
|
|
"role_f1": calculate_f1(role_precision, role_recall),
|
|
"role_field_accuracy": safe_divide(role_fields_correct, role_fields_total),
|
|
"role_field_accuracy_by_field": role_field_accuracy_by_field,
|
|
}
|
|
|
|
if evaluation_type == "AD":
|
|
summary = {
|
|
"folders_total": folders_total,
|
|
"folders_evaluated": folders_evaluated,
|
|
"company_field_accuracy": safe_divide(company_correct, company_total),
|
|
"role_precision": role_precision,
|
|
"role_recall": role_recall,
|
|
"role_f1": calculate_f1(role_precision, role_recall),
|
|
"role_field_accuracy": safe_divide(role_fields_correct, role_fields_total),
|
|
"role_field_accuracy_by_field": role_field_accuracy_by_field,
|
|
}
|
|
|
|
report = {
|
|
"evaluation": {
|
|
"type": evaluation_type,
|
|
"root_dir": str(ROOT_DIR),
|
|
"gold_file": gold_file,
|
|
"llm_file": llm_file,
|
|
},
|
|
"summary": summary,
|
|
"problem_pair_ids": problem_pair_ids,
|
|
}
|
|
|
|
return report
|
|
|
|
|
|
def save_report(report, output_path):
|
|
with output_path.open("w", encoding="utf-8", newline="\n") as file:
|
|
json.dump(report, file, ensure_ascii=False, indent=2)
|
|
|
|
print(f"Saved: {output_path}")
|
|
|
|
|
|
def run_ad_evaluation(pair_ids):
|
|
problem_pair_ids = make_problem_pair_ids("AD")
|
|
results = []
|
|
|
|
for pair_id in pair_ids:
|
|
result = evaluate_ad_pair(pair_id, problem_pair_ids)
|
|
results.append(result)
|
|
|
|
report = make_report(
|
|
pair_ids,
|
|
results,
|
|
problem_pair_ids,
|
|
"AD",
|
|
AD_GOLD_FILE_NAME,
|
|
AD_LLM_FILE_NAME,
|
|
AD_ROLE_FIELDS,
|
|
)
|
|
|
|
save_report(report, ROOT_DIR / AD_REPORT_FILE_NAME)
|
|
|
|
|
|
def run_cd_evaluation(pair_ids):
|
|
problem_pair_ids = make_problem_pair_ids("CD")
|
|
results = []
|
|
|
|
for pair_id in pair_ids:
|
|
result = evaluate_cd_pair(pair_id, problem_pair_ids)
|
|
results.append(result)
|
|
|
|
report = make_report(
|
|
pair_ids,
|
|
results,
|
|
problem_pair_ids,
|
|
"CD",
|
|
CD_GOLD_FILE_NAME,
|
|
CD_LLM_FILE_NAME,
|
|
CD_ROLE_FIELDS,
|
|
)
|
|
|
|
save_report(report, ROOT_DIR / CD_REPORT_FILE_NAME)
|
|
|
|
|
|
def main():
|
|
pair_ids = get_pair_ids()
|
|
|
|
if RUN_AD:
|
|
run_ad_evaluation(pair_ids)
|
|
|
|
if RUN_CD:
|
|
run_cd_evaluation(pair_ids)
|
|
|
|
print("Evaluation finished")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|