Alyssa

name_generator

Jun 30th, 2016
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 KB | None | 0 0
  1. import math
  2. import random
  3. from itertools import takewhile
  4.  
  5. def accumulate(iterator):
  6.         """Returns a cumulative sum of the elements.
  7.         accumulate([1, 2, 3, 4, 5]) --> 1 3 6 10 15"""
  8.         current = 0
  9.         for value in iterator:
  10.                 current += value
  11.                 yield current
  12.  
  13. def weightedChoice(weights, objects):
  14.         """Return a random item from objects, with the weighting defined by weights
  15.         (which must sum to 1)."""
  16.         limit = random.random()
  17.         return objects[sum(takewhile(bool, (value < limit for value in accumulate(weights))))]
  18.  
  19. letters = ["a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J","k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T","u","U","v","V","w","W","x","X","y","Y","z","Z"]
  20.  
  21. irl = []
  22. ric = []
  23. irlchances = []
  24. words = []
  25. rwc = []
  26. wordchances = []
  27.  
  28. with open("realnames.txt") as f:
  29.     for line in f:
  30.         irl.append(line.rstrip().lower())
  31.  
  32. with open("creal.txt") as f:
  33.     for line in f:
  34.         ric.append(float(line.rstrip()))
  35.  
  36. for val in ric:
  37.     irlchances.append(val * (1/sum(ric)))
  38.  
  39. with open("words.txt") as f:
  40.     for line in f:
  41.         words.append(line.rstrip())
  42.  
  43. with open("cwords.txt") as f:
  44.     for line in f:
  45.         rwc.append(math.sqrt(float(line.rstrip())))
  46.  
  47. for val in rwc:
  48.     wordchances.append(val * (1/sum(rwc)))
  49.  
  50. def gen(max_length=16):
  51.     initial = random.randint(1,2)
  52.     if initial == 1:
  53.         bases = irl
  54.         chances = irlchances
  55.  
  56.     elif initial == 2:
  57.         bases = words
  58.         chances = wordchances
  59.  
  60.     base = weightedChoice(chances,bases)
  61.     base = base[0:1].upper() + base[1:len(base)].lower()
  62.     result = base
  63.  
  64.     #print("Base is " + base)
  65.     result = base * (max_length + 1)
  66.     tries = 0
  67.     while len(result) > max_length:
  68.         tries += 1
  69.         if tries > 32:
  70.             return "FAILED"
  71.         result = base
  72.         mods = []
  73.        
  74.         if random.randint(1,2) == 1:
  75.             mods.append("l")
  76.         if random.randint(1,2) == 1:
  77.             mods.append("w")
  78.         if random.randint(1,2) == 1:
  79.             mods.append("i")
  80.         if random.randint(1,2) == 1:
  81.             mods.append("n")
  82.            
  83.         random.shuffle(mods)
  84.         #print("--Mods--")
  85.         for mod in mods:
  86.             ladd = ""
  87.             if mod == "l":
  88.                 i = 0
  89.                 max = random.randint(1,3)
  90.                 while i < max:
  91.                     i += 1
  92.                     ladd += random.choice(letters)
  93.                 result += ladd
  94.                 #print("Added letters \"" + ladd + "\"")
  95.             elif mod == "w":
  96.                 i = 0
  97.                 max = random.randint(1,2)
  98.                 while i < max:
  99.                     i += 1
  100.                     caps = random.randint(1,2)
  101.                     randword = weightedChoice(wordchances,words)
  102.                     if caps == 2:
  103.                         randword = randword[0:1].upper() + randword[1:len(randword)].lower()
  104.                     ladd += randword
  105.                 result += ladd
  106.                 #print("Added words \"" + ladd + "\"")
  107.             elif mod == "i":
  108.                 randname = weightedChoice(irlchances,irl)
  109.                 ladd += randname[0:1].upper() + randname[1:len(randname)].lower()
  110.                 result += ladd
  111.                 #print("Added name \"" + ladd + "\"")
  112.             elif mod == "n":
  113.                 i = 0
  114.                 max = random.randint(1,5)
  115.                 while i < max:
  116.                     i += 1
  117.                     num = random.randint(0,9)
  118.                     ladd += str(num)
  119.                 result += ladd
  120.                 #print("Added number \"" + ladd + "\"")
  121.         #print(str(len(result)) + ": " + result)
  122.         #print("--Mods--")
  123.    
  124.     return result
Advertisement
Add Comment
Please, Sign In to add comment