-
-
Save apparition47/228af08a6e32e82ce32feff23674d61d to your computer and use it in GitHub Desktop.
Swift Random Word Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Inspired by: http://planetozh.com/blog/2012/10/generate-random-pronouceable-words/ | |
func randomWord(wordLength: Int = 6) -> String { | |
let kCons = 1 | |
let kVows = 2 | |
var cons: [String] = [ | |
// single consonants. Beware of Q, it"s often awkward in words | |
"b", "c", "d", "f", "g", "h", "j", "k", "l", "m", | |
"n", "p", "r", "s", "t", "v", "w", "x", "z", | |
// possible combinations excluding those which cannot start a word | |
"pt", "gl", "gr", "ch", "ph", "ps", "sh", "st", "th", "wh" | |
] | |
// consonant combinations that cannot start a word | |
let cons_cant_start: [String] = [ | |
"ck", "cm", | |
"dr", "ds", | |
"ft", | |
"gh", "gn", | |
"kr", "ks", | |
"ls", "lt", "lr", | |
"mp", "mt", "ms", | |
"ng", "ns", | |
"rd", "rg", "rs", "rt", | |
"ss", | |
"ts", "tch" | |
] | |
var vows : [String] = [ | |
// single vowels | |
"a", "e", "i", "o", "u", "y", | |
// vowel combinations your language allows | |
"ee", "oa", "oo", | |
] | |
// start by vowel or consonant ? | |
var current = (Int(arc4random_uniform(2)) == 1 ? kCons : kVows ); | |
var word : String = "" | |
while ( word.characters.count < wordLength ){ | |
// After first letter, use all consonant combos | |
if word.characters.count == 2 { | |
cons = cons + cons_cant_start | |
} | |
// random sign from either $cons or $vows | |
var rnd: String = ""; | |
var index: Int; | |
if current == kCons { | |
index = Int(arc4random_uniform(UInt32(cons.count))) | |
rnd = cons[index] | |
}else if current == kVows { | |
index = Int(arc4random_uniform(UInt32(vows.count))) | |
rnd = vows[index] | |
} | |
// check if random sign fits in word length | |
var tempWord = "\(word)\(rnd)" | |
if( tempWord.characters.count <= wordLength ) { | |
word = "\(word)\(rnd)" | |
// alternate sounds | |
current = ( current == kCons ) ? kVows : kCons; | |
} | |
} | |
return word | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment