marqant 1.0.0

Quantum-compressed markdown format for AI consumption with 90% token reduction
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! # Angels & Demons: The Duality of Compression
//!
//! This module implements the Angel Decompressor with blessing levels.
//!
//! ## Philosophy
//!
//! - **DEMONS** compress data by finding patterns and removing redundancy
//! - **ANGELS** decompress with divine interpretation, adding blessed variations
//!
//! ## Thermodynamic Poetry
//!
//! ```text
//! Demons sort the chaos, reducing entropy's reign
//! Angels bless the output, adding variance again
//! Together they create a cycle, neither good nor bad
//! Just information dancing, making Maxwell glad
//! ```
//!
//! ## Blessing Levels
//!
//! - **Level 0 (STRICT)**: Bit-perfect reconstruction (for Hutter Prize)
//! - **Level 1 (MINOR_BLESSINGS)**: Fix typos, spacing, obvious errors
//! - **Level 2 (HARMONY)**: Wikipedia structure fixes, template harmonization
//! - **Level 3 (CREATIVE)**: Training data augmentation, semantic variations

use anyhow::Result;
use std::collections::HashMap;

/// Boltzmann constant in J/K
const K_BOLTZMANN: f64 = 1.380649e-23;

/// Temperature in Kelvin (room temperature)
const TEMPERATURE: f64 = 293.15;

/// Energy per bit in joules: kT * ln(2)
fn energy_per_bit() -> f64 {
    K_BOLTZMANN * TEMPERATURE * 2_f64.ln()
}

/// Blessing levels for angel decompression
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BlessingLevel {
    /// Level 0: Bit-perfect reconstruction (no blessings)
    Strict = 0,

    /// Level 1: Minor fixes (typos, spacing)
    MinorBlessings = 1,

    /// Level 2: Harmony (Wikipedia structure fixes)
    Harmony = 2,

    /// Level 3: Creative (training data augmentation)
    Creative = 3,
}

impl BlessingLevel {
    /// Parse blessing level from integer
    pub fn from_i32(level: i32) -> Result<Self> {
        match level {
            0 => Ok(BlessingLevel::Strict),
            1 => Ok(BlessingLevel::MinorBlessings),
            2 => Ok(BlessingLevel::Harmony),
            3 => Ok(BlessingLevel::Creative),
            _ => Err(anyhow::anyhow!(
                "Invalid blessing level: {}. Must be 0-3.",
                level
            )),
        }
    }

    /// Get human-readable name
    pub fn name(&self) -> &'static str {
        match self {
            BlessingLevel::Strict => "STRICT",
            BlessingLevel::MinorBlessings => "MINOR_BLESSINGS",
            BlessingLevel::Harmony => "HARMONY",
            BlessingLevel::Creative => "CREATIVE",
        }
    }

    /// Get description
    pub fn description(&self) -> &'static str {
        match self {
            BlessingLevel::Strict => "Bit-perfect reconstruction (for Hutter Prize)",
            BlessingLevel::MinorBlessings => "Fix typos, spacing, obvious errors",
            BlessingLevel::Harmony => "Wikipedia structure fixes, template harmonization",
            BlessingLevel::Creative => "Training data augmentation, semantic variations",
        }
    }
}

/// Statistics about the blessing process
#[derive(Debug, Default)]
pub struct BlessingStats {
    /// Original length in bytes
    pub original_length: usize,

    /// Blessed length in bytes
    pub blessed_length: usize,

    /// Number of blessings applied
    pub blessings_applied: usize,

    /// Entropy added (in bits)
    pub entropy_added: f64,

    /// Energy added (in joules)
    pub energy_added: f64,
}

impl BlessingStats {
    /// Calculate thermodynamic properties
    pub fn calculate_thermodynamics(&mut self) {
        // Each blessing adds approximately 1 bit of entropy
        self.entropy_added = self.blessings_applied as f64;

        // Energy = entropy * kT * ln(2)
        self.energy_added = self.entropy_added * energy_per_bit();
    }

    /// Get size change
    pub fn size_delta(&self) -> i64 {
        self.blessed_length as i64 - self.original_length as i64
    }
}

/// The Angel Decompressor
pub struct Angel {
    /// Blessing level to apply
    level: BlessingLevel,

