-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
350 lines (321 loc) · 10.7 KB
/
script.js
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
var undoingText="";
function getWordStatsForNonEnglishText(nonEnglishText){
var wordStats=[];
var trimedText=$.trim(nonEnglishText);
wordStats['num_character'] = trimedText.length;
var wordList=trimedText.split(/[\s\n]+/);
wordStats['num_word']=trimedText.length>0? wordList.length:0;
return wordStats;
}
function updateStats(){
getWordStats();
densityStats();
}
function updateStatsWithExtraStatistics(){
getWordStats(true);
densityStats();
}
function getWordStats(withExtraStatistics){
var text = $("#textbox").val();
var isNotEnglish=$("#isNotEnglish").prop('checked');
var wordStats;
if(isNotEnglish)
wordStats = getWordStatsForNonEnglishText(text);
else{
wordStats = $.wordCountTool.getWordStats(text,withExtraStatistics);
}
displayWordStats(wordStats,withExtraStatistics);
displayWordStatsInTitle(wordStats);
}
function displayWordStats(wordStats,withExtraStatistics){
var isNotEnglish=$("#isNotEnglish").prop('checked');
if(!isNotEnglish){
$("#num_word").text(wordStats['num_word']);
$("#num_character").text(wordStats['num_character']);
$("#num_character_wo_spaces").text(wordStats['num_character_wo_spaces']);
$("#num_sentence").text(wordStats['num_sentence']);
$("#num_paragraph").text(wordStats['num_paragraph']);
$("#avg_sentence_length").text(wordStats['avg_sentence_length']);
$("#avg_word_length").text(wordStats['avg_word_length']);
$("#dale_chall_readability").text(wordStats['dale_chall_index']);
$("#readability_level").text(getReadabilityLevel(wordStats['dale_chall_index']));
var MAX_NUMBER_TO_DISPLAY_PERCENTAGE_SIGN= 10000;
var numDifficultWords=wordStats['num_difficult_words']< MAX_NUMBER_TO_DISPLAY_PERCENTAGE_SIGN?
""+wordStats['num_difficult_words']+" ("+wordStats['percentage_difficult_words']+"%)":""+wordStats['num_difficult_words'];
$("#num_difficult_word").text(numDifficultWords);
var numShortWords=wordStats['num_short_word']< MAX_NUMBER_TO_DISPLAY_PERCENTAGE_SIGN?
""+wordStats['num_short_word']+" ("+wordStats['percentage_num_short_word']+"%)":""+wordStats['num_short_word'];
$("#num_short_word").text(numShortWords);
if(wordStats['num_word']<MAX_WORDS_TO_DISPLAY_UNIQUENESS||withExtraStatistics==true){
$("#unique_word_container").show();
$("#showExtraStatistics").hide();
var numUniqueWords=wordStats['num_unique_word']< MAX_NUMBER_TO_DISPLAY_PERCENTAGE_SIGN?
""+wordStats['num_unique_word']+" ("+wordStats['percentage_num_unique_word']+"%)":""+wordStats['num_unique_word'];
$("#num_unique_word").text(numUniqueWords);
}
else{
$("#unique_word_container").hide();
$("#showExtraStatistics").show();
}
}
else{
$("#num_word").html(" ");
$("#num_difficult_word").html(" ");
$("#num_short_word").html(" ");
$("#num_unique_word").html(" ");
$("#num_character").html(" ");
$("#num_character_wo_spaces").html(" ");
$("#num_sentence").html(" ");
$("#num_paragraph").html(" ");
$("#avg_sentence_length").html(" ");
$("#avg_word_length").html(" ");
$("#dale_chall_readability").html(" ");
$("#readability_level").html(" ");
}
//console.log("Number of difficult words: "+wordStats['num_difficult_words']);
}
function getReadabilityLevel(score){
var readability="";
if(score>10)
readability+="graduate";
else if(score>9)
readability+="13-15th";
else if(score>8)
readability+="11-12th";
else if(score>7)
readability+="9-10th";
else if(score>6)
readability+="7-8th";
else if(score>5)
readability+="5-6th";
else
readability+="< 4th";
return readability;
}
function displayWordStatsInTitle(wordStats){
var WORDS=" words";
if(wordStats['num_word']==1)
WORDS=" word";
var CHARACTERS=" characters";
if(wordStats['num_character']==1)
CHARACTERS=" character";
var str=wordStats['num_word']+WORDS+", "+wordStats['num_character']+CHARACTERS+".";
var isNotEnglish=$("#isNotEnglish").prop('checked');
if(!isNotEnglish){
var readability="<span id='readability'> Readability level: Easily understood by a ";
if(wordStats['dale_chall_index']>10)
readability+="college graduate";
else if(wordStats['dale_chall_index']>9)
readability+="college student";
else if(wordStats['dale_chall_index']>8)
readability+="11-12th grade student";
else if(wordStats['dale_chall_index']>7)
readability+="9-10th grade student";
else if(wordStats['dale_chall_index']>6)
readability+="7-8th grade student";
else if(wordStats['dale_chall_index']>5)
readability+="5-6th grade student";
else
readability+="less than 4th grade";
str+=readability+".</span>";
}
$("#counter").html(str);
}
function sumWeight(weights){
var sum=0;
$.each(weights,function(){
sum+=this;
});
return sum;
}
function displayWordCloud(){
$('#myCanvasContainer').show();
if( ! $('#myCanvas').tagcanvas({
weight:true,
weightMode:"both",
weightFrom:"data-weight",
weightSizeMin:5,
weightSizeMax:150
},'tagList')) {
// TagCanvas failed to load
$('#myCanvasContainer').hide();
}
}
function densityStats(){
var isNotEnglish=$("#isNotEnglish").prop('checked');
if(isNotEnglish){
$("#topDensity").html("");
$('#myCanvasContainer').hide();
return;
}
var numTop=parseInt($("#numTopKeyWord").val());;
var viewWordCloud=$("#viewWordCloudCheckbox").prop('checked');
if($("#excludingCheckbox").prop('checked'))
$.wordStats.setStopWords(true);
else
$.wordStats.setStopWords(false);
$.wordStats.computeTopWords(numTop,$("#textbox"));
//var listHtml="<ol id='topDensityList'>";
if(numTop>$.wordStats.topWords.length)
numTop=$.wordStats.topWords.length;
var sum=sumWeight($.wordStats.topWeights);
var tagList=$('#tagList ul');
tagList.empty();
var tagListStr="";
listHtml="<table id='densityList'><tr>";
for(var i=0;i<numTop;i++){
var word=$.wordStats.topWords[i];
var weight=$.wordStats.topWeights[i];
var percentage=(weight/sum*100).toFixed(1);
var timeAppeared=weight>1?"times":"time";
var orderInList=""+(i+1);
//listHtml+='<li><label><strong>'+order+'. '+word+'</strong> - '+weight+' '+time+'<span class="percentage"> ('+percentage+'%)</span></label></li>';
listHtml+='<td><label>'+orderInList+'. <strong>'+word+'</strong> - '+weight+' '+timeAppeared+'<span class="percentage"> ('+percentage+'%)</span></label></td>';
if((i+1)%2==0){
listHtml+="</tr><tr>";
}
tagListStr+="<li><a href='javascript:void(0)' data-weight="+weight+">"+word+"</a></li>";
}
listHtml+="</tr></table>";
listHtml+="</ol>";
$.wordStats.clear();
$("#topDensity").html(listHtml);
if(viewWordCloud){
tagList.append(tagListStr);
displayWordCloud();
}
}
function generateClicked(){
var typeOption = $('#typeOption').val();
var amountOption = $('#amountOption').val();
//console.log(amountOption);
var lorem=$.lorem.getLorem({type:typeOption,amount:amountOption,ptags:false});
$("#textbox").val(lorem);
//console.log(lorem);
getWordStats();
densityStats();
}
function defaultValueForOption(){
var typeOption = $('#typeOption').val();
//console.log(typeOption);
switch(typeOption){
case "paragraphs":
$('#amountOption').val("3")
break;
case "words":
$('#amountOption').val("250")
break;
case "characters":
$('#amountOption').val("1000")
break;
}
generateClicked();
}
String.prototype.toTitleCase = function() {
var i, str, lowers, uppers;
str = this.replace(/([^\W_]+[^\s-]*) */g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
// Certain minor words should be left lowercase unless
// they are the first or last words in the string
lowers = ['A', 'An', 'The', 'And', 'But', 'Or', 'For', 'Nor', 'As', 'At',
'By', 'For', 'From', 'In', 'Into', 'Near', 'Of', 'On', 'Onto', 'To', 'With'];
for (i = 0; i < lowers.length; i++)
str = str.replace(new RegExp('\\s' + lowers[i] + '\\s', 'g'),
function(txt) {
return txt.toLowerCase();
});
// Certain words such as initialisms or acronyms should be left uppercase
uppers = ['Id', 'Tv'];
for (i = 0; i < uppers.length; i++)
str = str.replace(new RegExp('\\b' + uppers[i] + '\\b', 'g'),
uppers[i].toUpperCase());
return str;
}
function convertCaseForText(){
var text = $("#textbox").val();
var buttonID=$(this).attr("id");
switch (buttonID){
case "toUpperCaseButton":
undoingText=text;
text=text.toUpperCase();
break;
case "toLowerCaseButton":
undoingText=text;
text=text.toLowerCase();
break;
case "toSentenceCaseButton":
undoingText=text;
text=text+'.'
text=text.replace(/\w[^.?!:\n]*[.?!:\n$]+/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
text=text.substr(0,text.length-1);
break;
case "toTitleCaseButton":
undoingText=text;
text=text.toTitleCase();
break;
case "toToggleCaseButton":
undoingText=text;
text=text.replace(/[\w]*/g, function(txt){return txt.charAt(0).toLowerCase() + txt.substr(1).toUpperCase();});
break;
case "htmlStripButton":
undoingText=text;
html="<html>"+text+"</html>";
text=$(html).text();
break;
case "clearTextButton":
undoingText=text;
text="";
break;
case "undoButton":
if(undoingText.length!=0){
var temp=text;
text=undoingText;
undoingText=temp;
}
break;
}
$("#textbox").val(text);
updateStats();
}
$(document).ready(function(){
var textbox=$("#textbox");
textbox.focus();
textbox.on('input propertychange',getWordStats);
textbox.on('input propertychange',densityStats);
textbox.bind('paste', function () {
setTimeout(function () {
getWordStats();
densityStats();
}, 10);
});
//$("#numTopKeyWord").on('keyup keypress blur change',densityStats);
$("#numTopKeyWord").on('keyup blur change',densityStats);
$("#excludingCheckbox").change(densityStats);
$("#viewWordCloudCheckbox").change(densityStats);
$("#isNotEnglish").change(getWordStats);
$("#isNotEnglish").change(densityStats);
$('#amountOption').on('keyup keypress blur change',generateClicked);
//Generate
$("#generate").click(generateClicked);
//Options
$("#typeOption").change(defaultValueForOption);
//Case converter
$(".convert input").click(convertCaseForText);
//Show number of unique words
$("#extraStatisticsButton").click(updateStatsWithExtraStatistics);
resizeHeightOfDefinitionBox();
$(window).resize(resizeHeightOfDefinitionBox);
});
function resizeHeightOfDefinitionBox(){
$("#definitionBox").height($("#infoBox").height());
$("#statistics").height($("#boxContainer").height());
}
/*
function avoidEmptyStringList(list){
if(list.length==1&&list[0]=="")
return [];
else
return list;
}
*/