マルコフ連鎖

何かをしようとして作ったらしき、マルコフ連鎖のプログラムが見つかった。JavaScript で書いてある。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Malkov</title>
<script type="text/javascript">
//<![CDATA[

function Malkov() {
    this.initialize.apply(this, arguments);
};

Malkov.prototype = {
    initialize: function(size) {
        this.size = size;
    },
    initWords: function() {
        var words = new Array(this.size);
        for (var i = 0; i < this.length; i++) {
            words[i] = "\n";
        }
        return words;
    },
    build: function(tokens) {
        var words = this.initWords();
    
        for (var i in tokens) {
            var key = words.join(":::");
            if (!this.data.hasOwnProperty(key)) {
                this.data[key] = new Array();
            }
            this.data[key].push(tokens[i]);
        
            words.shift();
            words.push(tokens[i]);
        }
    },
    generate: function() {
        var words = this.initWords();
        
        var len = 20;
        var delimiter = " ";
        if (arguments.length >= 1) len = parseInt(arguments[0]);
        if (arguments.length >= 2) delimiter = arguments[1];
        
        var output = "";
        for (var i = 0; i < len; i++) {
            var key = words.join(":::");
            var suf = this.data[key];

            if (!suf) break;
            var token = suf[parseInt(Math.random() * suf.length)];
            
            output += token + delimiter;

            words.shift();
            words.push(token);
        }
        return output;
    },
    data: {},
    size: 2,
};


function main() {
    var a = document.getElementById("in").innerHTML.split(" ");
    var t;

    var m = new Malkov(2);
    m.build(a);
    t = m.generate(30);

    document.getElementById("out").innerHTML = t;
}


//]]>
</script>

</head>
<body>
quote : http://en.wikipedia.org/wiki/JavaScript
<div id="in" style="border-style:solid;border-color:#999;border-width:1px;padding:5px;font-size:9px">
JavaScript is a scripting language widely used for client-side web development. It was the originating dialect of the ECMAScript standard. It is a dynamic, weakly typed, prototype-based language with first-class functions. JavaScript was influenced by many languages and was designed to look like Java, but be easier for non-programmers to work with.
Although best known for its use in websites (as client-side JavaScript), JavaScript is also used to enable scripting access to objects embedded in other applications.
JavaScript, despite the name, is essentially unrelated to the Java programming language, although both have the common C syntax, and JavaScript copies many Java names and naming conventions. The language's name is the result of a co-marketing deal between Netscape and Sun, in exchange for Netscape bundling Sun's Java runtime with their then-dominant browser. The key design principles within JavaScript are inherited from the Self and Scheme programming languages.
"JavaScript" is a trademark of Sun Microsystems. It was used under license for technology invented and implemented by Netscape Communications and current entities such as the Mozilla Foundation.
</div>

<button onclick="main()">do Malkov!</button>

<div id="out"></div>

</body>
</html>