Skip to content

Instantly share code, notes, and snippets.

@RonnyO
Forked from ernestom/word-frequency-counter.js
Created June 27, 2012 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RonnyO/3004194 to your computer and use it in GitHub Desktop.
Save RonnyO/3004194 to your computer and use it in GitHub Desktop.
Bookmarklet to count word frequency
(function () {
var settings = {
listLength: 30,
ignore: ['if', 'as', 'is', 'the', 'any', 'and', 'to', 'or', 'a', 'of']
},
w, s;
function getBodyText() {
var doc = document,
body = doc.body,
selection, range, bodyText;
if (body.createTextRange) {
return body.createTextRange().text;
} else if (getSelection) {
selection = getSelection();
range = doc.createRange();
range.selectNodeContents(body);
selection.addRange(range);
bodyText = selection.toString();
selection.removeAllRanges();
return bodyText;
}
}
var punctuation = /[\/\.\*\+\+\?\|\(\)\[\]\{\}\^\\,:;-`~!@#$%&_]+/g;
var words = getBodyText().trim().replace(punctuation, ' ').replace(/\s+/g, ' ').split(' '),
count = {},
sorted = [];
for (w in words) {
if (words.hasOwnProperty(w) && settings.ignore.indexOf(words[w]) == -1) {
var word = words[w];
count[word] = count[word] ? count[word] + 1 : 1;
}
}
for (w in count) if (count.hasOwnProperty(w)) {
sorted.push([w, count[w]]);
}
s = sorted.sort(function (a, b) {
return b[1] - a[1];
});
var output = '<title>ספירת מילים</title><ul style="direction: rtl; text-align: right; font-family: sans-serif; line-height: 130%;">';
for (s in sorted.slice(0, settings.listLength)) {
var c = sorted[s];
output += '<li>' + c[1] + ': ' + c[0] + '</li>';
}
output += '</ul>';
with(open().document){
write(output);
close();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment