-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
238 lines (201 loc) · 7.19 KB
/
Copy pathmain.js
File metadata and controls
238 lines (201 loc) · 7.19 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
function dingerController($scope, $timeout) {
'use strict';
$scope.hitEnter = function () {
//takes two words, returns the percent matching
function compareWords(word1,word2) {
//word 1 must always be smaller after this guy blaze
if (word1.length > word2.length) {
var temp = word2;
word2 = word1;
word1 = temp;
}
var previousIndex = -1;
var blazes = 0;
var currentIndex;
for (var i = 0; i < word1.length; i++) {
currentIndex = word2.indexOf(word1.charAt(i), previousIndex+1);
if (currentIndex == -1) {
continue;
}
else if (currentIndex < previousIndex+3) {
previousIndex = currentIndex;
blazes++;
}
else {
previousIndex++;
}
}
return (blazes / word2.length);
}
if ($scope.answer) {
// underscore templating feature
var userTemplate = _.template( $('#user-template').html() );
$('.chat-padding-bottom').before( userTemplate({query: $scope.answer}) );
// update scrolling after message
document.getElementById('chat-box').scrollTop = document.getElementById('chat-box').scrollHeight;
$('#text-input').attr('placeholder', '');
document.getElementById('text-input').disabled = true;
// start here, version 1 intelligence: exact matching query
// key to juice is the key in juiced that best matched the user's query
var keyToJuice = $scope.answer;
var allKeys = _.keys(juiced);
var counts = [];
// if no matches, start version 2 intelligence: percent matching with weights
if (!juiced[keyToJuice]) {
var queryWords = $scope.answer.replace(/[^A-Za-z\s]/g, '').toLowerCase().split(' ');
var whitelist = 'how what when where why did who are'.split(' ');
var prev = -1;
var splitKeys;
var increment;
var isQuestion;
var proletariatThresh = 0.5;
var bourgeoisieThreash = 0.75;
for (var i = 0; i < allKeys.length; i++) {
splitKeys = allKeys[i].toLowerCase().split(' ');
counts[i] = 0;
prev = 0;
for (var j = 0; j < queryWords.length; j++) {
//redefine this variable for what you want to compare;
var matchPercentage;
increment = 1;
isQuestion = false;
//match beginning
if (j==0) {
for (var k = 0; k < whitelist.length; k++) {
matchPercentage = compareWords(whitelist[k],queryWords[0]);
if (matchPercentage > bourgeoisieThreash) {
increment += 2.5 * matchPercentage;
isQuestion = true;
}
}
}
if (isQuestion) {
//match middle
if (j == Math.floor(queryWords.length/2)) {
for (var d = 0; d < splitKeys.length; d++) {
matchPercentage = compareWords(splitKeys[d],queryWords[Math.floor(queryWords.length/2)]);
if (matchPercentage > proletariatThresh) {
increment += 1.5 * matchPercentage;
}
}
}
//match ending
matchPercentage = compareWords(splitKeys[splitKeys.length-1], queryWords[queryWords.length-1]);
if(matchPercentage > proletariatThresh){
increment += 2 * matchPercentage;
}
}
//final calculation
for (var k = prev; k < splitKeys.length; k++) {
matchPercentage = compareWords(splitKeys[k], queryWords[j]);
if(matchPercentage > proletariatThresh && k >= prev) {
prev = k;
counts[i] += increment * matchPercentage;
splitKeys.splice(k, 1);
break;
}
}
}
}
var countsAndKeys = _.zip(counts, allKeys);
countsAndKeys = _.sortBy(countsAndKeys, function(blaze){
return blaze[0];
});
countsAndKeys.reverse();
var matchThreshold = 0.4;
var variance = 2;
var bestKeyCounts = [];
var bestKeyMatches = [];
for(var i = 0; i < 3; i++){
if(countsAndKeys[i][0] < matchThreshold){
break;
}
//has to have a score within the variance of the maximum match
if (Math.abs(countsAndKeys[0][0] - countsAndKeys[i][0]) <= variance) {
bestKeyMatches.push(countsAndKeys[i][1]);
bestKeyCounts.push(countsAndKeys[i][0]);
}
}
if(bestKeyMatches.length != 0){
//this is the random key
var yolo = Math.floor(Math.random() * bestKeyMatches.length);
//first get total of all prob
var totalProb = 0;
for (var i = 0; i < bestKeyCounts.length; i++) {
totalProb += bestKeyCounts[i];
}
var weightedRandom = Math.floor(Math.random() * totalProb);
//by the end of this, yolo must be the index of weighted random
for (var i = 0; i < bestKeyCounts.length; i++) {
if (i == 0) {
if (weightedRandom <= bestKeyCounts[0]) {
yolo = i;
break;
}
} else if (i == 1) {
if (weightedRandom > bestKeyCounts[0] && weightedRandom <= (bestKeyCounts[0] + bestKeyCounts[1])) {
yolo = i;
break;
}
} else if (i == 2) {
if (weightedRandom > (bestKeyCounts[0] + bestKeyCounts[1]) && weightedRandom <= totalProb) {
yolo = i;
break;
}
}
}
console.log('total score ', totalProb);
var keyToJuice = bestKeyMatches[yolo];
console.log('best key:', bestKeyMatches[0]);
console.log('best key score:', bestKeyCounts[0]);
console.log('selected key: ', keyToJuice);
console.log('selected key score: ', bestKeyCounts[yolo]);
} else{
console.log('could not find match, going to random');
}
}
//all else fails pick random from all possible responses
if (!juiced[keyToJuice] || juiced[keyToJuice].length == 0 || juiced[keyToJuice][0].length == 0) {
keyToJuice = _.sample(Object.keys(juiced));
}
postJuice(keyToJuice);
$scope.answer = '';
}
};
// puts ding's response on the site
function postJuice (keyToJuice) {
var possRespon = juiced[keyToJuice];
var interval = 0;
var responseToPost = Math.floor(Math.random() * possRespon.length);
// say everything in key list
for (var i = 0; i < possRespon[responseToPost].length; i++) {
interval += randomizer(possRespon[responseToPost][i]);
$timeout(function() {
var georgeTemplate = _.template($('#george-template').html());
$('.chat-padding-bottom').before( georgeTemplate( {response: possRespon[responseToPost].shift()} ) );
var fbsound = new Audio('fb.mp3');
fbsound.play();
document.getElementById('chat-box').scrollTop = document.getElementById('chat-box').scrollHeight;
if(possRespon[responseToPost].length == 0){
possRespon.splice(responseToPost, 1);
document.getElementById('text-input').disabled = false;
document.getElementById('text-input').focus();
if(possRespon.length == 0){
delete juiced[keyToJuice];
}
}
}, interval);
}
}
//sets interval of his posts based on the length of his response.
function randomizer (word) {
return Math.floor((Math.random() * 800) + word.length*55); //used to be +200
}
}
// google analytics
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-52425810-1', 'auto');
ga('send', 'pageview');