Advertisement
my_hat_stinks

namegen.py

Apr 10th, 2021 (edited)
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.07 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. """
  3. Random Name Generator.
  4.  
  5. Combines characters to create a reasonably pronouncable name.
  6. """
  7.  
  8. import random
  9. import sys
  10.  
  11. vowels = ["a","e","i","o","u","y"]
  12. consonants = [
  13.     # Simple
  14.     "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z",
  15.     # Compund
  16.     "sh", "ch", "th"
  17. ]
  18.  
  19. def generate_name():
  20.     name_length = random.randint(3,10)
  21.     new_name = ""
  22.    
  23.     last_was_vowel = False
  24.     last_space = 0
  25.     force_vowel = False
  26.     force_consonant = False
  27.    
  28.     for i in range(name_length):
  29.         is_vowel = False if force_consonant else (force_vowel or (random.randint(1,2)==1))
  30.        
  31.         # Generate Space
  32.         if i>(last_space+2) and i<(name_length-2):
  33.             if random.randint(1,3)==1:
  34.                 new_name = new_name + " "
  35.                 last_space = i
  36.                
  37.                 force_vowel = False
  38.                 force_consonant = False
  39.        
  40.         # Select next character
  41.         next_char_list = vowels if is_vowel else consonants
  42.         new_char = next_char_list[ random.randint(0,len(next_char_list)-1) ]
  43.        
  44.         # Upper-case if appropriate
  45.         if (last_space==i):
  46.             if len(new_char)<=1:
  47.                 new_char = new_char.upper()
  48.             else:
  49.                 new_char = new_char[0].upper() + new_char[1:]
  50.        
  51.         new_name = new_name + new_char
  52.        
  53.         # Set up flags for next iteration
  54.         force_vowel = (not is_vowel) and (not last_was_vowel)
  55.         force_consonant = is_vowel and last_was_vowel
  56.        
  57.         last_was_vowel = is_vowel
  58.    
  59.     return new_name
  60.  
  61. if __name__ == '__main__':
  62.     name_count = 30
  63.    
  64.     try:
  65.         name_count = int(sys.argv[1])
  66.     except (ValueError,IndexError) as e:
  67.         pass
  68.    
  69.     if name_count<0:
  70.         name_count = 0
  71.    
  72.     print( "\nGenerating Names" )
  73.     print( "----------------" )
  74.     for i in range(name_count):
  75.         print( generate_name() )
  76.    
  77.     print( f"\nNames generated: {name_count}\n" )
  78.     pass
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement