name_gen.pl 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. # Read in strings from a file and generate
  5. # an interesting song/album/artist name
  6. # File to get strings from
  7. my $seed_file = $ARGV[0];
  8. my @data = split("\n", `cat $seed_file`);
  9. # Length of random name (in words)
  10. my $length = int(1 + rand(4 - 1));
  11. my @words;
  12. sub word_from_seed() {
  13. my $word = $data[ rand @data ];
  14. chomp $word;
  15. return $word;
  16. }
  17. sub get_word {
  18. my $tmp_word = word_from_seed();
  19. chomp $tmp_word;
  20. my $final = "";
  21. if ( $tmp_word =~ m/ / ) {
  22. my @strings = split(" ", $tmp_word);
  23. my $string = $strings[ rand @strings ];
  24. $final = $string;
  25. } elsif ( $tmp_word =~ m/\./ ) {
  26. my @strings = split(".", $tmp_word);
  27. my $string = $strings[ rand @strings ];
  28. $final = $string;
  29. } else {
  30. $final = $tmp_word;
  31. }
  32. # Chance to splice
  33. my $flip = 1 + int(rand(2));
  34. if ( $flip == 1 || $length == 1 ) {
  35. # Get word, take the first 3 chars,
  36. # save in $splice_word
  37. my $splice_word = word_from_seed();
  38. if ( length $splice_word >= 3 ) {
  39. if ( $splice_word =~ m/(^.{3})/ ) {
  40. $splice_word = $1;
  41. }
  42. } else {
  43. $splice_word =~ m/(^.{1})/;
  44. $splice_word = $1;
  45. }
  46. # Get final word, take last $rand chars,
  47. # add to splice_word
  48. if ( ! defined $final || $final eq "" ) {
  49. $final = word_from_seed();
  50. }
  51. my $splice_char_num = 1 + int(rand(length $final));
  52. if ( $splice_char_num < 1 ) {
  53. $splice_char_num = 1;
  54. }
  55. $final =~ m/(.{$splice_char_num}$)/;
  56. my $splice_word_2 = $1;
  57. $final = $splice_word . $splice_word_2;
  58. }
  59. return $final;
  60. }
  61. if ( defined $ARGV[1] && $ARGV[1] eq "--gen-seed" ) {
  62. my @seed_words = ();
  63. foreach my $i ( 1..10000 ) {
  64. my $word = get_word();
  65. push(@seed_words, $word);
  66. }
  67. my $word_list = join("\n", @seed_words);
  68. open(my $fh, '>', "generated_seed.txt");
  69. print $fh $word_list;
  70. close $fh;
  71. my $sorted_list = `cat generated_seed.txt | sort | uniq`;
  72. open(my $fh2, '>', "generated_seed.txt");
  73. print $fh2 $sorted_list;
  74. close $fh2;
  75. exit 0;
  76. }
  77. foreach my $word ( 1..$length ) {
  78. push(@words, get_word());
  79. }
  80. my $output = "";
  81. foreach my $word (@words){
  82. if ( ! defined $word || $word eq "") {
  83. next;
  84. }
  85. $output = $output . $word . " ";
  86. }
  87. print "$output\n";