    /// Typo correction dictionary (for Level 1+)
    typo_dict: HashMap<String, String>,

    /// Random seed for creative mode
    seed: u64,
}

impl Angel {
    /// Create a new Angel with specified blessing level
    pub fn new(level: BlessingLevel) -> Self {
        Self {
            level,
            typo_dict: Self::create_typo_dictionary(),
            seed: 0,
        }
    }

    /// Create with custom seed (for reproducible creative mode)
    pub fn with_seed(level: BlessingLevel, seed: u64) -> Self {
        Self {
            level,
            typo_dict: Self::create_typo_dictionary(),
            seed,
        }
    }

    /// Create the typo correction dictionary
    fn create_typo_dictionary() -> HashMap<String, String> {
        let mut dict = HashMap::new();

        // Common typos
        dict.insert("teh".to_string(), "the".to_string());
        dict.insert("recieve".to_string(), "receive".to_string());
        dict.insert("occured".to_string(), "occurred".to_string());
        dict.insert("seperate".to_string(), "separate".to_string());
        dict.insert("definately".to_string(), "definitely".to_string());
        dict.insert("wierd".to_string(), "weird".to_string());
        dict.insert("accomodate".to_string(), "accommodate".to_string());
        dict.insert("beleive".to_string(), "believe".to_string());

        dict
    }

    /// Apply blessings to decompressed text
    pub fn bless(&self, text: &str) -> Result<(String, BlessingStats)> {
        let mut stats = BlessingStats {
            original_length: text.len(),
            ..Default::default()
        };

        let blessed = match self.level {
            BlessingLevel::Strict => {
                // No blessings - return as-is
                text.to_string()
            }
            BlessingLevel::MinorBlessings => self.apply_minor_blessings(text, &mut stats),
            BlessingLevel::Harmony => {
                // Apply minor blessings first, then harmony
                let minor = self.apply_minor_blessings(text, &mut stats);
                self.apply_harmony_blessings(&minor, &mut stats)
            }
            BlessingLevel::Creative => {
                // Apply all previous levels, then creative
                let minor = self.apply_minor_blessings(text, &mut stats);
                let harmony = self.apply_harmony_blessings(&minor, &mut stats);
                self.apply_creative_blessings(&harmony, &mut stats)
            }
        };

        stats.blessed_length = blessed.len();
        stats.calculate_thermodynamics();

        Ok((blessed, stats))
    }

    /// Apply Level 1: Minor blessings (typos, spacing)
    fn apply_minor_blessings(&self, text: &str, stats: &mut BlessingStats) -> String {
        let mut result = text.to_string();

        // Fix double spaces -> single space
        let double_space_count = result.matches("  ").count();
        result = result.replace("  ", " ");
        if double_space_count > 0 {
            stats.blessings_applied += double_space_count;
        }

        // Fix common typos
        for (typo, correct) in &self.typo_dict {
            let before = result.clone();
            result = result.replace(typo, correct);
            if result != before {
                stats.blessings_applied += 1;
            }
        }

        // Fix triple+ newlines -> double newline
        while result.contains("\n\n\n") {
            result = result.replace("\n\n\n", "\n\n");
            stats.blessings_applied += 1;
        }

        // Fix space before punctuation
        let before = result.clone();
        result = result.replace(" .", ".");
        if result != before {
            stats.blessings_applied += 1;
        }
        let before = result.clone();
        result = result.replace(" ,", ",");
        if result != before {
            stats.blessings_applied += 1;
        }
        let before = result.clone();
        result = result.replace(" !", "!");
        if result != before {
            stats.blessings_applied += 1;
        }
        let before = result.clone();
        result = result.replace(" ?", "?");
        if result != before {
            stats.blessings_applied += 1;
        }

        result
    }

