stuff
This commit is contained in:
39
untitled/namegen.py
Normal file
39
untitled/namegen.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import random
|
||||
from collections import defaultdict
|
||||
|
||||
# NAME GEN MODEL
|
||||
|
||||
|
||||
class NameGenerator:
|
||||
def __init__(self, corpus, validate=None):
|
||||
self.model = self.build_model(self.pad_names_for_model(corpus))
|
||||
self.validate = validate
|
||||
|
||||
def pad_names_for_model(self, names):
|
||||
new_names = []
|
||||
for name in names:
|
||||
new_names.append(f"<<{name}>")
|
||||
return new_names
|
||||
|
||||
def build_model(self, words):
|
||||
model = defaultdict(list)
|
||||
for word in words:
|
||||
for pos in range(len(word) - 2):
|
||||
model[word[pos] + word[pos + 1]].append(word[pos + 2])
|
||||
return model
|
||||
|
||||
def make_name(self, model):
|
||||
result = "<<"
|
||||
while True:
|
||||
window = result[-2:]
|
||||
nxt = random.choice(model[window])
|
||||
if nxt == ">":
|
||||
break
|
||||
result += nxt
|
||||
return result.removeprefix("<<")
|
||||
|
||||
def generate_name(self):
|
||||
while True:
|
||||
name = self.make_name(self.model)
|
||||
if self.validate and self.validate(name, auto_gen=True) is None:
|
||||
return name
|
||||
Reference in New Issue
Block a user