Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
Latest commit
68 lines (57 loc) · 1.79 KB
/
Copy pathscripts.js
File metadata and controls
68 lines (57 loc) · 1.79 KB
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
if(typeofresponses==='undefined'){
alert("Error: responses.js not loaded");
}
window.onload=function(){
setupEventListeners();
}
functionsetupEventListeners(){
input=document.getElementById('chatInput');
input.addEventListener('keydown',function(event){
if(event.keyCode==13){
text=document.getElementById('chatText');
text.innerText+='user@localhost$ '+input.value+'\n';
varresponse=getResponse(input.value);
text.innerText+='chell@remote$ '+response+'\n';
input.value="";
text.scrollTop=text.scrollHeight;
};
});
}
functionnormaliseText(text){
returntext.toLowerCase().replace(/!?.,'"/g,'').trim();
}
functiongetResponse(input){
varmaxResponseScore=-Infinity;
varmaxResponseId=0;
for(vari=0;i<responses.length;i++){
varresponseScore=0;
varmatchTokens=responses[i].match.toLowerCase().split(",");
for(varj=0;j<matchTokens.length;j++){
if(input.indexOf(matchTokens[j])!==-1){
if(input[0]==="!"){
responseScore-=10;
}else{
responseScore+=matchTokens[j].split(" ").length;
}
}
}
if(responses[i].used){
// apply heavy penalty
responseScore/=(2*responses[i].used);
responseScore-=(3*responses[i].used);
}
if(responseScore>maxResponseScore){
maxResponseId=i;
maxResponseScore=responseScore;
}
}
responses[maxResponseId].used=responses[maxResponseId].used+1||1;
returnevaluateResponse(responses[maxResponseId].response);
}
functionevaluateResponse(response){
if(typeofresponse==="string"||responseinstanceofString){
returnresponse;
}elseif(typeofresponse==="function"){
returnevaluateResponse(response());
}
}