use std::collections::BTreeMap;
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub struct TripleCandidate {
pub subject: String,
pub relation: String,
pub object: String,
pub confidence: f32,
pub span: [usize; 2],
pub rule: String,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub struct Rejection {
pub subject: String,
pub relation: String,
pub object: String,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
#[non_exhaustive]
pub enum Language {
#[default]
English,
Chinese,
}
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub struct Options {
#[cfg_attr(feature = "serde", serde(default))]
pub aliases: BTreeMap<String, String>,
#[cfg_attr(feature = "serde", serde(default))]
pub min_confidence: Option<f32>,
#[cfg_attr(feature = "serde", serde(default))]
pub rejections: Vec<Rejection>,
#[cfg_attr(feature = "serde", serde(default))]
pub ontology: BTreeMap<String, String>,
#[cfg_attr(feature = "serde", serde(default))]
pub language: Language,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub enum Polarity {
Asserted,
Denied,
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub struct AggregateTriple {
pub subject: String,
pub relation: String,
pub object: String,
pub polarity: Polarity,
pub confidence: f32,
pub spans: Vec<[usize; 2]>,
pub rules: Vec<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub enum ConflictKind {
Denial,
Cardinality,
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub struct Conflict {
pub kind: ConflictKind,
pub left: AggregateTriple,
pub right: AggregateTriple,
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub struct SpannedTriple {
pub candidate: TripleCandidate,
pub text: String,
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub struct Analysis {
pub candidates: Vec<TripleCandidate>,
pub aggregates: Vec<AggregateTriple>,
pub conflicts: Vec<Conflict>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub enum Gender {
Feminine,
Masculine,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub struct FamilyLink {
pub role: String,
pub relative: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub struct CharacterProfile {
pub id: String,
pub aliases: Vec<String>,
pub gender: Option<Gender>,
pub family: Vec<FamilyLink>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub enum MentionKind {
Name,
Pronoun,
Description,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub struct CharacterMention {
pub id: String,
pub kind: MentionKind,
pub span: [usize; 2],
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub struct DocumentAnalysis {
pub analysis: Analysis,
pub characters: Vec<CharacterProfile>,
pub mentions: Vec<CharacterMention>,
pub timeline: Vec<FactOccurrence>,
pub events: Vec<EventCandidate>,
pub knowledge: Vec<KnowledgeFact>,
pub dialogue: Dialogue,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub struct DialogueLine {
pub speaker: Option<String>,
pub listener: Option<String>,
pub span: [usize; 2],
pub spoken_spans: Vec<[usize; 2]>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub struct Dialogue {
pub lines: Vec<DialogueLine>,
pub unattributed: usize,
}
impl Dialogue {
pub(crate) fn from_lines(mut lines: Vec<DialogueLine>) -> Self {
lines.sort_by_key(|line| line.span);
let unattributed = lines
.iter()
.filter(|line| line.speaker.is_none() || line.listener.is_none())
.count();
Self {
lines,
unattributed,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub enum RelationshipState {
Engaged,
Married,
Widowed,
Divorced,
Friends,
Enemies,
Allies,
}
impl RelationshipState {
pub(crate) fn from_relation(relation: &str) -> Option<Self> {
Some(match relation {
"engaged_to" | "fiance_of" => Self::Engaged,
"spouse_of" | "wife_of" | "husband_of" => Self::Married,
"widow_of" => Self::Widowed,
"divorced" | "ex_wife_of" | "ex_husband_of" => Self::Divorced,
"friend_of" => Self::Friends,
"enemy_of" => Self::Enemies,
"ally_of" => Self::Allies,
_ => return None,
})
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub struct FactOccurrence {
pub candidate: TripleCandidate,
pub polarity: Polarity,
pub state: Option<RelationshipState>,
pub ends_state: bool,
pub calibrated_confidence: Option<f32>,
}
impl DocumentAnalysis {
pub fn relationship_history<'a>(
&'a self,
person: &'a str,
other: &'a str,
) -> impl Iterator<Item = &'a FactOccurrence> {
self.timeline.iter().filter(move |entry| {
let c = &entry.candidate;
entry.state.is_some()
&& ((c.subject == person && c.object == other)
|| (c.subject == other && c.object == person))
})
}
pub fn facts_at_least(
&self,
probability: f32,
) -> crate::Result<impl Iterator<Item = &FactOccurrence>> {
if !(0.0..=1.0).contains(&probability) {
return Err(crate::NarrativeGraphError::InvalidConfidenceThreshold(
probability,
));
}
Ok(self.timeline.iter().filter(move |entry| {
entry
.calibrated_confidence
.is_some_and(|estimate| estimate >= probability)
}))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub enum EventKind {
Duel,
Proposal,
Journey,
Death,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub struct EventCandidate {
pub kind: EventKind,
pub actor: String,
pub target: Option<String>,
pub location: Option<String>,
pub span: [usize; 2],
pub trigger: [usize; 2],
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub enum KnowledgeKind {
Learned,
Known,
Told,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
pub struct KnowledgeFact {
pub kind: KnowledgeKind,
pub actor: String,
pub recipient: Option<String>,
pub topic: Option<String>,
pub span: [usize; 2],
pub content: [usize; 2],
}