Skip to content

Instantly share code, notes, and snippets.

@GeoffWilliams
Last active July 23, 2016 12:28
Show Gist options
  • Select an option

  • Save GeoffWilliams/f5488f0a3da12f6f296a5654d1713d84 to your computer and use it in GitHub Desktop.

Select an option

Save GeoffWilliams/f5488f0a3da12f6f296a5654d1713d84 to your computer and use it in GitHub Desktop.
Generate cool names for things that are hard to name from a wordlist. Entries in the wordlist should start with star and a space, one per line. Other notes will be ignored
#!/usr/bin/env python
import random
import re
import argparse
parser = argparse.ArgumentParser(description="Generate names from wordlist")
parser.add_argument('--wordlist', default='wordlist', help='wordlist file to read')
parser.add_argument('--iterations', default=100, type=int, help='how many words to generate?')
parser.add_argument('--words', default=3, type=int, help='how many words to fuse together')
parser.add_argument('--min', default=2, type=int, help='min letters to pick from word')
parser.add_argument('--max', default=4, type=int, help='max letters to pick from word')
parser.add_argument('--no-offset', dest='offset', action='store_false', help='offset pick from start of word?')
parser.add_argument('--cutshut', dest='cutshut', action='store_true', help='cut n shut mode')
parser.set_defaults(offset=True)
parser.set_defaults(cutshut=False)
args = parser.parse_args()
# for reversed min/max just use max
if args.min > args.max:
args.min = args.max
filename = args.wordlist
c = 0
wordlist=[]
# read the file line by line
with open(filename, 'r') as f:
for line in f:
if line.startswith("*"):
word = line.strip()
# eliminate leading spaces and stars
wordlist.append(re.sub('\*\s+', '', word))
c = c+1
print "Loaded {c} words!".format(c=c)
# pick a random element in wordlist
names = {}
for iteration in range(args.iterations):
name = ""
source = []
# always cram 3 words together
for i in range(args.words):
# pick a random word from the wordlist, make sure its not a dup
# use a loop-and-a-half: http://stackoverflow.com/questions/1662161/is-there-a-do-until-in-python
while True:
r = random.randint(0, len(wordlist)-1)
random_word = wordlist[r]
if random_word not in source:
break
# store the source word so we know where we got our final name from
source.append(random_word)
# kill all spaces
unspaced = re.sub('\s+', '', random_word)
# pick random group of letter of varying length. Can only be as long
# as the word we are looking at
l = min(random.randint(args.min, args.max), len(unspaced))
# pick a random offset for the above group
if args.cutshut:
# pick from start, then middle, then end as we build the word
if i==0:
# first word
o=0
elif i==args.words-1:
# last word
o=len(unspaced)-l
else:
# middle word
o=random.randint(1, max(len(unspaced)-l,2))
else:
# 'normal' offset mode
if args.offset:
o = random.randint(0, len(unspaced)-l)
else:
o = 0
sect = unspaced[o:o+l]
name = name+sect
# filter away words with no vowels ;-)
if re.match('.*[aeiou].*', name):
# use name as a key to remove duplicates and then save the stringified
# source words
names[name] = str(source)
for name in names:
print name + " <---- " + names[name]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment