Skip to main content

aprender_shell/
model.rs

1//! N-gram Markov model for command prediction
2//!
3//! Uses the .apr binary format for efficient model persistence.
4
5use aprender::format::{self, ModelType, SaveOptions};
6use aprender::metrics::ranking::RankingMetrics;
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9use std::path::Path;
10
11use crate::trie::Trie;
12
13/// N-gram Markov model for shell command prediction
14#[derive(Serialize, Deserialize)]
15pub struct MarkovModel {
16    /// N-gram size
17    n: usize,
18    /// N-gram counts: context -> (next_token -> count)
19    ngrams: HashMap<String, HashMap<String, u32>>,
20    /// Command frequency
21    command_freq: HashMap<String, u32>,
22    /// Prefix trie for fast lookup
23    #[serde(skip)]
24    trie: Option<Trie>,
25    /// Total commands trained on
26    total_commands: usize,
27    /// Last trained position in history (for incremental updates)
28    #[serde(default)]
29    last_trained_pos: usize,
30}
31
32include!("model_markov.rs");
33include!("validation_result.rs");
34include!("model_encrypted_roundtrip.rs");
35include!("model_corpus_path_load.rs");