Skip to main content

index.html

Simpler Name Generator Thing

gist link

Attachment Type Size
index.html text/html 746.0B
script.js text/javascript 3.7KiB

index.html – text/html, 746.0B

<html>
    
    <head></head>
    
    <body>
        <strong>Markov Team Name Generator</strong><br />
        <button id="get-markov-name">Get Name</button><br />
        <strong>Generated Name:</strong>
        <span id="generated-name">click Get Name to generate a name</span><br />
        <textarea style="min-width:200px; min-height: 400px;" id="previous-names"></textarea><br />
        <span>Source names based on people in the <a href="http://en.wikipedia.org/wiki/New_Zealand_national_cricket_team#Current_squad">Black Caps</a>, the New Zealand national cricket team<br />
        <a href="https://gist.github.com/master5o1/1e8f24628ab13282145a">gist source</a> on GitHub.
        <script src="script.js"></script>
    </body>

</html>

script.js – text/javascript, 3.7KiB

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 firstName = processList(firstNames, Infinity, true);
    var lastName = processList(lastNames, Infinity, true);
    var fullName = firstName + ' ' + lastName;

    var html = document.getElementById('previous-names').innerHTML;
    document.getElementById('previous-names').innerHTML = fullName + '\r\n' + html;
    document.getElementById('generated-name').innerHTML = fullName;
}

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);
}