    /// Apply Level 2: Harmony blessings (Wikipedia structure fixes)
    fn apply_harmony_blessings(&self, text: &str, stats: &mut BlessingStats) -> String {
        let mut result = text.to_string();

        // Fix Wikipedia category capitalization
        if result.contains("[[category:") {
            result = result.replace("[[category:", "[[Category:");
            stats.blessings_applied += 1;
        }

        if result.contains("[[CATEGORY:") {
            result = result.replace("[[CATEGORY:", "[[Category:");
            stats.blessings_applied += 1;
        }

        // Fix template capitalization
        if result.contains("{{template ") {
            result = result.replace("{{template ", "{{Template ");
            stats.blessings_applied += 1;
        }

        // Fix broken wikilinks [[link ]] -> [[link]]
        let prev_result = result.clone();
        result = result.replace("]] ", "]]");
        if result != prev_result {
            stats.blessings_applied += 1;
        }
        let prev_result = result.clone();
        result = result.replace(" [[", "[[");
        if result != prev_result {
            stats.blessings_applied += 1;
        }

        // Fix heading spacing: ensure single space after #
        for i in 1..=6 {
            let wrong = format!("{}  ", "#".repeat(i));
            let right = format!("{} ", "#".repeat(i));
            if result.contains(&wrong) {
                result = result.replace(&wrong, &right);
                stats.blessings_applied += 1;
            }
        }

        // Fix list formatting
        result = result.replace("\n*", "\n* "); // Ensure space after bullet
        result = result.replace("\n-", "\n- "); // Ensure space after dash

        result
    }

    /// Apply Level 3: Creative blessings (training data augmentation)
    fn apply_creative_blessings(&self, text: &str, stats: &mut BlessingStats) -> String {
        let mut result = text.to_string();

        // Simple pseudo-random number generator based on seed
        let mut rng = self.seed;
        let mut next_random = || {
            rng = (rng.wrapping_mul(1103515245).wrapping_add(12345)) % (1 << 31);
            rng
        };

        // Add semantic variations (simple version)
        // In a full implementation, this would use ML models

        let variations = vec![
            ("is a", "is an example of"),
            ("the", "this"),
            ("and", "as well as"),
            ("but", "however"),
            ("also", "additionally"),
        ];

        // Apply random variations (about 5% of the time)
        for (from, to) in variations {
            if next_random() % 100 < 5
                && result.contains(from) {
                    // Replace first occurrence
                    result = result.replacen(from, to, 1);
                    stats.blessings_applied += 1;
                }
        }

        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_blessing_level_parsing() {
        assert_eq!(BlessingLevel::from_i32(0).unwrap(), BlessingLevel::Strict);
        assert_eq!(
            BlessingLevel::from_i32(1).unwrap(),
            BlessingLevel::MinorBlessings
        );
        assert_eq!(BlessingLevel::from_i32(2).unwrap(), BlessingLevel::Harmony);
        assert_eq!(BlessingLevel::from_i32(3).unwrap(), BlessingLevel::Creative);
        assert!(BlessingLevel::from_i32(4).is_err());
    }

    #[test]
    fn test_strict_mode() {
        let angel = Angel::new(BlessingLevel::Strict);
        let input = "This  has  double  spaces";
        let (output, stats) = angel.bless(input).unwrap();

        // Strict mode doesn't change anything
        assert_eq!(output, input);
        assert_eq!(stats.blessings_applied, 0);
    }

    #[test]
    fn test_minor_blessings() {
        let angel = Angel::new(BlessingLevel::MinorBlessings);
        let input = "This  has  double  spaces and teh is a typo";
        let (output, stats) = angel.bless(input).unwrap();

        // Should fix spacing and typo
        assert!(output.contains("the is a typo"));
        assert!(!output.contains("  "));
        assert!(stats.blessings_applied > 0);
    }

    #[test]
    fn test_harmony_blessings() {
        let angel = Angel::new(BlessingLevel::Harmony);
        let input = "[[category:test]] and [[CATEGORY:other]]";
        let (output, stats) = angel.bless(input).unwrap();

        // Should fix category capitalization
        assert!(output.contains("[[Category:test]]"));
        assert!(output.contains("[[Category:other]]"));
        assert!(stats.blessings_applied >= 2);
    }

    #[test]
    fn test_thermodynamics() {
        let angel = Angel::new(BlessingLevel::MinorBlessings);
        let input = "This  has  typos  and  teh  recieve";
        let (_output, stats) = angel.bless(input).unwrap();

        // Should have positive energy added
        assert!(stats.energy_added > 0.0);
        assert_eq!(stats.entropy_added, stats.blessings_applied as f64);
    }
}