File size: 1,021 Bytes
f0b9cf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
from pathlib import Path

from pydantic import BaseModel

from fdi.schema import Document

KNOWLEDGE_BASE_DIR = Path("data/knowledge_base")


def save_facts(document: Document, fact_type: str, facts: list[BaseModel]) -> Path:
    """Persist extracted facts for one document as JSON, organized by VDR category."""
    dest_dir = KNOWLEDGE_BASE_DIR / document.category
    dest_dir.mkdir(parents=True, exist_ok=True)
    dest_path = dest_dir / f"{Path(document.source_path).stem}.json"

    payload = {
        "source_path": document.source_path,
        "fact_type": fact_type,
        "facts": [fact.model_dump() for fact in facts],
    }
    dest_path.write_text(json.dumps(payload, indent=2))
    return dest_path


def load_category_facts(category: str) -> list[dict]:
    """Load every saved fact record for a given VDR category."""
    dest_dir = KNOWLEDGE_BASE_DIR / category
    if not dest_dir.exists():
        return []
    return [json.loads(path.read_text()) for path in dest_dir.glob("*.json")]