-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
68 lines (57 loc) · 1.79 KB
/
scripts.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 (typeof responses === 'undefined') {
alert("Error: responses.js not loaded");
}
window.onload = function() {
setupEventListeners();
}
function setupEventListeners() {
input = document.getElementById('chatInput');
input.addEventListener('keydown', function(event) {
if (event.keyCode == 13) {
text = document.getElementById('chatText');
text.innerText += 'user@localhost$ ' + input.value + '\n';
var response = getResponse(input.value);
text.innerText += 'chell@remote$ ' + response + '\n';
input.value = "";
text.scrollTop = text.scrollHeight;
};
});
}
function normaliseText(text) {
return text.toLowerCase().replace(/!?.,'"/g, '').trim();
}
function getResponse(input) {
var maxResponseScore = -Infinity;
var maxResponseId = 0;
for (var i = 0; i < responses.length; i++) {
var responseScore = 0;
var matchTokens = responses[i].match.toLowerCase().split(",");
for (var j = 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;
return evaluateResponse(responses[maxResponseId].response);
}
function evaluateResponse(response) {
if (typeof response === "string" || response instanceof String) {
return response;
} else if (typeof response === "function") {
return evaluateResponse(response());
}
}