Files
Gwendolyn/gwendolyn/funcs/other/name_generator.py
Nikolaj 2f4e606fbf
2024-10-30 15:31:46 +01:00

44 lines
1.3 KiB
Python

import random
class NameGenerator():
def __init__(self) -> None:
with open("gwendolyn/resources/names.txt", "r", encoding="UTF-8") as file_pointer:
self._names = file_pointer.read().lower().split("\n")
self._gen_letter_dict()
def _gen_letter_dict(self):
list_of_names = []
for name in self._names:
if (name != ""):
list_of_names.append("__" + name + "__")
dict_of_names = {}
for name in list_of_names:
for i in range(len(name)-3):
combination = name[i:i+2]
if combination not in dict_of_names:
dict_of_names[combination] = []
dict_of_names[combination].append(name[i+2])
self._letter_dict = dict_of_names
def generate(self):
combination = "__"
next_letter = ""
result = ""
while True:
number_of_letters = len(self._letter_dict[combination])
index = random.randint(0,number_of_letters - 1)
next_letter = self._letter_dict[combination][index]
if next_letter == "_":
break
else:
result = result + next_letter
combination = combination[1] + next_letter
return result.title()
if __name__ == "__main__":
print(NameGenerator().generate())