inferCausality method
Analyses a pair of Concepts and returns a CausalRelationship if sufficient causal evidence is found, otherwise null.
Implementation
CausalRelationship? inferCausality(Concept cause, Concept effect) {
final combined =
'${cause.content} ${effect.content}'.toLowerCase();
var score = 0.0;
final matchedCues = <String>[];
for (final cue in _cues) {
if (combined.contains(cue.pattern)) {
score += cue.weight;
matchedCues.add(cue.pattern);
}
}
// Temporal ordering boost: earlier = more likely cause
final causeFirst =
cause.creationTime.isBefore(effect.creationTime);
if (causeFirst) score += 0.15;
// Normalise
final confidence = math.min(1.0, score / 2.0);
if (confidence < 0.2) return null;
final relationship = CausalRelationship(
causeConceptId: cause.id,
effectConceptId: effect.id,
description:
'"${cause.content}" causes "${effect.content}"',
strength: confidence,
temporalGap: effect.creationTime
.difference(cause.creationTime)
.abs(),
);
_knownCausality.add(relationship);
_logger.debug(
'Causal relationship found: '
'${relationship.description} '
'(strength: ${confidence.toStringAsFixed(2)}, '
'cues: ${matchedCues.join(", ")})');
return relationship;
}