Skip to content

Keyword density with JavaScript

24 August 2011

I was asked to supply a script that counts how many times a word appears in a text – i.e. word density.

Here is the result. It is made for a single words only, but easily could be made for more (2 or 3 words) combinations. Hope to be useful.

var wordCounter = new function(){
    var s = arguments[0] || '';
    var wordsCounted = [];
    function toList(s){     return s.split(/\s/g);  }
    function count(arr) {
        if(arr.constructor !== Array) {  return;  }
        var res=[];
        var i=0;
        var len = arr.length;
        for(i=0; i<len; i++){
            if(typeof res[arr[i]]  == "undefined")  {  res[arr[i]] = 1;  }
            else{  res[arr[i]]+=1;  }
        }
        return res;
    }
    this.getWordsCounted = function() {  return wordsCounted; }
    this.setWordsCounted = function()  {  
        var s = arguments[0] || '';
        wordsCounted = count(toList(s));
    }
};

This script could be tested in windows prompt with

function doIt() {
	var s = arguments[0] || 'Introduction Hash tables are a permutation of associative arrays (i.e. name => value pairs). If you use PHP, then you are very familiar with this type of data structure already since all PHP arrays are associative. The Javascript language implements very loose and somewhat limited support for associative arrays. Any JavaScript array can use other objects as keys, making it a hash, but there exists no formal constructor for initializing them and it is more or less unweildy to work with. A short example of a hash in JavaScript would be as follows:';
	wordCounter.setWordsCounted(s);
	var r = wordCounter.getWordsCounted();
	for(var i in r){
		WScript.Echo(i + ': ' + r[i]);
	}
}

doIt();

or in browser (sample is fir Firebug)

function doIt() {
	var s = arguments[0] || 'Introduction Hash tables are a permutation of associative arrays (i.e. name => value pairs). If you use PHP, then you are very familiar with this type of data structure already since all PHP arrays are associative. The Javascript language implements very loose and somewhat limited support for associative arrays. Any JavaScript array can use other objects as keys, making it a hash, but there exists no formal constructor for initializing them and it is more or less unweildy to work with. A short example of a hash in JavaScript would be as follows:';
	wordCounter.setWordsCounted(s);
	var r = wordCounter.getWordsCounted();
	for(var i in r){
		console,log(i + ': ' + r[i]);
	}
}

doIt();
Advertisements
3 Comments
  1. I just said that a number of days in the past!!!

  2. how to modify this script to find combination of two or more keywords?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: