Skip to main content

index.html

Name Generator

gist link

Attachment Type Size
index.html text/html 1.2KiB
script.js text/javascript 4.2KiB

index.html – text/html, 1.2KiB

<html>
    
    <head></head>
    
    <body>
<strong>Markov Team Name Generator</strong>
        <br />
        <button id="get-markov-name">Get Name</button>
        <p>First table shows names generated by creating a first name starting with the most common first two letters, and a last name starting with the most common first two letters.</p>
        <table border=1>
            <thead>
                <th>Max name length 15 characters</th>
                <th>Using Average Name Length</th>
                <th>No Max Length</th>
            </thead>
            <tbody id="output"></tbody>
        </table>
        <p>Second table creates names where the first name starts with a random pair of first letters and same case for the last name.</p>
        <table border=1>
            <thead>
                <th>Max name length 15 characters</th>
                <th>Using Average Name Length</th>
                <th>No Max Length</th>
            </thead>
            <tbody id="output-arbitrary"></tbody>
        </table>
        <a href="https://gist.github.com/master5o1/ae3fe23d8b6e0e5a36d1">gist source</a> on github
        <script src="script.js"></script>
    </body>

</html>

script.js – text/javascript, 4.2KiB

document.getElementById('get-markov-name').onclick = team;

function team() {
    var players = [
        ['Brendon', 'McCullum'],
        ['Kane', 'Williamson'],
        ['Ross', 'Taylor'],
        ['Martin', 'Guptill'],
        ['Hamish', 'Rutherford'],
        ['Dean', 'Brownlie'],
        ['Colin', 'Munro'],
        ['Tom', 'Latham'],
        ['BJ', 'Watling'],
        ['Luke', 'Ronchi'],
        ['Corey', 'Anderson'],
        ['Grant', 'Elliott'],
        ['Nathan', 'McCullum'],
        ['Anton', 'Devcich'],
        ['Jimmy', 'Neesham'],
        ['Daniel', 'Vettori'],
        ['Pace', 'Bowlers'],
        ['Trent', 'Boult'],
        ['Doug', 'Bracewell'],
        ['Mitchell', 'McClenaghan'],
        ['Kyle', 'Mills'],
        ['Tim', 'Southee'],
        ['Neil', 'Wagner'],
        ['Hamish', 'Bennett'],
        ['Andrew', 'Ellis'],
        ['Matt', 'Henry'],
        ['Adam', 'Milne'],
        ['Ish', 'Sodhi'],
        ['Mark', 'Craig']
    ];

    var firstNames = players.map(function (player) {
        return player[0];
    });

    var lastNames = players.map(function (player) {
        return player[1];
    });
    
    var html = '';
    
    
    html = '<tr>';
    html = addHtml(html, firstNames, lastNames, 15);
    html = addHtml(html, firstNames, lastNames, true);
    html = addHtml(html, firstNames, lastNames, Infinity);
    html += '</tr>';
    document.getElementById('output').innerHTML += html;
    
    html = '<tr>';
    html = addHtml(html, firstNames, lastNames, 15, true);
    html = addHtml(html, firstNames, lastNames, true, true);
    html = addHtml(html, firstNames, lastNames, Infinity, true);
    html += '</tr>';
    document.getElementById('output-arbitrary').innerHTML += html;
}

function addHtml(html, firstNames, lastNames, maxLength, useRandomFirstLetters) {
    firstName = processList(firstNames, maxLength, useRandomFirstLetters);
    lastName = processList(lastNames, maxLength, useRandomFirstLetters);
    return html + '<td>' + firstName + ' ' + lastName + '</td>';
}

function processList(namesList, useAverageNameLength, useRandomFirstLetters) {
    var firstTwoLetters = mode(namesList.map(function (name) {
        return name.substring(0, 2);
    }));
    
    if (useRandomFirstLetters) {
        firstTwoLetters = pickRandomly(namesList.map(function (name) {
            return name.substring(0, 2);
        }));   
    }

    var avgNameLength = avg(namesList.map(function (name) {
        return name.length;
    }));

    var markov = processWords(namesList);

    if (useAverageNameLength === true) {
        return buildString(markov, firstTwoLetters, avgNameLength);
    }

    return buildString(markov, firstTwoLetters, useAverageNameLength);
}

function processWords(words) {
    var dict = {};
    for (var i = 0; i < words.length; i++) {
        dict = generateMarkov(words[i], dict);
    }
    return dict;
}

function generateMarkov(letters, dict) {
    var i, key, letter, first, second, third;

    i = 0;
    first = letters[i++];
    second = letters[i++];
    while (i < letters.length) {
        third = letters[i++];
        key = first + second;
        dict[key] = dict[key] || [];
        dict[key].push(third);
        first = second;
        second = third;
    }

    return dict;
}

function buildString(dict, start, maxLength) {
    var dictKeys = Object.keys(dict);
    var key = start;
    var str = start;
    while (typeof dict[key] !== 'undefined' && str.length < maxLength) {
        var value = dict[key];
        var third = pickRandomly(value);
        str = str + third;
        key = key.split('')[1] + third;
    }
    return str;
}

function mode(list) {
    var frequency, items;

    frequency = {};
    list.forEach(function (item) {
        this[item] = this[item] || 0;
        this[item] = this[item] + 1;
    }, frequency);
    items = Object.keys(frequency);
    items.sort(function (a, b) {
        return frequency[b] - frequency[a];
    });

    return items[0];
}

function pickRandomly(list) {
    var index = Math.round(Math.random() * list.length) % list.length;
    return list[index];
}

function avg(list) {
    var sum = list.reduce(function (p, c) {
        return p + c;
    }, 0);
    var total = list.length;
    return Math.round(sum / total);
}