Skip to content

Commit

Permalink
Merge pull request #10 from mgeisler/correct-number-of-words
Browse files Browse the repository at this point in the history
generate_from: generate at most n words
  • Loading branch information
mgeisler authored Jul 2, 2017
2 parents 01941e0 + af05169 commit 85def12
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl<'a> MarkovChain<'a> {
let (mut a, mut b) = from;
let mut sentence = String::from(a) + " " + b;

for _ in 0..n {
for _ in 2..n {
while !self.map.contains_key(&(a, b)) {
let new_key = **rng.choose(&keys).unwrap();
a = new_key.0;
Expand Down Expand Up @@ -163,6 +163,18 @@ mod tests {
assert_eq!(&lipsum(10)[..11], "Lorem ipsum");
}

#[test]
fn two_words_minimum() {
assert_eq!(lipsum(0).split_whitespace().count(), 2);
assert_eq!(lipsum(1).split_whitespace().count(), 2);
assert_eq!(lipsum(2).split_whitespace().count(), 2);
}

#[test]
fn more_than_two_words() {
assert_eq!(lipsum(17).split_whitespace().count(), 17);
}

#[test]
fn empty_chain() {
let chain = MarkovChain::new();
Expand All @@ -173,8 +185,8 @@ mod tests {
fn generate_from() {
let mut chain = MarkovChain::new();
chain.learn("foo bar baz quuz");
assert_eq!(chain.generate_from(1, ("foo", "bar")), "foo bar baz");
assert_eq!(chain.generate_from(1, ("bar", "baz")), "bar baz quuz");
assert_eq!(chain.generate_from(3, ("foo", "bar")), "foo bar baz");
assert_eq!(chain.generate_from(3, ("bar", "baz")), "bar baz quuz");
}

#[test]
Expand All @@ -183,7 +195,7 @@ mod tests {
// point that doesn't exist in the chain.
let mut chain = MarkovChain::new();
chain.learn("foo bar baz");
assert_eq!(chain.generate_from(1, ("xxx", "yyy")), "xxx yyy baz");
assert_eq!(chain.generate_from(3, ("xxx", "yyy")), "xxx yyy baz");
}

#[test]
Expand Down

0 comments on commit 85def12

Please sign in to comment.