-
Notifications
You must be signed in to change notification settings - Fork 0
/
markov_generator.html
75 lines (62 loc) · 2.16 KB
/
markov_generator.html
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<html>
<button id="generate_button">Generate</button>
<label>N = </label><input id="markov_n" value="100"></input><br />
<input type="radio" name="markov_type" value="words" checked="true">Words</input>
<input type="radio" name="markov_type" value="sentences">Sentences</input><br />
<label>At least </label><input id="markov_min" value="0"><label> subunits long</label><br />
<label>No more than </label><input id="markov_max" value="Infinity"><label> subunits</label><br />
<label>From source:</label><br />
<textarea id="markov_source"></textarea>
<div id="markov_results"></div>
<script src="markov_generator.js"></script>
<script>
function id(str){
return document.getElementById(str);
}
var prev_markov = {
"min" : 0,
"max" : Infinity,
"type" : "",
"source" : "",
"generator" : null
}
function run_markov_generator(){
var markov_source = id("markov_source").value;
if(document.getElementsByName("markov_type")[0].checked){
var markov_type = "words";
} else {
var markov_type = "sentences";
}
var markov_min = parseInt(id("markov_min").value);
if(isNaN(markov_min)){
markov_min = 0;
}
var markov_max = parseInt(id("markov_max").value);
if(isNaN(markov_max)){
markov_max = Infinity;
}
if(markov_min>markov_max){
markov_max = Infinity;
markov_min = 0;
}
var generator = new MarkovGenerator(type=markov_type);
if(markov_type == prev_markov.type && markov_source == prev_markov.source){
generator = prev_markov.generator;
} else {
generator.train(id("markov_source").value);
}
var generated = generator.generate(N=id("markov_n").value,min=markov_min,max=markov_max)
if(markov_type == "words"){
id("markov_results").innerText = generated.join("\n");
} else if(markov_type == "sentences"){
id("markov_results").innerText = generated.join(". ");
}
prev_markov.generator = generator;
prev_markov.type = markov_type;
prev_markov.source = markov_source;
prev_markov.min = markov_min;
prev_markov.max = markov_max;
}
id("generate_button").addEventListener("click",run_markov_generator)
</script>
</html>