Skip to main content

qualia_core_db/research/
corpus.rs

1//! Corpus management — items, confidence, extraction.
2
3use std::collections::BTreeMap;
4
5/// Confidence level for a corpus item.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum CorpusConfidence {
8    High,
9    Medium,
10    Low,
11    Unverified,
12}
13
14impl CorpusConfidence {
15    /// Numeric confidence in 0..1 for the qualitative level.
16    pub fn as_f64(&self) -> f64 {
17        match self {
18            Self::High => 0.9,
19            Self::Medium => 0.6,
20            Self::Low => 0.3,
21            Self::Unverified => 0.0,
22        }
23    }
24}
25
26/// A corpus item — a document, dataset, or other source.
27#[derive(Debug, Clone)]
28pub struct CorpusItem {
29    pub id: String,
30    pub source_type: String, // "literature", "dataset", "web", "interview"
31    pub title: String,
32    pub content: String,
33    pub confidence: CorpusConfidence,
34    pub metadata: BTreeMap<String, String>,
35    pub extracted_facts: Vec<String>,
36}
37
38impl CorpusItem {
39    /// Creates a corpus item with `source_type`, `title`, `content`, and default confidence.
40    pub fn new(id: &str, source_type: &str, title: &str, content: &str) -> Self {
41        Self {
42            id: id.to_string(),
43            source_type: source_type.to_string(),
44            title: title.to_string(),
45            content: content.to_string(),
46            confidence: CorpusConfidence::Unverified,
47            metadata: BTreeMap::new(),
48            extracted_facts: Vec::new(),
49        }
50    }
51
52    /// Sets the item's confidence level.
53    pub fn set_confidence(&mut self, confidence: CorpusConfidence) {
54        self.confidence = confidence;
55    }
56
57    /// Appends an extracted fact string.
58    pub fn add_fact(&mut self, fact: &str) {
59        self.extracted_facts.push(fact.to_string());
60    }
61
62    /// Sets a `key`/`value` metadata pair.
63    pub fn add_metadata(&mut self, key: &str, value: &str) {
64        self.metadata.insert(key.to_string(), value.to_string());
65    }
66}
67
68/// A research corpus — a collection of items with confidence tracking.
69#[derive(Debug, Clone, Default)]
70pub struct Corpus {
71    pub items: Vec<CorpusItem>,
72}
73
74impl Corpus {
75    /// Creates an empty corpus.
76    pub fn new() -> Self {
77        Self::default()
78    }
79
80    /// Adds `item` to the corpus.
81    pub fn add_item(&mut self, item: CorpusItem) {
82        self.items.push(item);
83    }
84
85    /// Imports a literature item and returns it mutably.
86    pub fn import_literature(&mut self, id: &str, title: &str, content: &str) -> &mut CorpusItem {
87        let item = CorpusItem::new(id, "literature", title, content);
88        self.items.push(item);
89        self.items.last_mut().unwrap()
90    }
91
92    /// Imports a dataset item and returns it mutably.
93    pub fn import_dataset(&mut self, id: &str, title: &str, content: &str) -> &mut CorpusItem {
94        let item = CorpusItem::new(id, "dataset", title, content);
95        self.items.push(item);
96        self.items.last_mut().unwrap()
97    }
98
99    /// Sets confidence on item `item_id`; `false` when unknown.
100    pub fn set_confidence(&mut self, item_id: &str, confidence: CorpusConfidence) -> bool {
101        if let Some(item) = self.items.iter_mut().find(|i| i.id == item_id) {
102            item.set_confidence(confidence);
103            true
104        } else {
105            false
106        }
107    }
108
109    /// Extract simple facts from corpus items matching a keyword.
110    pub fn extract_facts(&self, keyword: &str) -> Vec<(String, String)> {
111        let mut results = Vec::new();
112        let kw_lower = keyword.to_lowercase();
113        for item in &self.items {
114            for sentence in item.content.split('.') {
115                if sentence.to_lowercase().contains(&kw_lower) {
116                    results.push((item.id.clone(), sentence.trim().to_string()));
117                }
118            }
119        }
120        results
121    }
122
123    /// Number of items in the corpus.
124    pub fn len(&self) -> usize {
125        self.items.len()
126    }
127
128    /// `true` when the corpus has no items.
129    pub fn is_empty(&self) -> bool {
130        self.items.is_empty()
131    }
132
133    /// Mean confidence across items (0.0 when empty).
134    pub fn average_confidence(&self) -> f64 {
135        if self.items.is_empty() {
136            return 0.0;
137        }
138        self.items
139            .iter()
140            .map(|i| i.confidence.as_f64())
141            .sum::<f64>()
142            / self.items.len() as f64
143    }
144}
145
146#[cfg(test)]
147mod tests {
148    use super::*;
149
150    #[test]
151    fn corpus_add_item() {
152        let mut corpus = Corpus::new();
153        corpus.add_item(CorpusItem::new(
154            "c1",
155            "literature",
156            "Paper A",
157            "Content here.",
158        ));
159        assert_eq!(corpus.len(), 1);
160    }
161
162    #[test]
163    fn corpus_import_literature() {
164        let mut corpus = Corpus::new();
165        corpus.import_literature("l1", "Title", "Some content about climate.");
166        assert_eq!(corpus.items[0].source_type, "literature");
167    }
168
169    #[test]
170    fn corpus_set_confidence() {
171        let mut corpus = Corpus::new();
172        corpus.import_literature("l1", "T", "C");
173        assert!(corpus.set_confidence("l1", CorpusConfidence::High));
174        assert_eq!(corpus.items[0].confidence, CorpusConfidence::High);
175    }
176
177    #[test]
178    fn corpus_extract_facts() {
179        let mut corpus = Corpus::new();
180        corpus.import_literature("l1", "T", "Climate change is real. The sky is blue.");
181        let facts = corpus.extract_facts("climate");
182        assert_eq!(facts.len(), 1);
183        assert_eq!(facts[0].0, "l1");
184    }
185
186    #[test]
187    fn corpus_average_confidence() {
188        let mut corpus = Corpus::new();
189        corpus.import_literature("l1", "T", "C");
190        corpus.set_confidence("l1", CorpusConfidence::High);
191        corpus.import_literature("l2", "T", "C");
192        corpus.set_confidence("l2", CorpusConfidence::Low);
193        let avg = corpus.average_confidence();
194        assert!((avg - 0.6).abs() < 0.01);
195    }
196}