View Full Version : Frequency
josefkrzysztof
05-26-2005, 03:08 PM
Hi,
Please, do you know how extract from a <textarea> the 10 most frequent words together with frequency?
Thank you.
Jkrzysztof
Spudhead
05-26-2005, 04:47 PM
Well.. I shouldn't do this, as strictly speaking the idea is to offer advice rather then do it for you... but, I was bored, and your question seemed like an interesting challenge, and I'm rather sad :o
So:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Word Frequency Counter Thing</title>
<script language="JavaScript">
function cleanWord(word){
rv = word.toLowerCase();
rv = rv.replace(",","");
rv = rv.replace(".","");
return rv;
}
function calculate(text){
var words = text.split(" ");
for (i=0;i<words.length;i++){
words[i] = cleanWord(words[i]);
}
var wordsFrequencies = new Array();
var uniqueWordCount = 0;
// Count unique words and place into array
for (c=0;c<words.length;c++){
alreadyCounted=false;
currentWord = words[c];
// make sure it's a word we haven't already counted
for (e=0;e<wordsFrequencies.length;e++){
if (currentWord==wordsFrequencies[e][0]){
alreadyCounted = true;
}
}
if (!alreadyCounted){
// add to array
wordsFrequencies[uniqueWordCount] = new Array(2);
wordsFrequencies[uniqueWordCount][0] = currentWord;
wordsFrequencies[uniqueWordCount][1] = 0;
// count total instances
for (d=0;d<words.length;d++){
if (words[d]==wordsFrequencies[uniqueWordCount][0]){
wordsFrequencies[uniqueWordCount][1]++;
}
}
uniqueWordCount++;
}
}
// Sort array
var sorted = new Array();
var maxFreq = 0;
var sortCounter = 0;
// find the highest word frequency
for (c=0;c<wordsFrequencies.length;c++){
maxFreq = (wordsFrequencies[c][1]>maxFreq) ? wordsFrequencies[c][1] : maxFreq;
}
// count down through possible frequencies, adding words to new array if their frequency matches.
for (c=maxFreq;c>=1;c--){
for (d=0;d<wordsFrequencies.length;d++){
word = wordsFrequencies[d][0];
freq = wordsFrequencies[d][1];
if (freq==c){
sorted[sortCounter] = new Array(2);
sorted[sortCounter][0] = word;
sorted[sortCounter][1] = freq;
sortCounter++;
}
}
}
// create HTML list output
var reportLength = (sorted.length>10) ? 10 : sorted.length;
var strReport="<ol>";
for (c=0;c<reportLength;c++){
strReport += "<li>" + sorted[c][0] + ": " + sorted[c][1] + "</li>";
}
strReport += "</ol>";
document.getElementById("report").innerHTML = strReport;
}
</script>
</head>
<body>
<textarea id="t1" name="t1" rows=6 cols=30></textarea>
<input type="button" name="b1" id="b1" value="Calculate" onClick="calculate(document.getElementById('t1').value);">
<div id="report" name="report"></div>
</body>
</html>
I know that's javascript and you posted this in an ASP forum but hey, you can't win 'em all....
(Unlike Liverpool FC, mighty champions of Europe. Oh yes :D)
josefkrzysztof
05-26-2005, 05:39 PM
Congratulations!
It is true! It is JS! All right! No problems! Since the Bill Gates Revolution
and your Windows I am 'learning' as working with JS, ASP and another tools!
Your script works fine and It is very cool and useful. Believe!
Thank you.
Jkrzysztof
PS: Do you are bored? Do you are a Kids? No problem so. The world is blue! The adolescence must would be to have 100 years!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.