*24 Jan 2015*
<div class="posthaven-post-body">
Here's a little something i tried yesterday: a ruby script that mixes and matches verbs, nouns, adjectives, and colors to come up with crazy ass lyrics, red hot style. Enjoy :)
\$nouns = IO.readlines('names.txt') \$adjectives = IO.readlines('adjectives.txt') \$verbs = IO.readlines('verbs.txt') \$adjectives \<\< \['red','blue','green','white','black','yellow','brown','gray'\] \$pronouns = IO.readlines('pronouns.txt') \$adverbs = IO.readlines('adverbs.txt') \$prepositions = IO.readlines('prepositions.txt')
def get_rand(collection) sleep rand / 10000 collection\[rand \* collection.length.to_i - 1\] end
def preposition_phrase "#{get_rand(\$prepositions).strip} the \#{get_rand(\$nouns).strip}" end
def adjectivated_subject_phrase the_part = "the" if rand.to_f \> 0.5 adjective = get_rand(\$adjectives).strip if rand.to_f \> 0.5 return "#{the_part} \#{adjective} \#{get_rand(\$nouns).strip}" if the_part and adjective nil end
def generate_phrase as = adjectivated_subject_phrase adjectivated_subject_part = as.nil? ? get_rand(\$pronouns).strip : as preposition_part = rand.to_f \> 0.5 ? preposition_phrase : "" verb_part = get_rand(\$verbs).strip object_part = rand.to_f \> 0.25 ? get_rand(\$nouns).strip : "" the_part = (rand.to_f \> 0.5 and object_part != "") ? "the" : "" adverb_part = rand.to_f \> 0.75 ? get_rand(\$adverbs).strip : ""
"#{adjectivated_subject_part} \#{preposition_part} \#{verb_part} \#{the_part} \#{object_part} \#{adverb_part}".squeeze(" ").strip end
def generate_lyrics result = "" (1..5).each do first_phrase = generate_phrase new_phrase = generate_phrase while new_phrase\[-2, 2\] != first_phrase\[-2, 2\] or new_phrase.split(" ")\[-1\] == first_phrase.split(" ")\[-1\] do new_phrase = generate_phrase end result \<\< "#{first_phrase}\r\n#{new_phrase}\r\n\r\n" end result end
puts generate_lyrics Here are the sources: rhcp_lyrics_v2.tar. As a bonus there is a script to see how many attempts are required to reach the phrase "The quick brown fox" :)
</div>
[[tags/experiments|experiments]] [[tags/findings|findings]] [[tags/fun|fun]] [[tags/jokes|jokes]] [[tags/software_development|software_development]] [[tags/writing|writing]]