-
-
Save RonnyO/3004194 to your computer and use it in GitHub Desktop.
Bookmarklet to count word frequency
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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