Difference between revisions of "Markov chains name generator in Python"

From RogueBasin
Jump to navigation Jump to search
m (added category)
m (Replace block with syntaxhighlight.)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
Here is the piece of code used in [[Mines of Elderlore]] to generate random names:
Here is the piece of code used in [[Mines of Elderlore]] to generate random names:


import random
<syntaxhighlight lang="Python" line>
import random
# from http://www.geocities.com/anvrill/names/cc_goth.html
 
PLACES = ['Adara', 'Adena', 'Adrianne', 'Alarice', 'Alvita', 'Amara', 'Ambika', 'Antonia', 'Araceli', 'Balandria', 'Basha',
# from http://www.geocities.com/anvrill/names/cc_goth.html
'Beryl', 'Bryn', 'Callia', 'Caryssa', 'Cassandra', 'Casondrah', 'Chatha', 'Ciara', 'Cynara', 'Cytheria', 'Dabria', 'Darcei',
PLACES = ['Adara', 'Adena', 'Adrianne', 'Alarice', 'Alvita', 'Amara', 'Ambika', 'Antonia', 'Araceli', 'Balandria', 'Basha',
'Deandra', 'Deirdre', 'Delores', 'Desdomna', 'Devi', 'Dominique', 'Drucilla', 'Duvessa', 'Ebony', 'Fantine', 'Fuscienne',
'Beryl', 'Bryn', 'Callia', 'Caryssa', 'Cassandra', 'Casondrah', 'Chatha', 'Ciara', 'Cynara', 'Cytheria', 'Dabria', 'Darcei',
'Gabi', 'Gallia', 'Hanna', 'Hedda', 'Jerica', 'Jetta', 'Joby', 'Kacila', 'Kagami', 'Kala', 'Kallie', 'Keelia', 'Kerry',
'Deandra', 'Deirdre', 'Delores', 'Desdomna', 'Devi', 'Dominique', 'Drucilla', 'Duvessa', 'Ebony', 'Fantine', 'Fuscienne',
'Kerry-Ann', 'Kimberly', 'Killian', 'Kory', 'Lilith', 'Lucretia', 'Lysha', 'Mercedes', 'Mia', 'Maura', 'Perdita', 'Quella',
'Gabi', 'Gallia', 'Hanna', 'Hedda', 'Jerica', 'Jetta', 'Joby', 'Kacila', 'Kagami', 'Kala', 'Kallie', 'Keelia', 'Kerry',
'Riona', 'Safiya', 'Salina', 'Severin', 'Sidonia', 'Sirena', 'Solita', 'Tempest', 'Thea', 'Treva', 'Trista', 'Vala', 'Winta']
'Kerry-Ann', 'Kimberly', 'Killian', 'Kory', 'Lilith', 'Lucretia', 'Lysha', 'Mercedes', 'Mia', 'Maura', 'Perdita', 'Quella',
'Riona', 'Safiya', 'Salina', 'Severin', 'Sidonia', 'Sirena', 'Solita', 'Tempest', 'Thea', 'Treva', 'Trista', 'Vala', 'Winta']
###############################################################################
 
# Markov Name model
###############################################################################
# A random name generator, by Peter Corbett
# Markov Name model
# http://www.pick.ucam.org/~ptc24/mchain.html
# A random name generator, by Peter Corbett
# This script is hereby entered into the public domain
# http://www.pick.ucam.org/~ptc24/mchain.html
###############################################################################
# This script is hereby entered into the public domain
class Mdict:
###############################################################################
    def __init__(self):
class Mdict:
        self.d = {}
    def __init__(self):
    def __getitem__(self, key):
        self.d = {}
        if key in self.d:
    def __getitem__(self, key):
            return self.d[key]
        if key in self.d:
        else:
            return self.d[key]
            raise KeyError(key)
        else:
    def add_key(self, prefix, suffix):
            raise KeyError(key)
        if prefix in self.d:
    def add_key(self, prefix, suffix):
            self.d[prefix].append(suffix)
        if prefix in self.d:
        else:
            self.d[prefix].append(suffix)
            self.d[prefix] = [suffix]
        else:
    def get_suffix(self,prefix):
            self.d[prefix] = [suffix]
        l = self[prefix]
    def get_suffix(self,prefix):
        return random.choice(l)   
        l = self[prefix]
        return random.choice(l)   
