Struct markov_strings::Markov[][src]

pub struct Markov { /* fields omitted */ }
Expand description

The Markov chain generator

  1. Initialize it empty or from saved corpus
  2. Add data to complete the corpus
  3. Generate results

Implementations

Creates an empty Markov instance

use markov_strings::*;

let mut markov = Markov::new();

Creates a Markov instance from previously imported data

See Markov::export() for more information.

Example: load your saved corpus from a flat file with the bincode crate.

let file = File::open("dumped.db").unwrap();
let reader = BufReader::new(file);
let data = bincode::deserialize_from(reader).unwrap();
let mut markov = Markov::from_export(data);

Sets the “state size” of your Markov generator.

The result chain is made up of consecutive blocks of words, and each block is called a state. Each state is itself made up of one (1) or more words.

let data: Vec<InputData> = vec![];
let mut markov = Markov::new();

// We _must_ set the state_size before adding data...
assert!(markov.set_state_size(3).is_ok());

// ...or it will return an error
markov.add_to_corpus(data);
assert!(markov.set_state_size(4).is_err());
  • A state size of 1 word will mostly output non-sense gibberish.
  • A state size of 2 words can produce interesting results, when correctly filtered.
  • A state size of 3 or more words will produce more intelligible results, but you’ll need a source material that will allow it while staying random enough.

! You CANNOT change the state_size once you’ve added data with Markov::add_to_corpus().
The internal data structure is reliant on the state size, and it cannot be changed without rebuilding the whole corpus.

Default value 2.

Adds data to your Markov instance’s corpus.

This is an expensive method that can take a few seconds, depending on the size of your input data. For example, adding 50.000 tweets while running on fairly decent computer takes more than 20 seconds.

To avoid rebuilding the corpus each time you want to generate a text, you can use Markov::export() and Markov::from_export()

You can call .add_to_corpus() as many times as you need it.

Sets a filter to ensure that outputted results match your own criteria.

A good filter is essential to get interesting results out of Markov::generate(). The values you should check at minimum are the MarkovResult.score and MarkovResult.refs’ length.

The higher these values, the “better” the results. The actual thresholds are entierely dependant of your source material.

let mut markov = Markov::new();
// We're going to generate tweets, so...
markov
    .set_filter(|r| {
        // Minimum score and number of references
        // to ensure good randomness
        r.score > 50 && r.refs.len() > 10
            // Max length of a tweet
            && r.text.len() <= 280
            // No mentions
            && !r.text.contains("@")
            // No urls
            && !r.text.contains("http")
  });

Removes the filter, if any

let mut markov = Markov::new();
// Those two lines a functionally identical.
markov.set_filter(|r| true);
markov.unset_filter();

Sets the maximum number of times the generator will try to generate a result.

If Markov::generate fails [max_tries] times to generate a sentence, it returns an ErrorType.TriesExceeded.

Default value: 100

Generates a random result from your corpus.

let mut markov = Markov::new();
let data: Vec<InputData> = vec![/* lots of data */];
markov.add_to_corpus(data);
let result = markov.generate().unwrap();

Gets an item from the original data.

Use this with the indices from MarkovResult.refs

let data: Vec<InputData> = vec![
  InputData{ text: "foo bar lorem ipsum".to_string(), meta: Some("something".to_string()) },
];
let mut markov = Markov::new();
markov.add_to_corpus(data);
let result = markov.generate().unwrap();

// Since we only have 1 string in our corpus, we have 1 ref...
let mut expected: Vec<usize> = vec![];
expected.push(0);
assert_eq!(result.refs, expected);
let input_ref = *result.refs.get(0).unwrap();
assert_eq!(markov.get_input_ref(input_ref).unwrap().text, "foo bar lorem ipsum");
assert_eq!(markov.get_input_ref(input_ref).unwrap().meta, Some("something".to_string()));

Exports the corpus into a serializable structure.

The Markov::add_to_corpus() method being expensive, you may want to build your corpus once, then export it to a serializable file file for later use.

let data: Vec<InputData> = vec![];
let mut markov = Markov::new();
markov.add_to_corpus(data);
let export = markov.export();

let markov = Markov::from_export(export);
let result = markov.generate();

Trait Implementations

Deserialize this value from the given Serde deserializer. Read more

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.