Skip to content

Instantly share code, notes, and snippets.

@ernestom
Created February 13, 2012 18:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ernestom/1818962 to your computer and use it in GitHub Desktop.
Save ernestom/1818962 to your computer and use it in GitHub Desktop.
Bookmarklet to count word frequency
(function () {
function getBodyText(win) {
var doc = win.document,
body = doc.body,
selection, range, bodyText;
if (body.createTextRange) {
return body.createTextRange().text;
} else if (win.getSelection) {
selection = win.getSelection();
range = doc.createRange();
range.selectNodeContents(body);
selection.addRange(range);
bodyText = selection.toString();
selection.removeAllRanges();
return bodyText;
}
}
var words = getBodyText(window).trim().replace(/[\r\n\.\,\;]+/g, ' ').replace(/\s+/g, ' ').split(' '),
count = {},
sorted = [];
for (var w in words) if (words.hasOwnProperty(w)) {
var word = words[w];
count[word] = count[word] ? count[word] + 1 : 1;
}
for (var w in count) if (count.hasOwnProperty(w)) {
sorted.push([w, count[w]])
}
var s = sorted.sort(function (a, b) {
return b[1] - a[1];
});
for (var s in sorted.slice(0, 100)) {
var c = sorted[s];
console.log(c[1] + ' -> ' + c[0])
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment