saroye
08-25-2003, 07:35 PM
I have a great word counter script, but I need a bit more functionality with it than my limited skills allow.
Currently, the count results are displayed in a results textarea box. I would like for the results to be displayed in individual text boxes in addition to the textarea box.
It would also really cool if the counter results were displayed in real time in the individual text boxes as words are being typed into the main textarea box.
Any help would be great. Here's the script:
<html>
<head>
<script language = "JavaScript">
function process1 (count) { // for words
m=new Array(10000);
m1=new Array(10000);
N=new Array (10000);
for (i=0;i<=1;i++) // which is chosen
{ if ( count.radio1[i].checked)
{ ch=i;
}
}
A=count.message.value; // original message
B="";
A=" " + A+" ";
A=A.toUpperCase(); // changes all alphas to Upper case
if (ch==0) // get rid of everything but apha's
{ condense1(count);
}
else
{ lesscondense(count);
}
for (i=1;i<=A.length;i++) // trims leading spaces and multiple spaces
{ if ((!(A.charAt(i)==" ")) || (!(A.charAt(i-1)==" ")))
{B=B + A.charAt(i);
}
}
//count.result1.value=B;
B=B+" "; // makes sure there is a space at end
k=0; str=" ";
for (i=0;i<=B.length;i++)
{ k1=B.indexOf(str,k);
if (k1==-1) //end of string B
{ Numwords=i-1;
break;
}
m[i+1]=B.substring(k,k1); // places all the words into an array m
k=k1+1;
}
//count.result1.value=B;
C="";
NN=0;
for (i=1;i<=Numwords; i++) // Numwords is total number of words
{ if (!(m[i]=="")) // only looks at m1 words that have not been processed before (not empty)
{ NN=NN+1; //unique word stored in m1 array
m1[NN]=m[i];
N[NN]=1; // initialize counter for word
for (j=i+1;j<=Numwords+1;j++) //counts and makes m1 elements with unique word empty.
{ if (m1[NN]==m[j])
{ N[NN]=N[NN]+1;
m[j]="";
}
}
}
}
C=C+"Unique:" + NN+" Total:" + Numwords+"\n";
C=C+"Freq.\tWord\n";
for (i=1;i<=NN;i++) // sets up C for showing
{ C=C + N[i]+ "\t" + m1[i] + "\n";
}
count.result1.value=C;
}
function condense1(count) { // allows only alpha's and spaces for choice 1
C="";
for (i=0;i<=A.length-1;i++)
{ k=A.charCodeAt(i);
if (((k>64) && (k<91)) || (k==32))
{ C=C+A.charAt(i);
}
else
{ C=C+" ";
}
}
A=C;
//count.result1.value=B;
}
function lesscondense(count) { // allows all characters from space on
C="";
for (i=0;i<=A.length-1;i++)
{ k=A.charCodeAt(i);
if (k>31)
{ C=C+A.charAt(i);
}
else
{ C=C+" ";
}
}
A=C;
}
//========================================
function letters1(count) { // counting characters upto unicode #255
mN=new Array(5000);
for (i=0;i<=1;i++) // which is chosen
{ if ( count.radio2[i].checked)
{ ch1=i;
}
}
A=count.message.value;
T=0;
for (i=0;i<=5000;i++) // initialize Array mN to 0
{ mN[i]=0;
}
for (i=0;i<=A.length-1;i++) // k is character and counted in mN[k]
{ k=A.charCodeAt(i);
mN[k]=mN[k]+1;
}
//count.result1.value=E;
D="";
D=D+"Freq.\tLetter\n"
if (ch1==0) // shows only alpha's and total number of letters T
{ for (i=65; i<91;i++)
{ if (!(mN[i]==0))
{ D=D + mN[i] + "\t"+ String.fromCharCode(i) + "\n";
T=T+mN[i];
}
}
for (i=97;i<=122;i++)
{ if (!(mN[i]==0))
{ D=D+mN[i]+"\t"+String.fromCharCode(i)+"\n";
T=T+mN[i];
}
}
}
else // shows all characters from space (32) to unicode 255
{ for (i=32; i<=255;i++)
{ if (!(mN[i]==0))
{ D=D + mN[i] + "\t"+ String.fromCharCode(i) + "\n";
T=T+mN[i];
}
}
}
count.result1.value=D;
count.total.value=T;
count.wordav.value=Math.round(T/Numwords*10000)/10000; // rounds off
}
function sortalph(count) { // sort words alphabetically
for (i=1;i<=NN-1;i++)
{ for (j=i+1;j<=NN;j++)
{ if (m1[i]>m1[j])
{ temp=m1[i];
m1[i]=m1[j];
m1[j]=temp;
temp=N[i];
N[i]=N[j];
N[j]=temp;
}
}
}
C="Unique words:" + NN+" Total words:" + Numwords+"\n";
C=C+"Freq.\tWord\n";
for (i=1;i<=NN;i++)
{ C=C + N[i]+ "\t" + m1[i] + "\n";
}
count.result1.value=C;
}
function sortfreq(count) { // sorts words according to frequency
for (i=1;i<=NN-1;i++)
{ for (j=i+1;j<=NN;j++)
{ if (N[i]<N[j])
{ temp=m1[i];
m1[i]=m1[j];
m1[j]=temp;
temp=N[i];
N[i]=N[j];
N[j]=temp;
}
}
}
C="Unique words:" + NN+" Total words:" + Numwords+"\n";
C=C+"Freq.\tWord\n";
for (i=1;i<=NN;i++)
{ C=C + N[i]+ "\t" + m1[i] + "\n";
}
count.result1.value=C;
}
function sortletters(count) { //sorts characters according to frequency
nletters=new Array(256);
for (i=1;i<=255;i++)
{ nletters[i]=i;
}
D="";
D=D+"Freq.\tLetter\n"
if (ch1==0) // sort for alpha's
{ for (i=65;i<=122;i++)
{ for (j=i+1;j<=122;j++)
{ if (mN[i]<mN[j])
{ temp=mN[i];
mN[i]=mN[j];
mN[j]=temp;
temp=nletters[i];
nletters[i]=nletters[j];
nletters[j]=temp;
}
}
}
for (i=65; i<91;i++) // show for upper case letters
{ if (!(mN[i]==0))
D=D + mN[i] + "\t"+ String.fromCharCode(nletters[i]) + "\n"
}
for (i=97;i<=122;i++) // show for lower case letters
{ if (!(mN[i]==0))
D=D + mN[i] + "\t"+ String.fromCharCode(nletters[i]) + "\n"
}
}
else // show from space to 255
{ for (i=32;i<=255;i++)
{ for (j=i+1;j<=122;j++)
{ if (mN[i]<mN[j])
{ temp=mN[i];
mN[i]=mN[j];
mN[j]=temp;
temp=nletters[i];
nletters[i]=nletters[j];
nletters[j]=temp;
}
}
}
for (i=32; i<255;i++)
{ if (!(mN[i]==0))
D=D + mN[i] + "\t"+ String.fromCharCode(nletters[i]) + "\n"
}
}
count.result1.value=D;
count.total.value=T;
}
</script>
</head>
<body onload="process1(document.count)">
<form name ="count">
Count pure words<INPUT type="radio" name="radio1" value="1" CHECKED>
Count everything as words <INPUT type ="radio" name="radio1" value="0">
<INPUT id=button1 name="button1" onclick="process1(document.count)" style="height: 24px; width: 165px; font-weight: bold" type="button" value="COUNT WORDS">
<INPUT id=button3 name="button3" onclick="sortfreq(document.count)" style=" HEIGHT: 24px; WIDTH: 142px" type="button" value="Word Sort: Frequency">
<INPUT id=button4 name="button4" onclick="sortalph(document.count)" style=" HEIGHT: 24px; WIDTH: 142px" type="button" value="Word Sort: Alphabetic">
<TEXTAREA cols=30 name=message rows=12 style="background-color: #ffffff; font-size: 10pt" wrap=PHYSICAL>WORD COUNT INPUT BOX </TEXTAREA>
<TEXTAREA cols=33 name=result1 rows=12 style="font-size: 10pt; font-family: Arial" wrap=PHYSICAL>WORD COUNT OUTPUT BOX</TEXTAREA>
Letters Only <INPUT type = "radio" name="radio2" value="1" CHECKED>
All symbols <INPUT type="radio" name="radio2" value="0" >
<INPUT name="button5" onclick="letters1(document.count)" style="HEIGHT: 24px; WIDTH: 112px" type="button" value="Character Count"> <br>
Total letters in all words:<INPUT name="total" size="10" >
Average letters per word:<INPUT name=wordav style="HEIGHT: 22px; WIDTH: 70px">
<input type="reset" value="Reset" name="B1">
</FORM>
</body>
</html>
Thanks in advance for your help!
Currently, the count results are displayed in a results textarea box. I would like for the results to be displayed in individual text boxes in addition to the textarea box.
It would also really cool if the counter results were displayed in real time in the individual text boxes as words are being typed into the main textarea box.
Any help would be great. Here's the script:
<html>
<head>
<script language = "JavaScript">
function process1 (count) { // for words
m=new Array(10000);
m1=new Array(10000);
N=new Array (10000);
for (i=0;i<=1;i++) // which is chosen
{ if ( count.radio1[i].checked)
{ ch=i;
}
}
A=count.message.value; // original message
B="";
A=" " + A+" ";
A=A.toUpperCase(); // changes all alphas to Upper case
if (ch==0) // get rid of everything but apha's
{ condense1(count);
}
else
{ lesscondense(count);
}
for (i=1;i<=A.length;i++) // trims leading spaces and multiple spaces
{ if ((!(A.charAt(i)==" ")) || (!(A.charAt(i-1)==" ")))
{B=B + A.charAt(i);
}
}
//count.result1.value=B;
B=B+" "; // makes sure there is a space at end
k=0; str=" ";
for (i=0;i<=B.length;i++)
{ k1=B.indexOf(str,k);
if (k1==-1) //end of string B
{ Numwords=i-1;
break;
}
m[i+1]=B.substring(k,k1); // places all the words into an array m
k=k1+1;
}
//count.result1.value=B;
C="";
NN=0;
for (i=1;i<=Numwords; i++) // Numwords is total number of words
{ if (!(m[i]=="")) // only looks at m1 words that have not been processed before (not empty)
{ NN=NN+1; //unique word stored in m1 array
m1[NN]=m[i];
N[NN]=1; // initialize counter for word
for (j=i+1;j<=Numwords+1;j++) //counts and makes m1 elements with unique word empty.
{ if (m1[NN]==m[j])
{ N[NN]=N[NN]+1;
m[j]="";
}
}
}
}
C=C+"Unique:" + NN+" Total:" + Numwords+"\n";
C=C+"Freq.\tWord\n";
for (i=1;i<=NN;i++) // sets up C for showing
{ C=C + N[i]+ "\t" + m1[i] + "\n";
}
count.result1.value=C;
}
function condense1(count) { // allows only alpha's and spaces for choice 1
C="";
for (i=0;i<=A.length-1;i++)
{ k=A.charCodeAt(i);
if (((k>64) && (k<91)) || (k==32))
{ C=C+A.charAt(i);
}
else
{ C=C+" ";
}
}
A=C;
//count.result1.value=B;
}
function lesscondense(count) { // allows all characters from space on
C="";
for (i=0;i<=A.length-1;i++)
{ k=A.charCodeAt(i);
if (k>31)
{ C=C+A.charAt(i);
}
else
{ C=C+" ";
}
}
A=C;
}
//========================================
function letters1(count) { // counting characters upto unicode #255
mN=new Array(5000);
for (i=0;i<=1;i++) // which is chosen
{ if ( count.radio2[i].checked)
{ ch1=i;
}
}
A=count.message.value;
T=0;
for (i=0;i<=5000;i++) // initialize Array mN to 0
{ mN[i]=0;
}
for (i=0;i<=A.length-1;i++) // k is character and counted in mN[k]
{ k=A.charCodeAt(i);
mN[k]=mN[k]+1;
}
//count.result1.value=E;
D="";
D=D+"Freq.\tLetter\n"
if (ch1==0) // shows only alpha's and total number of letters T
{ for (i=65; i<91;i++)
{ if (!(mN[i]==0))
{ D=D + mN[i] + "\t"+ String.fromCharCode(i) + "\n";
T=T+mN[i];
}
}
for (i=97;i<=122;i++)
{ if (!(mN[i]==0))
{ D=D+mN[i]+"\t"+String.fromCharCode(i)+"\n";
T=T+mN[i];
}
}
}
else // shows all characters from space (32) to unicode 255
{ for (i=32; i<=255;i++)
{ if (!(mN[i]==0))
{ D=D + mN[i] + "\t"+ String.fromCharCode(i) + "\n";
T=T+mN[i];
}
}
}
count.result1.value=D;
count.total.value=T;
count.wordav.value=Math.round(T/Numwords*10000)/10000; // rounds off
}
function sortalph(count) { // sort words alphabetically
for (i=1;i<=NN-1;i++)
{ for (j=i+1;j<=NN;j++)
{ if (m1[i]>m1[j])
{ temp=m1[i];
m1[i]=m1[j];
m1[j]=temp;
temp=N[i];
N[i]=N[j];
N[j]=temp;
}
}
}
C="Unique words:" + NN+" Total words:" + Numwords+"\n";
C=C+"Freq.\tWord\n";
for (i=1;i<=NN;i++)
{ C=C + N[i]+ "\t" + m1[i] + "\n";
}
count.result1.value=C;
}
function sortfreq(count) { // sorts words according to frequency
for (i=1;i<=NN-1;i++)
{ for (j=i+1;j<=NN;j++)
{ if (N[i]<N[j])
{ temp=m1[i];
m1[i]=m1[j];
m1[j]=temp;
temp=N[i];
N[i]=N[j];
N[j]=temp;
}
}
}
C="Unique words:" + NN+" Total words:" + Numwords+"\n";
C=C+"Freq.\tWord\n";
for (i=1;i<=NN;i++)
{ C=C + N[i]+ "\t" + m1[i] + "\n";
}
count.result1.value=C;
}
function sortletters(count) { //sorts characters according to frequency
nletters=new Array(256);
for (i=1;i<=255;i++)
{ nletters[i]=i;
}
D="";
D=D+"Freq.\tLetter\n"
if (ch1==0) // sort for alpha's
{ for (i=65;i<=122;i++)
{ for (j=i+1;j<=122;j++)
{ if (mN[i]<mN[j])
{ temp=mN[i];
mN[i]=mN[j];
mN[j]=temp;
temp=nletters[i];
nletters[i]=nletters[j];
nletters[j]=temp;
}
}
}
for (i=65; i<91;i++) // show for upper case letters
{ if (!(mN[i]==0))
D=D + mN[i] + "\t"+ String.fromCharCode(nletters[i]) + "\n"
}
for (i=97;i<=122;i++) // show for lower case letters
{ if (!(mN[i]==0))
D=D + mN[i] + "\t"+ String.fromCharCode(nletters[i]) + "\n"
}
}
else // show from space to 255
{ for (i=32;i<=255;i++)
{ for (j=i+1;j<=122;j++)
{ if (mN[i]<mN[j])
{ temp=mN[i];
mN[i]=mN[j];
mN[j]=temp;
temp=nletters[i];
nletters[i]=nletters[j];
nletters[j]=temp;
}
}
}
for (i=32; i<255;i++)
{ if (!(mN[i]==0))
D=D + mN[i] + "\t"+ String.fromCharCode(nletters[i]) + "\n"
}
}
count.result1.value=D;
count.total.value=T;
}
</script>
</head>
<body onload="process1(document.count)">
<form name ="count">
Count pure words<INPUT type="radio" name="radio1" value="1" CHECKED>
Count everything as words <INPUT type ="radio" name="radio1" value="0">
<INPUT id=button1 name="button1" onclick="process1(document.count)" style="height: 24px; width: 165px; font-weight: bold" type="button" value="COUNT WORDS">
<INPUT id=button3 name="button3" onclick="sortfreq(document.count)" style=" HEIGHT: 24px; WIDTH: 142px" type="button" value="Word Sort: Frequency">
<INPUT id=button4 name="button4" onclick="sortalph(document.count)" style=" HEIGHT: 24px; WIDTH: 142px" type="button" value="Word Sort: Alphabetic">
<TEXTAREA cols=30 name=message rows=12 style="background-color: #ffffff; font-size: 10pt" wrap=PHYSICAL>WORD COUNT INPUT BOX </TEXTAREA>
<TEXTAREA cols=33 name=result1 rows=12 style="font-size: 10pt; font-family: Arial" wrap=PHYSICAL>WORD COUNT OUTPUT BOX</TEXTAREA>
Letters Only <INPUT type = "radio" name="radio2" value="1" CHECKED>
All symbols <INPUT type="radio" name="radio2" value="0" >
<INPUT name="button5" onclick="letters1(document.count)" style="HEIGHT: 24px; WIDTH: 112px" type="button" value="Character Count"> <br>
Total letters in all words:<INPUT name="total" size="10" >
Average letters per word:<INPUT name=wordav style="HEIGHT: 22px; WIDTH: 70px">
<input type="reset" value="Reset" name="B1">
</FORM>
</body>
</html>
Thanks in advance for your help!