-
Notifications
You must be signed in to change notification settings - Fork 0
/
story.js
37 lines (31 loc) · 969 Bytes
/
story.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function MarkovModel() {
this.states = {}
this.state = ["\n"]
}
MarkovModel.prototype.addSample = function(state, followup) {
if (!this.states.hasOwnProperty(state)) {
this.states[state] = []
}
this.states[state].push(followup);
}
MarkovModel.prototype.startIteration = function() {
this.state = ["\n"]
}
MarkovModel.prototype.next = function() {
var options = this.states[this.state.join(' ')];
var followup = draw(options);
var oldstate = this.state.join(" ");
this.state = this.state.concat(followup)
while (this.state.length > 3) {
this.state.shift();
}
console.log('"' + oldstate + '" -> "' + followup.join(' ') + '", "' + this.state.join(" ") + '"');
return {'taken': followup, 'options': options};
}
function randomInt(minIncl, maxExcl) {
return minIncl + Math.floor(Math.random() * (maxExcl - minIncl));
}
function draw(list) {
var x = randomInt(0, list.length);
return list[x];
}