Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
118 lines (104 sloc) 3.08 KB
<!doctype html>
<html>
<head>
<title>Word Analytics Challenge</title>
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.10.1/build/cssreset/cssreset-min.css" />
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.10.1/build/cssbase/cssbase-min.css" />
<style type="text/css">
body > div {
margin: 10px;
}
#inputText {
resize: both;
width: 400px;
max-width: 1000px;
max-height: 800px;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
console.log('ready');
var inputText = $('#inputText');
var wordCount = $('#wordCount');
var letterCount = $('#letterCount');
var nonAlphaNumCount = $('#nonAlphaNumCount');
var mostPopularWords = $('#mostPopularWords');
var mostPopularLetters = $('#mostPopularLetters');
function getMostCommonWordOrChar(arr, outputelement) {
var hash = {};
var upper;
for (var ii in arr) {
upper = arr[ii].toUpperCase();
if (hash[upper] > 0) hash[upper]++;
else hash[upper] = 1;
}
var top3 = [{word: null, count: 0}, {word: null, count: 0}, {word: null, count: 0}];
var cc = 0;
for (var zz in hash) {
cc = hash[zz];
if (cc > top3[0].count) {
top3.pop();
top3.unshift({word: zz, count: cc});
}
else if (cc > top3[1].count) {
top3.pop();
top3[2] = top3.pop();
top3[1] = {word: zz, count: cc};
}
else if (cc > top3[2].count) {
top3.pop();
top3.push({word: zz, count: cc});
}
}
var out = '';
for (var xx = 0; xx < top3.length; xx++) out += top3[xx].word + ':' + top3[xx].count + ' ';
outputelement.html(out);
}
function textChange(evt) {
var wordArr = this.value.match(/\b\w+\b/g);
wordCount.html(wordArr ? wordArr.length : 'NA');
getMostCommonWordOrChar(wordArr, mostPopularWords);
var letterArr = this.value.match(/\w+?/g);
letterCount.html(letterArr ? letterArr.length : 'NA');
getMostCommonWordOrChar(letterArr, mostPopularLetters);
var nonAlphaNumArr = this.value.match(/[^\w\d\s]/g);
nonAlphaNumCount.html(nonAlphaNumArr ? nonAlphaNumArr.length : 'NA');
}
inputText.keyup(textChange);
inputText.bind('paste', textChange);
});
</script>
</head>
<body>
<div>
<a href="http://www.reddit.com/r/dailyprogrammer/comments/1e97ob/051313_challenge_125_easy_word_analytics/">Challenge Description</a>
</div>
<div>
<span>Place Text Here</span>
<div>
<textarea rows="10" id="inputText"></textarea>
<div>
Word Count:
<span id="wordCount"></span>
</div>
<div>
Letter Count:
<span id="letterCount"></span>
</div>
<div>
Non Alphanumeric Count:
<span id="nonAlphaNumCount"></span>
</div>
<div>
Most Popular Words:
<span id="mostPopularWords"></span>
</div>
<div>
Most Popular Letters:
<span id="mostPopularLetters"></span>
</div>
</div>
</div>
</body>
</html>