diff options
author | David Phillips <david@sighup.nz> | 2019-10-26 18:03:42 +1300 |
---|---|---|
committer | David Phillips <david@sighup.nz> | 2019-10-26 18:04:53 +1300 |
commit | 2f0feee0d3e654d58fa5b7de3d53930783581605 (patch) | |
tree | ad5dcec423196dfef01a8dfa1378af4a1ba188ae /Plugin | |
parent | bc41a099b3f01c8ad646d2725ea27d99fa37eb87 (diff) | |
download | idalius-2f0feee0d3e654d58fa5b7de3d53930783581605.tar.xz |
Markov: Retry limited times if message isn't long enough
Diffstat (limited to 'Plugin')
-rw-r--r-- | Plugin/Markov.pm | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/Plugin/Markov.pm b/Plugin/Markov.pm index 1733372..e29bb45 100644 --- a/Plugin/Markov.pm +++ b/Plugin/Markov.pm @@ -74,18 +74,24 @@ sub some sub do_markov { - my $word = $_[0]; - $word = some(keys %markov_data) unless $word; - my $message = ""; - my $i = 0; - do { - $i++; - $message .= "$word "; - $word = some(@{$markov_data{$word}}); - } until(not $word or $word eq "" or $i == 1000); - - return if scalar(split / /, $message) <= 2; - return $message; + my $tries = 0; + + while ($tries++ < 10) { + my $word = $_[0]; + $word = some(keys %markov_data) unless $word; + my $message = ""; + my $i = 0; + do { + $i++; + $message .= "$word "; + $word = some(@{$markov_data{$word}}); + } until(not $word or $word eq "" or $i == 1000); + + return $message if scalar(split / /, $message) > 2; + } + + # fallthrough: didn't form a long enough message in enough tries + return; } sub markov_cmd |