Skip to main content

narrative_graph/
types.rs

1use std::collections::BTreeMap;
2
3/// A candidate relational fact extracted from text.
4/// Every candidate carries confidence, provenance span, and the rule that produced it.
5#[derive(Debug, Clone)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
8pub struct TripleCandidate {
9    /// The subject entity (normalized to lowercase with underscores for multi-word entities).
10    pub subject: String,
11    /// The relation type (normalized from verb phrases to a controlled vocabulary).
12    pub relation: String,
13    /// The object entity (normalized to lowercase with underscores for multi-word entities).
14    pub object: String,
15    /// Confidence score from 0.0 to 1.0, derived from rule strength and entity proximity.
16    pub confidence: f32,
17    /// Byte range [start, end) in the input text where this candidate was extracted.
18    pub span: [usize; 2],
19    /// The name of the extraction rule that produced this candidate.
20    /// Examples: "possessive-sister-pattern", "verb-mentor-pattern".
21    pub rule: String,
22}
23
24/// Configuration for entity and relation extraction.
25#[derive(Debug, Clone, Default)]
26#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
27#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
28pub struct Options {
29    /// Optional mapping from surface forms to canonical entity names.
30    /// Example: { "the detective": "marcus", "ms. chen": "chen" }
31    /// All mentions matching a key in this map resolve to the value.
32    #[cfg_attr(feature = "serde", serde(default))]
33    pub aliases: BTreeMap<String, String>,
34
35    /// Minimum confidence threshold for returned candidates.
36    /// Candidates below this score are filtered out.
37    /// Default: 0.0 (all candidates returned).
38    #[cfg_attr(feature = "serde", serde(default))]
39    pub min_confidence: Option<f32>,
40
41    /// Optional mapping from recognized relation patterns to a caller-supplied controlled vocabulary.
42    /// Example: { "loves": "romantic_interest", "is_married_to": "spouse" }
43    /// If a relation type is not in this map, the heuristic default is used.
44    #[cfg_attr(feature = "serde", serde(default))]
45    pub ontology: BTreeMap<String, String>,
46}
47
48/// A reference span into the input text with its corresponding surface text.
49#[derive(Debug, Clone)]
50#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
51#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
52pub struct SpannedTriple {
53    pub candidate: TripleCandidate,
54    /// The actual text from input[span[0]..span[1]].
55    pub text: String,
56}