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