-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
399 lines (354 loc) · 14.4 KB
/
index.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href='https://fonts.googleapis.com/css?family=Architects+Daughter' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="stylesheets/stylesheet.css" media="screen">
<link rel="stylesheet" type="text/css" href="stylesheets/github-light.css" media="screen">
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" media="screen">
<!-- above the fold, it is render-blocking deliberately. -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<title>Javascript-modeling by pstrinkle</title>
</head>
<body>
<header>
<div class="inner">
<h1>javascript-modeling</h1>
<h2>Some fun and somewhat silly modeling modules written in pure JavaScript</h2>
<a href="https://github.com/pstrinkle/javascript-modeling" class="button"><small>View project on</small> GitHub</a>
</div>
</header>
<div id="content-wrapper">
<div class="inner clearfix">
<section id="main-content">
<h3><a id="welcome-to-github-pages" class="anchor" href="#welcome-to-github-pages" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>The Basics</h3>
<p>A very basic approach to modeling of text in JavaScript.</p>
<script>
$(function() {
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(searchString, position) {
var subjectString = this.toString();
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
}
});
</script>
<h3>Vectorspace</h3>
<div>
<h4>Stopwords</h4>
<p>You can upload a text file of newline separated stop words. These
words will be stripped from the input documents.</p>
<button id='stopwords'>upload Stopwords (.txt)</button>
<input type='file' id='stopwords_input' style="display:none;" />
<pre id='stopwords_area'></pre>
</div>
<div>
<h4>Queries</h4>
<p>Need to allow uploading loads of files for querying.</p>
<button id='upload_document'>upload document (.txt)</button><button id='clear_corpus'>clear</button>
<input type='file' id='upload_document_input' style="display:none;" />
<br /><br />
<div id='docs'></div>
<br />
<strong>Enter query:</strong> <input type='text' id='query_input' />
<button id='run_query'>query</button><button id='clear'>clear</button>
<br />
<div id='results'></div>
<pre id='qout'></pre>
</div>
<div>
<h4>Document Comparison</h4>
<p>You can enter text into each box and compare the two documents.</p>
<div>
<div style="width:50%;float:left;">
<strong>Document 1</strong>
<textarea id='content' style="width:98%;resize:vertical;" rows="5"></textarea>
</div>
<div style="width:50%;float:left;">
<strong>Document 2</strong>
<textarea id='content2' style="width:98%;resize:vertical;" rows="5"></textarea>
</div>
</div>
<div style="clear:both;"></div>
<button id='btn'>compare</button>
<pre id='out'></pre>
</div>
<script>
$(function() {
VectorSpace.cleanup = Util.cleanup;
var updateList = function() {
var docs = VectorSpace.listCorpus();
var $docs = $('#docs');
$docs.empty();
$.each(docs, function(index, element) {
var $li = $('<li>', {text: element});
$li.on('click', function(event) {
var $q = $('#qout');
$q.empty();
$q.text('"' + VectorSpace.getDocument(element) + '"\n');
$q.append(JSON.stringify(VectorSpace.getDocumentTf(element), null, 2));
});
$docs.append($li);
});
};
$('#clear_corpus').on('click', function(event) {
$('#docs').empty();
VectorSpace.emptyCorpus();
});
$('#clear').on('click', function(event) {
$('#results').empty();
});
$('#run_query').on('click', function(event) {
var $results = $('#results');
$('#qout').empty();
var results = VectorSpace.query($('#query_input').val());
$results.empty();
var $ol = $('<ol>');
$.each(results, function(index, element) {
$ol.append($('<li>', {text: element[0]}));
});
$results.append($ol);
});
$('#stopwords').on('click', function(event) {
$('#stopwords_input').trigger('click');
});
$('#upload_document').on('click', function(event) {
$('#upload_document_input').trigger('click');
});
$('#stopwords_input').on('change', function(event) {
if (this.files[0] == undefined) {
return;
}
var reader = new FileReader();
reader.onload = function(){
var text = reader.result;
VectorSpace.importStopwords(text);
/* */
$('#stopwords_area').text(JSON.stringify(VectorSpace.getStopwords(), null, 2));
};
reader.readAsText(this.files[0]);
});
$('#upload_document_input').on('change', function(event) {
if (this.files[0] == undefined) {
return;
}
var name = this.files[0]['name'];
var reader = new FileReader();
reader.onload = function(){
var text = reader.result;
VectorSpace.addDocument(name, text);
updateList();
};
reader.readAsText(this.files[0]);
});
$('#btn').on('click', function(event) {
event.preventDefault();
$('#log').empty();
var $out = $('#out');
$out.empty();
var modelA = VectorSpace.process($('#content').val());
var modelB = VectorSpace.process($('#content2').val());
var score = VectorSpace.cosineSimilarity(modelA, modelB) * 100;
$out.append(JSON.stringify(modelA, null, 2) + '\n');
$out.append(JSON.stringify(modelB, null, 2) + '\n');
$out.append(score + '%');
});
});
</script>
<h3>Bigrams</h3>
<div>
<ol>
<li>Enter text into the box.</li>
<li>Click "learn" for it to update the model</li>
<li>Click "write" to generate words, or type directly into the box.</li>
</ol>
<p>Presently it always tries to build the highest probability pair
(although given equal probabilities it randomly chooses).
I'd like to have it optionally choose randomly against a weighted
pool so it's more likely to choose that path, but won't every time.
</p>
<textarea id='learning' style="width:100%;" rows="10"></textarea><br />
<button id='learn'>learn</button>
<button id='write'>write</button>
<pre id='log'></pre>
</div>
<script>
$(function() {
var $learning = $('#learning');
Bigrams.cleanup = Util.cleanup;
$('#learn').on('click', function(event) {
Bigrams.init($learning.val());
$learning.val('');
var $log = $('#log');
$log.empty();
$log.text(JSON.stringify(Bigrams.getBigrams(), null, 2));
});
var generate = function(highlight = false) {
var sofar = $learning.val().trim();
var next = '';
var plusone = 0;
if (sofar.length == 0) {
next = Bigrams.generate("|");
} else {
var words = sofar.split(' ');
var last = words[words.length-1];
if (last === '.' || last.endsWith(".")) {
next = Bigrams.generate("|");
} else {
next = Bigrams.generate(last);
}
}
if (next === '.') {
if (!highlight) {
$learning.val(sofar + next + ' ');
}
} else {
if (sofar.length == 0) {
$learning.val(sofar + next);
} else {
plusone = 1;
$learning.val(sofar + ' ' + next);
}
if (highlight) {
$learning.selectRange(sofar.length + plusone, -1);
}
}
};
$learning.keyup(function(event) {
if (event.which == 32) {
// space
generate(true);
}
});
$('#write').on('click', function(event) {
generate();
});
});
</script>
<h3>Trigrams</h3>
<div>
<textarea id='learning2' style="width:100%;" rows="10"></textarea><br />
<button id='learn2'>learn</button>
<button id='write2'>write</button>
<pre id='log2'></pre>
</div>
<script>
$(function() {
var $learning2 = $('#learning2');
Trigrams.cleanup = Util.cleanup;
$('#learn2').on('click', function(event) {
var $log2 = $('#log2');
$log2.empty();
Trigrams.init($learning2.val());
$learning2.val('');
$log2.text(JSON.stringify(Trigrams.getTrigrams(), null, 2));
});
var generate2 = function(highlight = false) {
var sofar = $learning2.val().trim();
var next = '';
var plusone = 0;
if (sofar.length == 0) {
next = Trigrams.generate("|");
} else {
var words = sofar.split(' ');
var last = words[words.length-1];
/* handle there is only one word. */
if (words.length === 1 || words.length === 2 && words[1] === '') {
next = Trigrams.generate("|", words[0]);
} else {
if (last === '.' || last.endsWith(".")) {
next = Trigrams.generate("|");
} else {
var prev = words[words.length-2];
// prev last {next}
// so if prev was the end of a sentence, last was the first
// word of the next.
if (prev === '.' || prev.endsWith(".")) {
next = Trigrams.generate("|", last);
} else {
next = Trigrams.generate(prev, last);
}
}
}
}
if (next === '.') {
if (!highlight) {
$learning2.val(sofar + next + ' ');
}
} else {
if (sofar.length == 0) {
$learning2.val(sofar + next);
} else {
plusone = 1;
$learning2.val(sofar + ' ' + next);
}
if (highlight) {
$learning2.selectRange(sofar.length + plusone, -1);
}
}
};
$learning2.keyup(function(event) {
if (event.which == 32) {
// space
generate2(true);
}
});
$('#write2').on('click', function(event) {
generate2();
});
});
</script>
<h3>String Cleaning</h3>
<div>
<p>Just for testing what happens when strings are sent in.</p>
<textarea id='dirty' style="width:100%;" rows="5"></textarea><br />
<textarea id='clean' style="width:100%;" rows="5"></textarea><br />
<button id='cleanIt'>clean</button>
</div>
<script>
$(function() {
$('#cleanIt').on('click', function(event) {
var d = Util.cleanup($('#dirty').val(), false);
$('#clean').val(d);
});
});
</script>
<h3>License</h3>
<p><a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License 2.0</a></p>
<h3><a id="support-or-contact" class="anchor" href="#support-or-contact" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Support or Contact</h3>
<a href="https://twitter.com/YammyCozonac" class="twitter-follow-button" data-show-count="false" data-size="large">Follow @YammyCozonac</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<a class="github-button" href="https://github.com/pstrinkle" data-style="mega" aria-label="Follow @pstrinkle on GitHub">Follow @pstrinkle</a>
<!-- <span class="site-footer-credits">This page was generated by <a href="https://pages.github.com">GitHub Pages</a> using the <a href="https://github.com/jasonlong/cayman-theme">Cayman theme</a> by <a href="https://twitter.com/jasonlong">Jason Long</a>.</span> -->
<script async defer src="https://buttons.github.io/buttons.js"></script>
</section>
<aside id="sidebar">
<a href="https://github.com/pstrinkle/javascript-modeling/zipball/master" class="button">
<small>Download</small>
.zip file
</a>
<a href="https://github.com/pstrinkle/javascript-modeling/tarball/master" class="button">
<small>Download</small>
.tar.gz file
</a>
<p class="repo-owner"><a href="https://github.com/pstrinkle/javascript-modeling"></a> is maintained by <a href="https://github.com/pstrinkle">pstrinkle</a>.</p>
</aside>
</div>
</div>
<script src="assets/jquery.selectrange.js"></script>
<script src="src/util.js"></script>
<script src="src/bigrams.js"></script>
<script src="src/trigrams.js"></script>
<script src="src/vectorspace.js"></script>
</body>
</html>