random_poetry_generator.pl 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 09 February 2017
  5. # https://github.com/trizen
  6. # An experimental random poetry generator.
  7. # usage:
  8. # perl random_poetry_generator.pl [wordlist]
  9. use 5.016;
  10. use strict;
  11. use autodie;
  12. use warnings;
  13. use open IO => ':utf8', ':std';
  14. use List::Util qw(max);
  15. use File::Find qw(find);
  16. @ARGV || die "usage: $0 [wordlists]\n"; # wordlists or directories
  17. my $min_len = 20; # minimum length of each verse
  18. my $ending_len = 3; # rhyme ending length
  19. my $strophe_len = 4; # number of verses in a strophe
  20. #<<<
  21. # Rhymes template
  22. my @template = (
  23. 'A', 'A', 'B', 'B',
  24. 'A', 'B', 'B', 'A',
  25. 'A', 'B', 'A', 'B',
  26. 'B', 'A', 'A', 'B',
  27. );
  28. #>>>
  29. my $max_endings = do {
  30. my %count;
  31. ++$count{$_} for @template;
  32. max(values %count);
  33. };
  34. my %words;
  35. my %seen;
  36. sub collect_words {
  37. my ($file) = @_;
  38. open my $fh, '<', $file;
  39. my $content = do {
  40. local $/;
  41. <$fh>;
  42. };
  43. close $fh;
  44. my @words =
  45. grep { length($_) > $ending_len }
  46. map { CORE::fc(s/^[^\pL]+//r =~ s/[^\pL]+\z//r) }
  47. split(' ', $content);
  48. foreach my $word (@words) {
  49. next if $seen{$word}++;
  50. push @{$words{substr($word, -$ending_len)}}, $word;
  51. }
  52. }
  53. find {
  54. no_chdir => 1,
  55. wanted => sub {
  56. if ((-f $_) and (-T _)) {
  57. collect_words($_);
  58. }
  59. },
  60. } => @ARGV;
  61. my @keys = keys(%words);
  62. my %endings;
  63. my %used_ending;
  64. my %used_word;
  65. my $strofhe_i = 0;
  66. foreach my $r (@template) {
  67. my $ending;
  68. if (exists $endings{$r}) {
  69. $ending = $endings{$r};
  70. }
  71. else {
  72. my $try = 0;
  73. do {
  74. $ending = $keys[rand @keys];
  75. } while (@{$words{$ending}} < $max_endings and !exists($used_ending{$ending}) and ++$try < 1000);
  76. $endings{$r} = $ending;
  77. $used_ending{$ending} = 1;
  78. }
  79. my @row;
  80. for (my $length = 0 ; ;) {
  81. my $word;
  82. my $try = 0;
  83. do {
  84. my $key = ($length > $min_len) ? $ending : $keys[rand @keys];
  85. my $words = $words{$key};
  86. $word = $words->[rand @$words];
  87. } while (exists($used_word{$word}) and ++$try < 1000);
  88. $used_word{$word} = 1;
  89. push @row, $word;
  90. last if $length > $min_len;
  91. $length += length($word) + 1;
  92. }
  93. say "@row";
  94. print "\n" if (++$strofhe_i % $strophe_len == 0);
  95. }