| 89 | |
| 90 | @classmethod |
| 91 | def from_dict(cls, data: dict) -> "KnowledgeGraph": |
| 92 | g = cls() |
| 93 | for e in data.get("entities", []) or []: |
| 94 | try: |
| 95 | ent = Entity( |
| 96 | name=str(e["name"]), |
| 97 | type=str(e.get("type", "")), |
| 98 | count=int(e.get("count", 1)), |
| 99 | last_seen=float(e.get("last_seen", 0.0)), |
| 100 | attributes=dict(e.get("attributes", {})), |
| 101 | ) |
| 102 | g.entities[cls._key(ent.name, ent.type)] = ent |
| 103 | except Exception: # noqa: BLE001 - skip malformed entries |
| 104 | continue |
| 105 | return g |
| 106 | |
| 107 | def save(self, path: Path | None = None) -> None: |
| 108 | p = path or default_graph_path() |