ruby rhyme generator

Nikolaus Gradwohl2012-07-08T09:43:04+02:00

I made a ruby rhyme generator - a simple commandline utility to generate nonsense rhymes like this

    si ninu si - na ma boto
    mo sito mo - de sa tato

    te dini ma - no ne dibi
    di nodi te - be be sebi

or this

    bo nibe bomeku
    te dibu domiku

    nu sibe keteso
    me tiba beneso

you can specify the number of verses and the rhythmic pattern on the commandline

Usage: rhyme [options]
    -p, --pattern PATTERN            the rythmic pattern
    -c, --count COUNT                number of verses

the default pattern is 121-112 like in the first example

here is the source-code if you want to generate some rhymes yourself

rhyme.rb

require 'optparse'

options={}

optparse = OptionParser.new do|opts|
    options[:pattern] = '121-112'
    opts.on( '-p', '--pattern PATTERN', 'the rythmic pattern' ) do |pattern| 
        options[:pattern] = pattern
    end
    options[:count]=4
    opts.on( '-c', '--count COUNT', 'number of verses' ) do |count| 
        options[:count] = count.to_i
    end
end
optparse.parse!

s = 'bdtmnsk'
v = 'aeiou'

def randl( arr )
    return arr[rand(arr.length)]
end

def rhythm( row, pattern ) 
    res = ''
    first = true
    idx = 0;
    pattern.each_char { |p| 
        if first then first = false else res += ' ' end
        if '1234567890'[p] then 
            (0..(p.to_i-1)).each {
                res += row[idx];
                idx+=1    
            }    
        else 
            res += p
        end
    }
    return res
end

pattern = options[:pattern]
sum = 0
pattern.each_char{ |l| sum+=l.to_i }
match_one = pattern[-1].to_i == 1

(1..options[:count]).each {
    last = match_one ? randl(v) : "#{randl(s)}#{randl(v)}" 
    (1..2).each{
        row = []
        (1..sum-1).each{ 
            row<< "#{randl(s)}#{randl(v)}" 
        }
        row << (match_one ? "#{randl(s)}#{last}" : last)
        puts rhythm( row, pattern )
    }
    puts 
}
Tweet This! submit to reddit Digg!   Tags: | 1 comments | no trackbacks

See also:

firefighting tools
Synthesizer dreaming
anykey skywalker
Robo-Ghost
Permission to Smile

Trackbacks

Comments

Leave a response

  1. wizzarde 2012-07-29T06:53:48+02:00

    Wenn das nicht geil ist....

Leave a comment