qualia_core_db/research/
assessment.rs1use std::collections::BTreeMap;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum EpistemicMode {
8 Empirical,
9 Theoretical,
10 Speculative,
11 Fictional,
12 Hypothetical,
13}
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum RealityCategory {
18 Factual,
19 Fictional,
20 Blended,
21 Uncertain,
22 Deceptive,
23}
24
25impl RealityCategory {
26 pub fn as_str(&self) -> &'static str {
28 match self {
29 Self::Factual => "factual",
30 Self::Fictional => "fictional",
31 Self::Blended => "blended",
32 Self::Uncertain => "uncertain",
33 Self::Deceptive => "deceptive",
34 }
35 }
36}
37
38#[derive(Debug, Clone)]
40pub struct EpistemicAssessment {
41 pub id: String,
42 pub content_ref: String,
43 pub mode: EpistemicMode,
44 pub reality_category: RealityCategory,
45 pub spatio_temporal_context: Option<String>,
46 pub social_context: Option<String>,
47 pub grounding_score: f64,
48 pub metadata: BTreeMap<String, String>,
49}
50
51impl EpistemicAssessment {
52 pub fn new(id: &str, content_ref: &str) -> Self {
54 Self {
55 id: id.to_string(),
56 content_ref: content_ref.to_string(),
57 mode: EpistemicMode::Empirical,
58 reality_category: RealityCategory::Uncertain,
59 spatio_temporal_context: None,
60 social_context: None,
61 grounding_score: 0.0,
62 metadata: BTreeMap::new(),
63 }
64 }
65
66 pub fn set_epistemic_mode(&mut self, mode: EpistemicMode) {
68 self.mode = mode;
69 }
70
71 pub fn set_reality_category(&mut self, category: RealityCategory) {
73 self.reality_category = category;
74 }
75
76 pub fn set_spatio_temporal_context(&mut self, context: &str) {
78 self.spatio_temporal_context = Some(context.to_string());
79 }
80
81 pub fn set_social_context(&mut self, context: &str) {
83 self.social_context = Some(context.to_string());
84 }
85
86 pub fn classify_reality(content: &str) -> RealityCategory {
88 let lower = content.to_lowercase();
89 let fiction_markers = [
90 "novel",
91 "story",
92 "fiction",
93 "narrative",
94 "character",
95 "plot",
96 ];
97 let factual_markers = [
98 "study", "research", "data", "evidence", "measured", "observed",
99 ];
100 let deceptive_markers = ["allegedly", "supposedly", "claim", "unverified"];
101
102 let fiction_count = fiction_markers
103 .iter()
104 .filter(|m| lower.contains(*m))
105 .count();
106 let factual_count = factual_markers
107 .iter()
108 .filter(|m| lower.contains(*m))
109 .count();
110 let deceptive_count = deceptive_markers
111 .iter()
112 .filter(|m| lower.contains(*m))
113 .count();
114
115 if deceptive_count > 0 && factual_count == 0 {
116 RealityCategory::Deceptive
117 } else if fiction_count > 0 && factual_count > 0 {
118 RealityCategory::Blended
119 } else if fiction_count > 0 {
120 RealityCategory::Fictional
121 } else if factual_count > 0 {
122 RealityCategory::Factual
123 } else {
124 RealityCategory::Uncertain
125 }
126 }
127
128 pub fn detect_blended_content(content: &str) -> bool {
130 Self::classify_reality(content) == RealityCategory::Blended
131 }
132
133 pub fn detect_deceptive_fiction(content: &str, claimed_category: RealityCategory) -> bool {
135 let actual = Self::classify_reality(content);
136 claimed_category == RealityCategory::Factual && actual == RealityCategory::Fictional
137 }
138
139 pub fn trace_fiction_to_reality(
141 fiction_content: &str,
142 reality_corpus: &[String],
143 ) -> Vec<String> {
144 let fiction_words: Vec<&str> = fiction_content.split_whitespace().collect();
145 let mut traces = Vec::new();
146 for corpus_item in reality_corpus {
147 let corpus_lower = corpus_item.to_lowercase();
148 let overlap = fiction_words
149 .iter()
150 .filter(|w| corpus_lower.contains(&w.to_lowercase()))
151 .count();
152 if overlap > 3 {
153 traces.push(corpus_item.clone());
154 }
155 }
156 traces
157 }
158}
159
160#[cfg(test)]
161mod tests {
162 use super::*;
163
164 #[test]
165 fn assessment_creation() {
166 let a = EpistemicAssessment::new("a1", "content_ref_1");
167 assert_eq!(a.mode, EpistemicMode::Empirical);
168 assert_eq!(a.reality_category, RealityCategory::Uncertain);
169 }
170
171 #[test]
172 fn classify_factual() {
173 let cat = EpistemicAssessment::classify_reality(
174 "The study measured evidence from observed data.",
175 );
176 assert_eq!(cat, RealityCategory::Factual);
177 }
178
179 #[test]
180 fn classify_fictional() {
181 let cat = EpistemicAssessment::classify_reality(
182 "The novel tells a story about a character in a plot.",
183 );
184 assert_eq!(cat, RealityCategory::Fictional);
185 }
186
187 #[test]
188 fn classify_blended() {
189 let cat = EpistemicAssessment::classify_reality(
190 "The study research measured data about a novel story character.",
191 );
192 assert_eq!(cat, RealityCategory::Blended);
193 }
194
195 #[test]
196 fn classify_deceptive() {
197 let cat =
198 EpistemicAssessment::classify_reality("The allegedly true events supposedly happened.");
199 assert_eq!(cat, RealityCategory::Deceptive);
200 }
201
202 #[test]
203 fn detect_deceptive_fiction() {
204 let content = "The novel tells a story about a character.";
205 assert!(EpistemicAssessment::detect_deceptive_fiction(
206 content,
207 RealityCategory::Factual
208 ));
209 }
210
211 #[test]
212 fn trace_fiction_to_reality() {
213 let fiction = "The climate study measured temperature data";
214 let corpus = vec![
215 "The climate study was conducted in 2024 with temperature data".to_string(),
216 "Unrelated content about cooking".to_string(),
217 ];
218 let traces = EpistemicAssessment::trace_fiction_to_reality(fiction, &corpus);
219 assert!(!traces.is_empty());
220 }
221
222 #[test]
223 fn set_contexts() {
224 let mut a = EpistemicAssessment::new("a1", "ref");
225 a.set_spatio_temporal_context("2024, London");
226 a.set_social_context("Academic community");
227 assert!(a.spatio_temporal_context.is_some());
228 assert!(a.social_context.is_some());
229 }
230}