├── README.md ├── json2.js ├── markov.js └── sample ├── corpus.js └── index.html /README.md: -------------------------------------------------------------------------------- 1 | Markov Text Generator 2 | ===================== 3 | 4 | Why? 5 | ---- 6 | 7 | Because to understand markov chains I needed to hack something together. And to be honest, it produces some pretty interesting little stories, given the right input. 8 | 9 | How? 10 | ---- 11 | 12 | After including json2.js and markov.js in your page... 13 | 14 | var test = new markov(text, type, regex); 15 | 16 | The text can be anything you so desire. 17 | The type should be "string" or "json". 18 | And the regex should always be global. All matches are used. 19 | 20 | Methods 21 | ------- 22 | 23 | to generate a phrase of a given length: 24 | 25 | test.gen(length); 26 | 27 | to see the object containing the data: 28 | 29 | test.data; 30 | 31 | to get the JSON generated by JSON2.js of the data: 32 | 33 | test.getJson(); 34 | 35 | A warning though, you probably only want to use the JSON for making things a bit faster on the processing end with long text, as the JSON tends to be an order of magnitude greater in file size than the input text. 36 | 37 | Copyright 38 | --------- 39 | 40 | This is all open source, you can use it however you like. Especially to learn. 41 | 42 | That said, I would enjoy it if you would send me a sample to work produced by this. 43 | 44 | Oh, and JSON2.js isn't mine, the source can be found at http://www.json.org/json2.js -------------------------------------------------------------------------------- /json2.js: -------------------------------------------------------------------------------- 1 | /* 2 | * http://www.JSON.org/json2.js 3 | * 2010-03-20 4 | * 5 | * Public Domain. 6 | * 7 | * NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. 8 | * 9 | * See http://www.JSON.org/js.html 10 | * 11 | */ 12 | if(!this.JSON){this.JSON={};} 13 | (function(){function f(n){return n<10?'0'+n:n;} 14 | if(typeof Date.prototype.toJSON!=='function'){Date.prototype.toJSON=function(key){return isFinite(this.valueOf())?this.getUTCFullYear()+'-'+ 15 | f(this.getUTCMonth()+1)+'-'+ 16 | f(this.getUTCDate())+'T'+ 17 | f(this.getUTCHours())+':'+ 18 | f(this.getUTCMinutes())+':'+ 19 | f(this.getUTCSeconds())+'Z':null;};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(key){return this.valueOf();};} 20 | var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},rep;function quote(string){escapable.lastIndex=0;return escapable.test(string)?'"'+string.replace(escapable,function(a){var c=meta[a];return typeof c==='string'?c:'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4);})+'"':'"'+string+'"';} 21 | function str(key,holder){var i,k,v,length,mind=gap,partial,value=holder[key];if(value&&typeof value==='object'&&typeof value.toJSON==='function'){value=value.toJSON(key);} 22 | if(typeof rep==='function'){value=rep.call(holder,key,value);} 23 | switch(typeof value){case'string':return quote(value);case'number':return isFinite(value)?String(value):'null';case'boolean':case'null':return String(value);case'object':if(!value){return'null';} 24 | gap+=indent;partial=[];if(Object.prototype.toString.apply(value)==='[object Array]'){length=value.length;for(i=0;i 2 | 3 | Markov Sample 4 | 5 | 6 | 7 | 31 | 32 | 33 | 34 | 35 | --------------------------------------------------------------------------------