Skip to main content

qualia_core_db/research/
perspective.rs

1//! Perspective analysis — register, bias, compare, conflict detection, reconciliation.
2
3/// A perspective held by an agent or observer.
4#[derive(Debug, Clone)]
5pub struct Perspective {
6    pub id: String,
7    pub agent_id: String,
8    pub viewpoint: String,
9    pub biases: Vec<Bias>,
10    pub confidence: f64,
11}
12
13/// A cognitive bias affecting a perspective.
14#[derive(Debug, Clone)]
15pub struct Bias {
16    pub bias_type: String,
17    pub description: String,
18    pub severity: f64,
19}
20
21/// A conflict between two perspectives.
22#[derive(Debug, Clone)]
23pub struct PerspectiveConflict {
24    pub perspective_a: String,
25    pub perspective_b: String,
26    pub conflict_type: String,
27    pub description: String,
28    pub severity: f64,
29}
30
31/// Register a new perspective.
32pub fn register_perspective(id: &str, agent_id: &str, viewpoint: &str) -> Perspective {
33    Perspective {
34        id: id.to_string(),
35        agent_id: agent_id.to_string(),
36        viewpoint: viewpoint.to_string(),
37        biases: Vec::new(),
38        confidence: 0.5,
39    }
40}
41
42/// Add a bias to a perspective.
43pub fn add_bias(perspective: &mut Perspective, bias: Bias) {
44    perspective.biases.push(bias);
45}
46
47/// Compare two perspectives — returns similarity score and identified conflicts.
48pub fn compare_perspectives(a: &Perspective, b: &Perspective) -> PerspectiveComparison {
49    let a_lower = a.viewpoint.to_lowercase();
50    let b_lower = b.viewpoint.to_lowercase();
51    let a_words: Vec<&str> = a_lower.split_whitespace().collect();
52    let b_words: Vec<&str> = b_lower.split_whitespace().collect();
53    let a_set: std::collections::BTreeSet<&str> = a_words.iter().copied().collect();
54    let b_set: std::collections::BTreeSet<&str> = b_words.iter().copied().collect();
55    let common = a_set.intersection(&b_set).count();
56    let total = a_set.union(&b_set).count();
57    let similarity = if total > 0 {
58        common as f64 / total as f64
59    } else {
60        0.0
61    };
62
63    let conflicts = detect_perspective_conflict(a, b);
64    PerspectiveComparison {
65        similarity,
66        conflicts,
67    }
68}
69
70#[derive(Debug, Clone)]
71pub struct PerspectiveComparison {
72    pub similarity: f64,
73    pub conflicts: Vec<PerspectiveConflict>,
74}
75
76/// Detect conflicts between two perspectives.
77pub fn detect_perspective_conflict(a: &Perspective, b: &Perspective) -> Vec<PerspectiveConflict> {
78    let mut conflicts = Vec::new();
79    // Check for opposing sentiment markers.
80    let opposition_markers = [
81        ("agree", "disagree"),
82        ("true", "false"),
83        ("correct", "incorrect"),
84        ("right", "wrong"),
85        ("good", "bad"),
86        ("yes", "no"),
87    ];
88    let a_lower = a.viewpoint.to_lowercase();
89    let b_lower = b.viewpoint.to_lowercase();
90    for (pos, neg) in opposition_markers {
91        if (a_lower.contains(pos) && b_lower.contains(neg))
92            || (a_lower.contains(neg) && b_lower.contains(pos))
93        {
94            conflicts.push(PerspectiveConflict {
95                perspective_a: a.id.clone(),
96                perspective_b: b.id.clone(),
97                conflict_type: "opposing_claim".into(),
98                description: format!("'{pos}' vs '{neg}' opposition detected"),
99                severity: 0.8,
100            });
101        }
102    }
103    conflicts
104}
105
106/// Reconcile two perspectives — find common ground.
107pub fn reconcile_perspectives(a: &Perspective, b: &Perspective) -> ReconciliationResult {
108    let comparison = compare_perspectives(a, b);
109    let a_lower = a.viewpoint.to_lowercase();
110    let b_lower = b.viewpoint.to_lowercase();
111    let common_words: Vec<&str> = {
112        let a_set: std::collections::BTreeSet<&str> = a_lower.split_whitespace().collect();
113        let b_set: std::collections::BTreeSet<&str> = b_lower.split_whitespace().collect();
114        a_set.intersection(&b_set).copied().collect()
115    };
116    ReconciliationResult {
117        common_ground: common_words.join(" "),
118        similarity: comparison.similarity,
119        conflict_count: comparison.conflicts.len(),
120        reconcilable: comparison.similarity > 0.3 && comparison.conflicts.len() < 3,
121    }
122}
123
124#[derive(Debug, Clone)]
125pub struct ReconciliationResult {
126    pub common_ground: String,
127    pub similarity: f64,
128    pub conflict_count: usize,
129    pub reconcilable: bool,
130}
131
132#[cfg(test)]
133mod tests {
134    use super::*;
135
136    #[test]
137    fn register_perspective_basic() {
138        let p = register_perspective("p1", "agent_a", "Climate change is real");
139        assert_eq!(p.id, "p1");
140        assert_eq!(p.agent_id, "agent_a");
141    }
142
143    #[test]
144    fn add_bias_basic() {
145        let mut p = register_perspective("p1", "agent_a", "Test");
146        add_bias(
147            &mut p,
148            Bias {
149                bias_type: "confirmation".into(),
150                description: "Seeks confirming evidence".into(),
151                severity: 0.7,
152            },
153        );
154        assert_eq!(p.biases.len(), 1);
155    }
156
157    #[test]
158    fn compare_similar_perspectives() {
159        let a = register_perspective("p1", "a", "climate change is real and dangerous");
160        let b = register_perspective("p2", "b", "climate change is real and concerning");
161        let comp = compare_perspectives(&a, &b);
162        assert!(comp.similarity > 0.5);
163    }
164
165    #[test]
166    fn compare_different_perspectives() {
167        let a = register_perspective("p1", "a", "the sky is blue");
168        let b = register_perspective("p2", "b", "economic policy reform");
169        let comp = compare_perspectives(&a, &b);
170        assert!(comp.similarity < 0.3);
171    }
172
173    #[test]
174    fn detect_conflict_opposing() {
175        let a = register_perspective("p1", "a", "I agree with the proposal");
176        let b = register_perspective("p2", "b", "I disagree with the proposal");
177        let conflicts = detect_perspective_conflict(&a, &b);
178        assert!(!conflicts.is_empty());
179    }
180
181    #[test]
182    fn reconcile_reconcilable() {
183        let a = register_perspective("p1", "a", "climate change is real and dangerous");
184        let b = register_perspective("p2", "b", "climate change is real and concerning");
185        let result = reconcile_perspectives(&a, &b);
186        assert!(result.reconcilable);
187        assert!(!result.common_ground.is_empty());
188    }
189
190    #[test]
191    fn reconcile_not_reconcilable() {
192        let a = register_perspective("p1", "a", "the sky is blue today");
193        let b = register_perspective("p2", "b", "economic policy reform needed");
194        let result = reconcile_perspectives(&a, &b);
195        assert!(!result.reconcilable);
196    }
197}