class MName:
 
    """
class MName:
    A name from a Markov chain
    """
    """
    A name from a Markov chain
    def __init__(self, chainlen = 2):
    """
        """
    def __init__(self, chainlen = 2):
        Building the dictionary
        """
        """
        Building the dictionary
        if chainlen > 10 or chainlen < 1:
        """
            print "Chain length must be between 1 and 10, inclusive"
        if chainlen > 10 or chainlen < 1:
            sys.exit(0)
            print "Chain length must be between 1 and 10, inclusive"
   
            sys.exit(0)
        self.mcd = Mdict()
   
        oldnames = []
        self.mcd = Mdict()
        self.chainlen = chainlen
        oldnames = []
   
        self.chainlen = chainlen
        for l in PLACES:
   
            l = l.strip()
        for l in PLACES:
            oldnames.append(l)
            l = l.strip()
            s = " " * chainlen + l
            oldnames.append(l)
            for n in range(0,len(l)):
            s = " " * chainlen + l
                self.mcd.add_key(s[n:n+chainlen], s[n+chainlen])
            for n in range(0,len(l)):
            self.mcd.add_key(s[len(l):len(l)+chainlen], "\n")
                self.mcd.add_key(s[n:n+chainlen], s[n+chainlen])
   
            self.mcd.add_key(s[len(l):len(l)+chainlen], "\n")
    def New(self):
   
        """
    def New(self):
        New name from the Markov chain
        """
        """
        New name from the Markov chain
        prefix = " " * self.chainlen
        """
        name = ""
        prefix = " " * self.chainlen
        suffix = ""
        name = ""
        while True:
        suffix = ""
            suffix = self.mcd.get_suffix(prefix)
        while True:
            if suffix == "\n" or len(name) > 9:
            suffix = self.mcd.get_suffix(prefix)
                break
            if suffix == "\n" or len(name) > 9:
            else:
                break
                name = name + suffix
            else:
                prefix = prefix[1:] + suffix
                name = name + suffix
        return name.capitalize()   
                prefix = prefix[1:] + suffix
        return name.capitalize()   
for i in range(100):
 
    print MName().New()
for i in range(100):
[[Category:Articles]]
    print MName().New()
</syntaxhighlight>
 
[[Category:Articles]] [[Category:Name_Generators]]

Latest revision as of 03:12, 22 June 2021

Here is the piece of code used in Mines of Elderlore to generate random names:

import random

# from http://www.geocities.com/anvrill/names/cc_goth.html
PLACES = ['Adara', 'Adena', 'Adrianne', 'Alarice', 'Alvita', 'Amara', 'Ambika', 'Antonia', 'Araceli', 'Balandria', 'Basha',
'Beryl', 'Bryn', 'Callia', 'Caryssa', 'Cassandra', 'Casondrah', 'Chatha', 'Ciara', 'Cynara', 'Cytheria', 'Dabria', 'Darcei',
'Deandra', 'Deirdre', 'Delores', 'Desdomna', 'Devi', 'Dominique', 'Drucilla', 'Duvessa', 'Ebony', 'Fantine', 'Fuscienne',
'Gabi', 'Gallia', 'Hanna', 'Hedda', 'Jerica', 'Jetta', 'Joby', 'Kacila', 'Kagami', 'Kala', 'Kallie', 'Keelia', 'Kerry',
'Kerry-Ann', 'Kimberly', 'Killian', 'Kory', 'Lilith', 'Lucretia', 'Lysha', 'Mercedes', 'Mia', 'Maura', 'Perdita', 'Quella',
'Riona', 'Safiya', 'Salina', 'Severin', 'Sidonia', 'Sirena', 'Solita', 'Tempest', 'Thea', 'Treva', 'Trista', 'Vala', 'Winta']

###############################################################################
# Markov Name model
# A random name generator, by Peter Corbett
# http://www.pick.ucam.org/~ptc24/mchain.html
# This script is hereby entered into the public domain
###############################################################################
class Mdict:
    def __init__(self):
        self.d = {}
    def __getitem__(self, key):
        if key in self.d:
            return self.d[key]
        else:
            raise KeyError(key)
    def add_key(self, prefix, suffix):
        if prefix in self.d:
            self.d[prefix].append(suffix)
        else:
            self.d[prefix] = [suffix]
    def get_suffix(self,prefix):
        l = self[prefix]
        return random.choice(l)  

class MName:
    """
    A name from a Markov chain
    """
    def __init__(self, chainlen = 2):
        """
        Building the dictionary
        """
        if chainlen > 10 or chainlen < 1:
            print "Chain length must be between 1 and 10, inclusive"
            sys.exit(0)
    
        self.mcd = Mdict()
        oldnames = []
        self.chainlen = chainlen
    
        for l in PLACES:
            l = l.strip()
            oldnames.append(l)
            s = " " * chainlen + l
            for n in range(0,len(l)):
                self.mcd.add_key(s[n:n+chainlen], s[n+chainlen])
            self.mcd.add_key(s[len(l):len(l)+chainlen], "\n")
    
    def New(self):
        """
        New name from the Markov chain
        """
        prefix = " " * self.chainlen
        name = ""
        suffix = ""
        while True:
            suffix = self.mcd.get_suffix(prefix)
            if suffix == "\n" or len(name) > 9:
                break
            else:
                name = name + suffix
                prefix = prefix[1:] + suffix
        return name.capitalize()  

for i in range(100):
    print MName().New()