Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save darthschmoo/57f995ee07c7f13e38e6ca3565e527bf to your computer and use it in GitHub Desktop.
Save darthschmoo/57f995ee07c7f13e38e6ca3565e527bf to your computer and use it in GitHub Desktop.
Ruby random title generator
# TODOS: move lists into configuration file, plurals, better finding/replacing of variables
# Not using
class Pluralizer
def initialize
@pluralize = {}
p "ash", "ashes"
p "fox", "foxes"
p "wolf", "wolves"
p "thief", "thieves"
p "city", "cities"
p "ash", "ashes"
end
def plural( s )
if @pluralize.has_key?( s )
@pluralize[s]
else
s + "s"
end
end
def p( s, p )
@pluralize[s] = p
end
end
class TitleGenerator
def initialize
@pluralizer = Pluralizer.new
end
def title( n = 1 )
n.times do
t = pick( templates )
if debugging?
puts "TITLE: #{ fill_in( t ).inspect }"
puts "\n\n---------------------------------------"
else
puts fill_in( t )
end
end
end
def fill_in( template )
puts " TEMPLATE: #{template}" if debugging?
rval = template.split.map{ |s|
if s[0] == "<"
list = lookup( s )
choice = pick( list )
puts " #{s} ---> #{choice}" if debugging?
choice
else
s
end
}.join( " " )
if rval.include?( "<" )
fill_in( rval )
else
rval
end
end
# Look up the items in the list(s) <THING|OTHER_THING|THIRD_THING>
def lookup( s )
key = s[1..-2]
keys = key.split("|")
keys.map{|k|
raise "No such list: #{k.inspect}" unless lists.has_key?(k)
lists[k]
}.flatten
end
def templates
@templates ||= [
"The <S0> of <S1> and <S1>",
"A <ADJ0> <S0> of <ADJ0> <S1>",
"<ADJ0> <S1> and <ADJ0> <S1>",
"The <PLACE> of <ADJ0> <ANIMALS>",
"A <ADJ0> <PERSON>",
"<OBJECT_PHRASE> , <OBJECT_PHRASE>",
"A <S0> of <S1>",
"<S0> of the <PLACE>"
]
end
def pick( list )
list[ rand( list.length) ]
end
def lists
@lists ||= {
"RELATION" => %w(Daughter Son Mother Father),
"RANK" => %w(King Queen Prince Duke Knight),
"OCCUPATION" => %w(Traveler Soldier Prophet Thief),
"RACE" => %w(Ghost Dragon Elf Dwarf Orc),
"MAGICIAN" => %w(Wizard Witch Sorcerer Sorceress),
"PERSON" => %w(<RELATION|RANK|OCCUPATION|RACE|MAGICIAN>),
"PLACE" => %w(City Citadel Palace Land Mountain River Field Empire Kingdom Court Tavern Gate Forge Path Tower Forest Desert Valley Ruins Waters),
"OBJ" => %w(Crown Book Throne Scroll Scepter Blade Sword Dagger),
"S0" => %w(<PERSON|PLACE|OBJ>),
"ANIMALS" => %w(Foxes Wolves Lions Ravens Crows Hawks Horses Dragons Basilisks ),
"ELEMENT" => %w(Fire Water Wind Air Sky Earth Steel Ash Lightning Storm Ice Blood Smoke Bone Glass Fog Embers Iron Night),
"EMOTION" => %w(Joy Sadness Dream Nightmares Sleep Evil),
"S1" => %w(<ANIMALS|ELEMENT|EMOTION>),
"JEWEL" => %w(Diamond Amethyst Ruby Emerald Obsidian),
"ADJ_CHANGE" => %w(Falling Rising Darkening Brightening Rising Waning Ascending Gathering),
"ADJ_ATTRIBUTE" => %w(Beguiling Holy Unholy Corrupted Consecrated Sacred Desolated Living Ethereal),
"ADJ_MISC" => %w(Burning Coming Windswept Endless Towering Forgotten Magical),
"ADJ_ELEMENT" => %w(Fiery Watery Earthen Ashen Stormy Icy Bloody Empty Chaos),
"ADJ_COLOR" => %w(Crimson Red Black Sable Silver Golden Dark),
"ADJ0" => %w(<ADJ_CHANGE|ADJ_ATTRIBUTE|ADJ_MISC|JEWEL|ADJ_ELEMENT|ADJ_COLOR>),
"OBJECT_PHRASE" => ["<ADJ0> <S0>", "<ADJ_COLOR> <PERSON>", "The <PERSON> 's <ADJ0> <PERSON>" ]
}
end
def debugging?
false
end
end
TitleGenerator.new.title( 20 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment