| from __future__ import annotations |
|
|
| from collections import Counter |
| from typing import Any |
|
|
| RELATION_ONTOLOGY = { |
| "IS_A", |
| "PART_OF", |
| "LOCATED_IN", |
| "HAS_ROLE", |
| "HAS_DATE", |
| "HAS_QUANTITY", |
| "MENTIONS", |
| "ENTAILS", |
| "CONTRADICTS", |
| "ASSOCIATED_WITH", |
| "CAUSES", |
| "REPORTS", |
| "ATTRIBUTED_TO", |
| "TREATS", |
| "PREVENTS", |
| "INCREASES_RISK", |
| "DECREASES_RISK", |
| } |
|
|
| FACT_FIELDS = ["fact", "source_doc_id", "source_sent_id", "source_text", "confidence"] |
| TRIPLE_FIELDS = ["head", "relation", "tail", "source_doc_id", "source_sent_id", "source_text", "confidence"] |
| BIOMED_RELATIONS = {"TREATS", "PREVENTS", "INCREASES_RISK", "DECREASES_RISK"} |
| HEALTHVER_DIRECT_CUES = { |
| "TREATS": { |
| "treat", |
| "therapy", |
| "therapeutic", |
| "effective", |
| "efficacy", |
| "inhibit", |
| "inactivat", |
| "antiviral", |
| "against", |
| "used for", |
| }, |
| "PREVENTS": { |
| "prevent", |
| "prevention", |
| "curb", |
| "protect", |
| "reduce", |
| "mitigat", |
| "avoid", |
| "limit spread", |
| "block transmission", |
| }, |
| "INCREASES_RISK": { |
| "increase", |
| "higher", |
| "risk", |
| "more likely", |
| "severe", |
| "worse", |
| "mortality", |
| }, |
| "DECREASES_RISK": { |
| "decrease", |
| "lower", |
| "reduced", |
| "risk", |
| "less likely", |
| "protective", |
| "mortality reduction", |
| }, |
| } |
|
|
|
|
| def normalize_relation(value: Any) -> str: |
| return str(value or "").strip().upper().replace(" ", "_").replace("-", "_") |
|
|
|
|
| def normalize_confidence(value: Any) -> float: |
| try: |
| score = float(value) |
| except (TypeError, ValueError): |
| return 0.0 |
| if score < 0: |
| return 0.0 |
| if score > 1: |
| return 1.0 |
| return score |
|
|
|
|
| def source_key(item: dict[str, Any]) -> tuple[str, str]: |
| return str(item.get("source_doc_id") or ""), str(item.get("source_sent_id") or "") |
|
|
|
|
| def candidate_sources(evidence: list[dict[str, Any]]) -> dict[tuple[str, str], dict[str, Any]]: |
| return {source_key(item): item for item in evidence} |
|
|
|
|
| def validate_fact(fact: dict[str, Any], sources: dict[tuple[str, str], dict[str, Any]]) -> list[str]: |
| errors: list[str] = [] |
| for field in FACT_FIELDS: |
| if fact.get(field) in {None, ""}: |
| errors.append(f"missing_{field}") |
| if source_key(fact) not in sources: |
| errors.append("citation_not_in_candidate_pool") |
| return errors |
|
|
|
|
| def validate_triple(triple: dict[str, Any], sources: dict[tuple[str, str], dict[str, Any]], dataset: str) -> list[str]: |
| errors: list[str] = [] |
| for field in TRIPLE_FIELDS: |
| if triple.get(field) in {None, ""}: |
| errors.append(f"missing_{field}") |
| relation = normalize_relation(triple.get("relation")) |
| if relation not in RELATION_ONTOLOGY: |
| errors.append("ontology_violation") |
| if dataset == "healthver" and relation in BIOMED_RELATIONS: |
| source_text = str(triple.get("source_text") or "").casefold() |
| cues = HEALTHVER_DIRECT_CUES.get(relation, set()) |
| if not any(token in source_text for token in cues): |
| errors.append("healthver_biomed_relation_not_direct") |
| if source_key(triple) not in sources: |
| errors.append("citation_not_in_candidate_pool") |
| return errors |
|
|
|
|
| def validate_claim_extraction( |
| dataset: str, |
| evidence: list[dict[str, Any]], |
| facts: list[dict[str, Any]], |
| triples: list[dict[str, Any]], |
| parse_success: bool, |
| ) -> dict[str, Any]: |
| sources = candidate_sources(evidence) |
| fact_errors = [error for fact in facts for error in validate_fact(fact, sources)] |
| triple_errors = [error for triple in triples for error in validate_triple(triple, sources, dataset)] |
| citation_errors = sum(1 for error in fact_errors + triple_errors if error == "citation_not_in_candidate_pool") |
| ontology_errors = sum(1 for error in triple_errors if error in {"ontology_violation", "healthver_biomed_relation_not_direct"}) |
| item_count = len(facts) + len(triples) |
| return { |
| "parse_success": bool(parse_success), |
| "facts": len(facts), |
| "triples": len(triples), |
| "item_count": item_count, |
| "citation_valid": max(0, item_count - citation_errors), |
| "citation_errors": citation_errors, |
| "ontology_errors": ontology_errors, |
| "fact_error_counts": dict(Counter(fact_errors)), |
| "triple_error_counts": dict(Counter(triple_errors)), |
| "claim_subgraph_built": len(triples) > 0, |
| } |
|
|