Word Frequency Counter

Paste some text in the box below, and this script will count the number of unique words in your text, and sort them by frequency.
stuff ends up here



Code:
function freqount()	{
  var allwords = new Array();
  var freqwords = new Array();
  var fqindex = 0;
  var textresult = "";
  allwords = document.getElementById("target").value.split(" ");
	
  while(allwords.length > 0){
    var thisword = allwords[0];
    var thiswordcount = 1;

    for(i=1; i<allwords.length; i++){
      if(allwords[i] == thisword){thiswordcount++;}
    }
    allwords = purge(thisword, allwords);
    freqwords[fqindex] = new Array(thiswordcount, thisword);
    fqindex++;	
  }
	
  freqwords = sort(freqwords);

  textresult = "<table cellpadding=3 cellspacing=0>";

  for(i=0; i<freqwords.length; i++){
    textresult += "<tr><td>" + freqwords[i][0] + "</td><td>" + freqwords[i][1] + "</td></tr>";
  }
  textresult += "</table>"

  document.getElementById("result").innerHTML = textresult;
}

function sort(sarray){
  var returnarray = new Array();
  var highnum = 0;
  var raindex = 0;
	
  for(j=0; j<sarray.length; j++){
    if(sarray[j][0] > highnum){ highnum = sarray[j][0]; }
  }	

  for(n=highnum; n>=0; n--){
    for(m=0; m<sarray.length; m++){
      if(sarray[m][0] == n){
        returnarray[raindex] = sarray[m];
        raindex++;
      }
    }
  }
		
  return returnarray;	

}


function purge(pword, wordarray){

  var returnarray = new Array();
  var raindex = 0;

  for(k=0; k<wordarray.length; k++){
    if(wordarray[k] != pword){
      returnarray[raindex] = wordarray[k];
      raindex++;
    }
  }

  return returnarray;
}



This work is licensed under a Creative Commons 3.0
Attribution-Noncommercial-Share Alike License
Creative Commons License