-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
284 lines (232 loc) · 10.8 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
<!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">
<meta property="og:url" content="http://www.1shoe.net/js-ngrams">
<meta property="og:type" content="website">
<meta property="og:title" content="New Word Generator (js-ngrams by pstrinkle)">
<meta property="og:description" content="Generate new words and names based on English">
<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>js-ngrams by pstrinkle</title>
<style>
.mybtn {
font-family: 'Architects Daughter', 'Helvetica Neue', Helvetica, Arial, serif;
background-color:#fff;
border-radius: 9px;
border: 3px solid #9ddcff;
padding: 20px;
font-weight:bold;
font-size: 30px;
}
.mybtn:active {
background-color:#666;
color:#fff;
}
</style>
</head>
<body>
<header>
<div class="inner">
<h1>js-ngrams</h1>
<h2>Generative model for creating brand new words and names</h2>
<a href="https://github.com/pstrinkle/js-ngrams" 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>Generate a random new word based on English words.</p>
<div style="text-align:center;">
<button id='trainAndGenerateWord' class="mybtn">Generate New Word</button>
<button id='trainAndGenerateName' class="mybtn">Generate New Name</button>
</div>
<div id='generatedContainer' style="text-align:center;display:none;">
<div style="height:7px;"></div>
<span id='generatedOutput' class='mybtn' style="display:inline-block;width:75%;align:center;">Generated</span>
</div>
<div style="text-align:center;">
<div style="height:7px;"></div>
<span id='twitter-share-button-area'></span>
</div>
<script>
$(function() {
var numberWithCommas = function(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
};
var wordsModel = new Ngrams3();
var namesModel = new Ngrams3();
var unModel = new Ngrams3();
wordsModel.train(words);
namesModel.train(names);
$('#namecnt').text(numberWithCommas(names.length));
$('#wordcnt').text(numberWithCommas(words.length));
var reset = function() {
$('#generatedContainer').hide();
};
var generate = function(value, capitalize, model) {
var newWord = [];
var build = function(value) {
var a, b, c = '';
if (value === 0) {
return;
}
if (newWord.length == 0) {
a = '|';
c = model.generate(a);
console.log(a + ' -> ' + c);
} else {
if (newWord.length == 1) {
a = '|';
} else {
a = newWord[newWord.length-2];
}
b = newWord[newWord.length-1];
c = model.generate(a, b);
console.log(a + ' -> ' + b + ' -> ' + c);
}
newWord.push(c);
/* could build in reverse with tail recursion. :D */
return build(value - 1);
};
build(value);
if (capitalize) {
newWord[0] = newWord[0].toUpperCase();
}
return newWord.join("");
};
var updateTwitter = function(word, is) {
$('#twitter-share-button-area').empty();
var tweet = '';
if (is === 'word') {
tweet = word + " is a brand new word. It means: ";
} else {
tweet = word + " is my brand new name.";
}
var tweetBtn = $('<a>', {href: "https://twitter.com/intent/tweet",
class: 'twitter-share-button',
'data-url': "http://www.1shoe.net/js-ngrams",
'data-text': tweet,
'data-hashtags': "wordgenerator",
'data-size': "large"});
$('#twitter-share-button-area').append(tweetBtn);
twttr.widgets.load();
};
$('#trainAndGenerateWord').on('click', function(event) {
$(this).blur();
reset();
// 8.136568439587757 average length of words in training.
var w = generate(8, false, wordsModel);
updateTwitter(w, 'word');
$('#generatedOutput').text(w);
$('#generatedContainer').show();
});
$('#trainAndGenerateName').on('click', function(event) {
$(this).blur();
reset();
// 7.285966981132075 average length of names in training.
var w = generate(7, true, namesModel);
var l = generate(7, true, namesModel);
updateTwitter(w + ' ' + l, 'name');
$('#generatedOutput').text(w + ' ' + l);
$('#generatedContainer').show();
});
$('#train').on('click', function(event) {
var selected = $('#input_selection option:selected').val();
unModel.init();
$('#training').addClass('fa-spin');
$('#trained').empty();
if (selected === 'names') {
$('#trained').text('trained with ' + names.length + ' names');
unModel.train(names);
} else if (selected === 'nouns') {
$('#trained').text('trained with ' + words.length + ' words');
unModel.train(words);
}
$('#generate').show();
$('#training').removeClass('fa-spin');
});
$('#generate').on('click', function(event) {
var v = parseInt($('#wordLen').val());
$('#generated').text(generate(v, false, unModel));
});
});
</script>
<h3>Advanced</h3>
<h4>Training Set Details</h4>
<p>The above word generator is trained with
<span id='wordcnt'></span> words, while the name generator trained
with <span id='namecnt'></span> names. The list of names and words
were pulled from a linux systems's english words file.
</p>
<p>The generator produces new words randomly by sequentially
requesting letters. Each letter requested is the result of a random
variable that is influenced by the previous two letters in the
sequence. This influence provides the generator with the ability to
randomly create new words that follow similar spelling guidelines to
English words.
</p>
<p>In more straightforward terms, each letter is drawn based on what
letters are already in the word. Consider that 'th' is a common piece
of a word in English. Therefore if the previous letter is 't' there's
a decent chance the next will be 'h'.
</p>
<h4>Select training</h4>
<p>I provided two data sets for training the generator. They are the
proper nouns and regular nouns from a linux word list file.
</p>
<div>
<select id='input_selection' class="form-control">
<option value='names'>english proper nouns</option>
<option value='nouns'>english nouns</option>
</select>
<br />
<button id='train'><i id='training' class="fa fa-circle-o-notch" aria-hidden="true"></i> Train</button>
<span id='trained'></span><br />
</div>
<h4>Generate new word (or name)</h4>
<p>Generate a word given the data input used for training.</p>
<div>
number of characters to generate: <input id='wordLen' type='text' value='5' />
<br />
<button id='generate' style='display:none'>Generate random letters</button>
<br />
New word: <span id='generated'></span>
</div>
<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/js-ngrams/zipball/master" class="button">
<small>Download</small>
.zip file
</a>
<a href="https://github.com/pstrinkle/js-ngrams/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/names.min.js"></script>
<script src="assets/words.min.js"></script>
<script src="src/ngrams.js"></script>
</body>
</html>