1use std::collections::BTreeMap;
2
3#[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 pub subject: String,
11 pub relation: String,
13 pub object: String,
15 pub confidence: f32,
17 pub span: [usize; 2],
19 pub rule: String,
22}
23
24#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
38#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
39#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
40pub struct Rejection {
41 pub subject: String,
42 pub relation: String,
43 pub object: String,
44}
45
46#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
53#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
54#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
55#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
56#[non_exhaustive]
57pub enum Language {
58 #[default]
59 English,
60 Chinese,
62}
63
64#[derive(Debug, Clone, Default)]
66#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
68pub struct Options {
69 #[cfg_attr(feature = "serde", serde(default))]
85 pub aliases: BTreeMap<String, String>,
86
87 #[cfg_attr(feature = "serde", serde(default))]
91 pub min_confidence: Option<f32>,
92
93 #[cfg_attr(feature = "serde", serde(default))]
107 pub rejections: Vec<Rejection>,
108
109 #[cfg_attr(feature = "serde", serde(default))]
113 pub ontology: BTreeMap<String, String>,
114
115 #[cfg_attr(feature = "serde", serde(default))]
120 pub language: Language,
121}
122
123#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
129#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
130#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
131#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
132pub enum Polarity {
133 Asserted,
134 Denied,
135}
136
137#[derive(Debug, Clone)]
144#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
145#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
146pub struct AggregateTriple {
147 pub subject: String,
148 pub relation: String,
149 pub object: String,
150 pub polarity: Polarity,
155 pub confidence: f32,
165 pub spans: Vec<[usize; 2]>,
167 pub rules: Vec<String>,
170}
171
172#[derive(Debug, Clone, Copy, PartialEq, Eq)]
174#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
175#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
176#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
177pub enum ConflictKind {
178 Denial,
180 Cardinality,
183}
184
185#[derive(Debug, Clone)]
191#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
192#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
193pub struct Conflict {
194 pub kind: ConflictKind,
195 pub left: AggregateTriple,
197 pub right: AggregateTriple,
199}
200
201#[derive(Debug, Clone)]
203#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
204#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
205pub struct SpannedTriple {
206 pub candidate: TripleCandidate,
207 pub text: String,
209}
210
211#[derive(Debug, Clone)]
214#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
215#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
216pub struct Analysis {
217 pub candidates: Vec<TripleCandidate>,
218 pub aggregates: Vec<AggregateTriple>,
219 pub conflicts: Vec<Conflict>,
220}
221
222#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
224#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
225#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
226#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
227pub enum Gender {
228 Feminine,
229 Masculine,
230}
231
232#[derive(Debug, Clone, PartialEq, Eq)]
234#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
235#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
236pub struct FamilyLink {
237 pub role: String,
238 pub relative: String,
239}
240
241#[derive(Debug, Clone, PartialEq, Eq)]
243#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
244#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
245pub struct CharacterProfile {
246 pub id: String,
247 pub aliases: Vec<String>,
248 pub gender: Option<Gender>,
249 pub family: Vec<FamilyLink>,
250}
251
252#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
253#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
254#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
255#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
256pub enum MentionKind {
257 Name,
258 Pronoun,
259 Description,
260}
261
262#[derive(Debug, Clone, PartialEq, Eq)]
264#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
265#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
266pub struct CharacterMention {
267 pub id: String,
268 pub kind: MentionKind,
269 pub span: [usize; 2],
270}
271
272#[derive(Debug, Clone)]
274#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
275#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
276pub struct DocumentAnalysis {
277 pub analysis: Analysis,
278 pub characters: Vec<CharacterProfile>,
279 pub mentions: Vec<CharacterMention>,
280 pub timeline: Vec<FactOccurrence>,
282 pub events: Vec<EventCandidate>,
284 pub knowledge: Vec<KnowledgeFact>,
286 pub dialogue: Dialogue,
287}
288
289#[derive(Debug, Clone, PartialEq, Eq)]
291#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
292#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
293pub struct DialogueLine {
294 pub speaker: Option<String>,
295 pub listener: Option<String>,
296 pub span: [usize; 2],
298 pub spoken_spans: Vec<[usize; 2]>,
300}
301
302#[derive(Debug, Clone, Default, PartialEq, Eq)]
304#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
305#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
306pub struct Dialogue {
307 pub lines: Vec<DialogueLine>,
308 pub unattributed: usize,
310}
311
312impl Dialogue {
313 pub(crate) fn from_lines(mut lines: Vec<DialogueLine>) -> Self {
314 lines.sort_by_key(|line| line.span);
315 let unattributed = lines
316 .iter()
317 .filter(|line| line.speaker.is_none() || line.listener.is_none())
318 .count();
319 Self {
320 lines,
321 unattributed,
322 }
323 }
324}
325
326#[derive(Debug, Clone, Copy, PartialEq, Eq)]
328#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
329#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
330#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
331pub enum RelationshipState {
332 Engaged,
333 Married,
334 Widowed,
335 Divorced,
336 Friends,
337 Enemies,
338 Allies,
339}
340
341impl RelationshipState {
342 pub(crate) fn from_relation(relation: &str) -> Option<Self> {
343 Some(match relation {
344 "engaged_to" | "fiance_of" => Self::Engaged,
345 "spouse_of" | "wife_of" | "husband_of" => Self::Married,
346 "widow_of" => Self::Widowed,
347 "divorced" | "ex_wife_of" | "ex_husband_of" => Self::Divorced,
348 "friend_of" => Self::Friends,
349 "enemy_of" => Self::Enemies,
350 "ally_of" => Self::Allies,
351 _ => return None,
352 })
353 }
354}
355
356#[derive(Debug, Clone)]
358#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
359#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
360pub struct FactOccurrence {
361 pub candidate: TripleCandidate,
362 pub polarity: Polarity,
363 pub state: Option<RelationshipState>,
364 pub ends_state: bool,
366 pub calibrated_confidence: Option<f32>,
368}
369
370impl DocumentAnalysis {
371 pub fn relationship_history<'a>(
373 &'a self,
374 person: &'a str,
375 other: &'a str,
376 ) -> impl Iterator<Item = &'a FactOccurrence> {
377 self.timeline.iter().filter(move |entry| {
378 let c = &entry.candidate;
379 entry.state.is_some()
380 && ((c.subject == person && c.object == other)
381 || (c.subject == other && c.object == person))
382 })
383 }
384
385 pub fn facts_at_least(
387 &self,
388 probability: f32,
389 ) -> crate::Result<impl Iterator<Item = &FactOccurrence>> {
390 if !(0.0..=1.0).contains(&probability) {
391 return Err(crate::NarrativeGraphError::InvalidConfidenceThreshold(
392 probability,
393 ));
394 }
395 Ok(self.timeline.iter().filter(move |entry| {
396 entry
397 .calibrated_confidence
398 .is_some_and(|estimate| estimate >= probability)
399 }))
400 }
401}
402
403#[derive(Debug, Clone, Copy, PartialEq, Eq)]
404#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
405#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
406#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
407pub enum EventKind {
408 Duel,
409 Proposal,
410 Journey,
411 Death,
412}
413
414#[derive(Debug, Clone, PartialEq, Eq)]
416#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
417#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
418pub struct EventCandidate {
419 pub kind: EventKind,
420 pub actor: String,
421 pub target: Option<String>,
422 pub location: Option<String>,
423 pub span: [usize; 2],
424 pub trigger: [usize; 2],
426}
427
428#[derive(Debug, Clone, Copy, PartialEq, Eq)]
429#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
430#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
431#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
432pub enum KnowledgeKind {
433 Learned,
434 Known,
435 Told,
436}
437
438#[derive(Debug, Clone, PartialEq, Eq)]
440#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
441#[cfg_attr(feature = "bindings", derive(ts_rs::TS), ts(export))]
442pub struct KnowledgeFact {
443 pub kind: KnowledgeKind,
444 pub actor: String,
445 pub recipient: Option<String>,
446 pub topic: Option<String>,
447 pub span: [usize; 2],
448 pub content: [usize; 2],
450}