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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""HuggingFace Datasets loader for .causal knowledge graph files."""
|
| 2 |
+
|
| 3 |
+
import datasets
|
| 4 |
+
from datasets import DatasetInfo, Features, Value, Sequence
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class CausalConfig(datasets.BuilderConfig):
|
| 8 |
+
"""BuilderConfig for .causal files."""
|
| 9 |
+
|
| 10 |
+
def __init__(
|
| 11 |
+
self,
|
| 12 |
+
include_inferred: bool = True,
|
| 13 |
+
min_confidence: float = 0.0,
|
| 14 |
+
**kwargs,
|
| 15 |
+
):
|
| 16 |
+
"""
|
| 17 |
+
Args:
|
| 18 |
+
include_inferred: Include inferred triplets (default: True)
|
| 19 |
+
min_confidence: Minimum confidence threshold (default: 0.0)
|
| 20 |
+
"""
|
| 21 |
+
super().__init__(**kwargs)
|
| 22 |
+
self.include_inferred = include_inferred
|
| 23 |
+
self.min_confidence = min_confidence
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class CausalDataset(datasets.GeneratorBasedBuilder):
|
| 27 |
+
"""
|
| 28 |
+
HuggingFace Dataset loader for .causal knowledge graph files.
|
| 29 |
+
|
| 30 |
+
The .causal format is a binary knowledge graph with embedded deterministic
|
| 31 |
+
inference. It provides zero-hallucination fact retrieval with full provenance.
|
| 32 |
+
|
| 33 |
+
Usage:
|
| 34 |
+
from datasets import load_dataset
|
| 35 |
+
|
| 36 |
+
# Load from local file
|
| 37 |
+
ds = load_dataset("chkmie/dotcausal", data_files="knowledge.causal")
|
| 38 |
+
|
| 39 |
+
# Load with config
|
| 40 |
+
ds = load_dataset(
|
| 41 |
+
"chkmie/dotcausal",
|
| 42 |
+
data_files="knowledge.causal",
|
| 43 |
+
include_inferred=True,
|
| 44 |
+
min_confidence=0.5,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
Features:
|
| 48 |
+
- trigger (str): The cause/trigger entity
|
| 49 |
+
- mechanism (str): The relationship type
|
| 50 |
+
- outcome (str): The effect/outcome entity
|
| 51 |
+
- confidence (float): Confidence score (0-1)
|
| 52 |
+
- is_inferred (bool): Whether derived or explicit
|
| 53 |
+
- source (str): Original source (e.g., paper)
|
| 54 |
+
- provenance (list): Source triplets for inferred facts
|
| 55 |
+
|
| 56 |
+
References:
|
| 57 |
+
- PyPI: https://pypi.org/project/dotcausal/
|
| 58 |
+
- GitHub: https://github.com/DT-Foss/dotcausal
|
| 59 |
+
- Paper: https://doi.org/10.5281/zenodo.18326222
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
BUILDER_CONFIG_CLASS = CausalConfig
|
| 63 |
+
BUILDER_CONFIGS = [
|
| 64 |
+
CausalConfig(
|
| 65 |
+
name="default",
|
| 66 |
+
version=datasets.Version("1.0.0"),
|
| 67 |
+
description="Load all triplets from .causal files",
|
| 68 |
+
),
|
| 69 |
+
CausalConfig(
|
| 70 |
+
name="explicit_only",
|
| 71 |
+
version=datasets.Version("1.0.0"),
|
| 72 |
+
description="Load only explicit triplets (no inferred)",
|
| 73 |
+
include_inferred=False,
|
| 74 |
+
),
|
| 75 |
+
CausalConfig(
|
| 76 |
+
name="high_confidence",
|
| 77 |
+
version=datasets.Version("1.0.0"),
|
| 78 |
+
description="Load triplets with confidence >= 0.8",
|
| 79 |
+
min_confidence=0.8,
|
| 80 |
+
),
|
| 81 |
+
]
|
| 82 |
+
DEFAULT_CONFIG_NAME = "default"
|
| 83 |
+
|
| 84 |
+
def _info(self):
|
| 85 |
+
return DatasetInfo(
|
| 86 |
+
description="""\
|
| 87 |
+
.causal knowledge graph dataset with embedded deterministic inference.
|
| 88 |
+
Each row represents a causal triplet (trigger → mechanism → outcome).
|
| 89 |
+
""",
|
| 90 |
+
features=Features(
|
| 91 |
+
{
|
| 92 |
+
"trigger": Value("string"),
|
| 93 |
+
"mechanism": Value("string"),
|
| 94 |
+
"outcome": Value("string"),
|
| 95 |
+
"confidence": Value("float32"),
|
| 96 |
+
"is_inferred": Value("bool"),
|
| 97 |
+
"source": Value("string"),
|
| 98 |
+
"provenance": Sequence(Value("string")),
|
| 99 |
+
}
|
| 100 |
+
),
|
| 101 |
+
homepage="https://dotcausal.com",
|
| 102 |
+
license="MIT",
|
| 103 |
+
citation="""\
|
| 104 |
+
@article{foss2026causal,
|
| 105 |
+
author = {Foss, David Tom},
|
| 106 |
+
title = {The .causal Format: Deterministic Inference for AI-Assisted Hypothesis Amplification},
|
| 107 |
+
journal = {Zenodo},
|
| 108 |
+
year = {2026},
|
| 109 |
+
doi = {10.5281/zenodo.18326222}
|
| 110 |
+
}
|
| 111 |
+
""",
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
def _split_generators(self, dl_manager):
|
| 115 |
+
"""Generate splits from data files."""
|
| 116 |
+
data_files = self.config.data_files
|
| 117 |
+
|
| 118 |
+
if not data_files:
|
| 119 |
+
raise ValueError(
|
| 120 |
+
"No data_files specified. Use: load_dataset('chkmie/dotcausal', data_files='your_file.causal')"
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
# Handle different data_files formats
|
| 124 |
+
if isinstance(data_files, dict):
|
| 125 |
+
# {"train": ["file1.causal"], "test": ["file2.causal"]}
|
| 126 |
+
splits = []
|
| 127 |
+
for split_name, files in data_files.items():
|
| 128 |
+
if isinstance(files, str):
|
| 129 |
+
files = [files]
|
| 130 |
+
downloaded = dl_manager.download_and_extract(files)
|
| 131 |
+
splits.append(
|
| 132 |
+
datasets.SplitGenerator(
|
| 133 |
+
name=split_name,
|
| 134 |
+
gen_kwargs={"filepaths": downloaded},
|
| 135 |
+
)
|
| 136 |
+
)
|
| 137 |
+
return splits
|
| 138 |
+
elif isinstance(data_files, (list, tuple)):
|
| 139 |
+
# ["file1.causal", "file2.causal"]
|
| 140 |
+
downloaded = dl_manager.download_and_extract(list(data_files))
|
| 141 |
+
return [
|
| 142 |
+
datasets.SplitGenerator(
|
| 143 |
+
name=datasets.Split.TRAIN,
|
| 144 |
+
gen_kwargs={"filepaths": downloaded},
|
| 145 |
+
)
|
| 146 |
+
]
|
| 147 |
+
else:
|
| 148 |
+
# Single file string
|
| 149 |
+
downloaded = dl_manager.download_and_extract([data_files])
|
| 150 |
+
return [
|
| 151 |
+
datasets.SplitGenerator(
|
| 152 |
+
name=datasets.Split.TRAIN,
|
| 153 |
+
gen_kwargs={"filepaths": downloaded},
|
| 154 |
+
)
|
| 155 |
+
]
|
| 156 |
+
|
| 157 |
+
def _generate_examples(self, filepaths):
|
| 158 |
+
"""Generate examples from .causal files."""
|
| 159 |
+
try:
|
| 160 |
+
from dotcausal import CausalReader
|
| 161 |
+
except ImportError:
|
| 162 |
+
raise ImportError(
|
| 163 |
+
"dotcausal package required. Install with: pip install dotcausal"
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
if isinstance(filepaths, str):
|
| 167 |
+
filepaths = [filepaths]
|
| 168 |
+
|
| 169 |
+
idx = 0
|
| 170 |
+
for filepath in filepaths:
|
| 171 |
+
reader = CausalReader(filepath)
|
| 172 |
+
|
| 173 |
+
# Get all triplets via search
|
| 174 |
+
results = reader.search("", limit=100000)
|
| 175 |
+
|
| 176 |
+
for r in results:
|
| 177 |
+
# Apply filters from config
|
| 178 |
+
confidence = r.get("confidence", 1.0)
|
| 179 |
+
is_inferred = r.get("is_inferred", False)
|
| 180 |
+
|
| 181 |
+
if confidence < self.config.min_confidence:
|
| 182 |
+
continue
|
| 183 |
+
if not self.config.include_inferred and is_inferred:
|
| 184 |
+
continue
|
| 185 |
+
|
| 186 |
+
# Convert provenance to list of strings
|
| 187 |
+
provenance = r.get("provenance", [])
|
| 188 |
+
if not isinstance(provenance, list):
|
| 189 |
+
provenance = [str(provenance)] if provenance else []
|
| 190 |
+
else:
|
| 191 |
+
provenance = [str(p) for p in provenance]
|
| 192 |
+
|
| 193 |
+
yield idx, {
|
| 194 |
+
"trigger": r.get("trigger", ""),
|
| 195 |
+
"mechanism": r.get("mechanism", ""),
|
| 196 |
+
"outcome": r.get("outcome", ""),
|
| 197 |
+
"confidence": float(confidence),
|
| 198 |
+
"is_inferred": bool(is_inferred),
|
| 199 |
+
"source": r.get("source", ""),
|
| 200 |
+
"provenance": provenance,
|
| 201 |
+
}
|
| 202 |
+
idx += 1
|