Skip to content

Commit

Permalink
replace printlns with real logging + move main.rs under 'examples'
Browse files Browse the repository at this point in the history
  • Loading branch information
joeclark-phd committed Aug 7, 2024
1 parent effa9b2 commit 3e44ca0
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 8 deletions.
223 changes: 222 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
[package]
edition = "2021"
name = "multimarkov"
version = "0.5.0"
version = "1.0.0"
authors = ["joeclark-phd <[email protected]>"]
description = """
This is a generic tool for training and using multi-order Markov chains for procedural generation applications
Expand All @@ -22,6 +22,12 @@ readme = "README.md"
license = "MIT"
repository = "https://github.com/joeclark-phd/multimarkov"

[dependencies]
log = "0.4.22"

[dependencies.rand]
version = "0.8.5"
features = [ "small_rng" ]

[dev-dependencies]
env_logger = "0.11.5"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ is much more likely to draw `'f'` because it has trained a model for what comes

## Release notes:

**1.0.0**: Replaced `println`s with logging using the `log` crate. Added logging of the number of known states and trained sequences within the `add_priors` function on `MultiMarkovBuilder`. It turns out that that step can really explode if you have a large dataset, so this logging may be helpful downstream. Also: moved the binary target (`main.rs`) into the "examples" directory. Run it with `cargo run --example main`.

0.5.0: MultiMarkov now implements `Debug`

0.4.0: The addition of a method `with_rng()` on the builder allows you to add a custom RNG, for example, if you want to use a random number seed. Thanks RicardRC.
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs → examples/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
mod builder;

use multimarkov::MultiMarkov;
use rand::{rngs::SmallRng, SeedableRng};
use std::fs::File;
use std::io::{BufRead, BufReader};

fn main() {

// initialize logging
env_logger::builder().filter_level(log::LevelFilter::Debug).init();


let file = File::open("resources/romans.txt").unwrap();
let reader = BufReader::new(file);
let lines = reader
Expand Down
11 changes: 7 additions & 4 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::MultiMarkov;
use log::{debug, info};
use rand::{thread_rng, RngCore};
use std::cmp::max;
use std::collections::{BTreeMap, HashMap, HashSet};
Expand Down Expand Up @@ -80,8 +81,8 @@ where
Err(_) => error_count += 1,
};
}
println!(
"{} sequences successfully trained; {} errors",
debug!(
"{} sequences successfully trained; {} errors.",
success_count, error_count
);
self
Expand All @@ -90,7 +91,7 @@ where
/// Learn all the transitions possible from one training sequence, adding observations to the Markov model.
fn train_sequence(&mut self, sequence: Vec<T>) -> Result<(), &str> {
if sequence.len() < 2 {
return Err("sequence was too short, must contain at least two states");
return Err("Sequence was too short, must contain at least two states.");
}

// loop backwards through the characters in the sequence
Expand Down Expand Up @@ -141,13 +142,15 @@ where
/// Should be called after training is complete, because only then do we know the full set of
/// known states, and which transitions are unobserved.
fn add_priors(&mut self) {
let mut num_priors_added: usize = 0;
match self.prior {
Some(p) => {
for v in self.markov_chain.values_mut() {
for a in self.known_states.iter() {
v.entry(a.clone()).or_insert(p);
v.entry(a.clone()).or_insert_with(|| {num_priors_added+=1; p});
}
}
info!("Model has {} known states and {} trained sequences. {} priors added.",self.markov_chain.len(),self.known_states.len(),num_priors_added);
}
None => (),
}
Expand Down

0 comments on commit 3e44ca0

Please sign in to comment.