Skip to main content

narrative_graph/
calibration.rs

1/// Rule families share calibration evidence for similar extraction patterns.
2#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
3pub enum CalibrationGroup {
4    DialogueTurn,
5    DialogueVocative,
6    Nominal,
7    Phrase,
8    Verb,
9}
10
11impl CalibrationGroup {
12    pub fn for_rule(rule: &str) -> Option<Self> {
13        let group = match rule {
14            "dialogue-turn" => Self::DialogueTurn,
15            "dialogue-vocative" => Self::DialogueVocative,
16            _ if rule.starts_with("phrase-") => Self::Phrase,
17            _ if rule.starts_with("verb-") || rule.starts_with("relative-") => Self::Verb,
18            _ if [
19                "possessive-",
20                "kin-prefix-",
21                "of-genitive-",
22                "pronoun-",
23                "plural-",
24            ]
25            .iter()
26            .any(|prefix| rule.starts_with(prefix)) =>
27            {
28                Self::Nominal
29            }
30            _ => return None,
31        };
32        Some(group)
33    }
34}
35
36/// Correctness counts for a family of rules on public fiction.
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub struct RuleCalibration {
39    pub correct: u32,
40    pub observations: u32,
41}
42
43impl RuleCalibration {
44    /// The posterior mean with four prior observations centered on the heuristic.
45    pub fn estimate(self, heuristic: f32) -> f32 {
46        const PRIOR_WEIGHT: f32 = 4.0;
47        (self.correct as f32 + PRIOR_WEIGHT * heuristic) / (self.observations as f32 + PRIOR_WEIGHT)
48    }
49}
50
51/// Calibration evidence for the rule's family in the public sample.
52pub fn rule_calibration(rule: &str) -> Option<RuleCalibration> {
53    let (correct, observations) = match CalibrationGroup::for_rule(rule)? {
54        CalibrationGroup::DialogueTurn => (9, 23),
55        CalibrationGroup::DialogueVocative => (10, 10),
56        CalibrationGroup::Nominal => (7, 10),
57        CalibrationGroup::Phrase => (15, 22),
58        _ => return None,
59    };
60    Some(RuleCalibration {
61        correct,
62        observations,
63    })
64}