[Tool] Namey, a name generator

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
Muris
Party member
Posts: 131
Joined: Fri May 23, 2014 9:18 am

[Tool] Namey, a name generator

Post by Muris »

From time to time I keep needing names for things, and I made a very simple name generator. It still makes bad names, because the rules of the generator aren't all that good, but I figured to share it with others.

I think there would be a lot of improvements on how it generates the name, like defining better sets of rules. For example now it cannot make names like aya, since it only puts 1 or 2 vowels in a row. Another thing that would probably need to be considered would be possible starting / ending letters and letters that can be in middle of name. I am not sure what kind of ruleset would work for determining these, but from what I've tested out, some combinations of letters just feel awkward in middle of names like for example 'j'.

Anyways here is the code:

Code: Select all

-- Namey, a name generator
math.randomseed(os.time()) -- to avoid setting same seed on every launch
local NameGenerator = {}

-- The names are bit odd ones but atleast it gives something.

-- Random rules for creating weird names!
local vowels = "aeiouy"
local consonants = "bcdfghjklmnpqrstvwxz"


local doubleConsonantFirst = "bcdghklmnprstvw"
local doubleConsonantSecond = "dhklmnrst"

local function pickOne(t)
	local pos = math.random( string.len(t) )
	return t:sub( pos, pos )
end

-- Generates some weird names
function NameGenerator:generateName( parts, isFirstLetterCapital )
	local tName = {}
	local pos = 0
	
	local isConsonant = math.random(2) == 1
	for i = 1, parts do
		if isConsonant then
			if math.random(2) == 1 then -- double consonant
				tName[ i ] = pickOne(doubleConsonantFirst) .. pickOne(doubleConsonantSecond)
			else  				
				tName[ i ] = pickOne( consonants )
			end
		else
			tName[i] = ""
			for j = 1, math.random(2) do -- 1 to 2 vowels in a row
				tName[ i ] = tName[ i ] .. pickOne( vowels ) 
			end
		end
		isConsonant = not isConsonant		
	end
	-- capitalize first letter
	if isFirstLetterCapital then 
		tName[1] = string.upper( tName[1]:sub(1,1) ) .. tName[1]:sub(2)
	end
	local sName = table.concat( tName )
	return sName
end

return NameGenerator
And an example of using it:

Code: Select all

local namey = require( "namey" )

local names = {}

local font = love.graphics.newFont( 16 )
local fontHeight = font:getHeight()
function love.load()
	for i = 1, 20 do
		-- names with 2 to 4 parts, 
		-- so the name can be 2 to 8 letter long
		names[i] = namey:generateName( math.random(2,4),true)
	end
end

function love.draw()
	for i = 1, #names do
		love.graphics.print( names[i], 10, i * fontHeight )
	end
	love.graphics.print( "Press R to regenerate names", 10, (#names+1) * fontHeight )
	love.graphics.print( "Press Q / esc to quit", 10, (#names + 2) * fontHeight )
end

function love.keypressed(key)
	if key == "r" then
		love.load()
	elseif key == "escape" or key == "q" then
		love.event.quit()
	end
end
And finally a love file attached.

Edit: Fixed a bug where I used math.random(1) == 1 where math.random(1) returns 1 always, instead of 0 or 1.
Attachments
namey.love
(1.14 KiB) Downloaded 184 times
Last edited by Muris on Mon Jan 19, 2015 12:05 pm, edited 2 times in total.
User avatar
borix134
Prole
Posts: 18
Joined: Sat Aug 10, 2013 5:37 am

Re: [Tool] Namey, a name generator

Post by borix134 »

Very interesting project, perhaps I might use it, as my friend wanted the characters in our current project to have randomized names.
Muris
Party member
Posts: 131
Joined: Fri May 23, 2014 9:18 am

Re: [Tool] Namey, a name generator

Post by Muris »

borix134 wrote:Very interesting project, perhaps I might use it, as my friend wanted the characters in our current project to have randomized names.
Glad to hear that there is at least some interest on this. You probably would want to refine the types of names you do want to use. For example the program still creates letter combinations that do not really go well with each other, like "dg". Personally I was more like thinking of hand picking names out of suggestions rather than use everything it outputs.

Originally I was using a list of possible combinations for double consonant, like:

Code: Select all

local doubleConsonants = { "rd", "th", "ck", "pp", "gg", "pr", "cl", "dr", "rk", "cr", "ll" , "kk"}
Better way to construct a working name would be handwriting a list of possible syllables that the program then randomly picks and uses, but that would be a lot of work.

Also I realised that since there is not much of rules on the names it can generate, it kind of generates names of all languages. For example it could make name like "Tommy" or it could make name like "Rhakm", even though for my "ears" they do not even slightly sound like being remotely same language or even worse something completely gibberish like: "Kdavd"
Muris
Party member
Posts: 131
Joined: Fri May 23, 2014 9:18 am

Re: [Tool] Namey, a name generator

Post by Muris »

I just realised that there is a quite a bug in 2 places of the code. In both places it says math.random(1) == 1... this obviosly is same as if true, since math.random(1) only returns 1 and not 0,1 like what I thoguht it would do. So basically it always started with double consonant, and never used single consonant anywhere.

Edit: Also made it use os.time as a seed value for random, so it wouldn't always pick same names on every start up.
Post Reply

Who is online

Users browsing this forum: No registered users and 63 guests