Skip to content

Instantly share code, notes, and snippets.

@ernestom
Created February 13, 2012 18:38

Revisions

  1. Ernesto Mendoza Blanco created this gist Feb 13, 2012.
    39 changes: 39 additions & 0 deletions word-frequency-counter.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    (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])
    }
    })();