-
Notifications
You must be signed in to change notification settings - Fork 10
/
rubber_rats_markov_chain.txt
55 lines (47 loc) · 1.39 KB
/
rubber_rats_markov_chain.txt
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
@name rubber rats markov chain
@inputs
@outputs
@persist TRAINING_DATA:string
@persist Markov_Data:table
@trigger
if(first()){
TRAINING_DATA = "Crazy? I was crazy once. They locked me in a room. A rubber room. A rubber room filled with rats. And rats make me crazy."
let ProcessedData = TRAINING_DATA:lower():explode(" ")
function dePunct(Str:string){
Str = Str:replace("?","")
return Str:replace(".","")
}
for (I=2, ProcessedData:count()) {
const Pre_Words = array(ProcessedData[I-1,string], dePunct(ProcessedData[I-1,string]))
const Post = ProcessedData[I,string]
foreach(_:number, Pre:string = Pre_Words){
if(!Markov_Data:exists(Pre)){
Markov_Data[Pre,array] = array()
}
Markov_Data[Pre,array]:pushString(Post)
}
}
function string pickRandom(R:array) {
return R[ randint(1,R:count()), string ]
}
function array generate(Words:array){
const Next = Words[Words:count(),string]
const Pool = Markov_Data[dePunct(Next),array]
if(!Pool:count()) {
Words:popString()
return generate(Words)
}
const Word = pickRandom(Pool)
if( (Words:concat(" ") + " " + Word):length() > 126 ){
return Words
}
Words:pushString(Word)
return generate(Words)
}
timer("chat",1000)
}
if(clk("chat")){
timer("chat",10000)
const Crazy = generate(array("crazy?"))
concmd("say " + Crazy:concat(" "))
}