Can Javascript count letter frequency?

D

Dung Ping

Such as:

<script>
//code

aaaa

zzzz

ccc
</script>

Counts will be produced: a=4, z=4, c=3. The is not for web site
uploading, but just to get statistics for own research. All data will
be in plain text. Everything will happen in a single computer, no
Internet, nor ISP.

I believe it is not a difficult job for a programing language, such as
C. With the MS Word the letters can be counted one type at a time. But
that is rather slow. Maybe Javascript can do the work so efficiently
as a fully-fledged programing language. Thanks.

Dung Ping
 
J

Jim Davis

Dung Ping said:

JavaScript can do this pretty easily (really any language that can navigate
a string and has the concept of a hash table or the like can do it easily).

Conversationally this might be:

+) Create a hash table (new Array()) to store the counts. The keys of this
array will be the characters in question.

+) Start looping from the first to last character of the string. Use the
String.length property to get the length and the String.charAt() method to
get the current character.

+) For each character see if there's already a hash-table entry for the
character. If there IS increment the value and move on. If there ISN'T
create an entry for the character and populate it's value with 1 (one).

+) Output your hash table in whatever format you like.

If this isn't clear let me know and I'll try to work up some code.

How you get the source string into the script might be a matter for
discussion (if you want the current script string you'd have to grab the
source from the DOM - but how you'd differentiate different scripts and such
is really something you'd have to answer).

Jim Davis
 
M

Martin Honnen

Jim said:
+) Create a hash table (new Array()) to store the counts. The keys of this
array will be the characters in question.

Why do you want to use
new Array()
and not
new Object()
if you want to have keys or properties which are characters?
 
J

Jim Davis

Martin Honnen said:
Why do you want to use
new Array()
and not
new Object()
if you want to have keys or properties which are characters?

Either way. Six of one, half-dozen of the other.

In this context they both work exactly the same except the Array will give
you access to the array properties and methods (which you may, or may not,
want to use).

Jim Davis
 
L

Lasse Reichstein Nielsen

Jim Davis said:
In this context they both work exactly the same except the Array will give
you access to the array properties and methods (which you may, or may not,
want to use).

In this case, yes, because you only use single characters as keys. But
there *is* one problem that will bite you eventually if you use
Javascript arrays as associative arrays: You can't use "length" as a
key.

/L
 
L

Lee

Dung Ping said:
Such as:

<script>
//code

aaaa

zzzz

ccc
</script>

Counts will be produced: a=4, z=4, c=3. The is not for web site
uploading, but just to get statistics for own research. All data will
be in plain text. Everything will happen in a single computer, no
Internet, nor ISP.

I believe it is not a difficult job for a programing language, such as
C. With the MS Word the letters can be counted one type at a time. But
that is rather slow. Maybe Javascript can do the work so efficiently
as a fully-fledged programing language. Thanks.

Sure. Almost like a fully-fledged programming language.
The following isn't very efficient, but it's pretty fast,
and it took under 10 minutes to write and debug.
Save this to your Windows desktop as "count.hta" and
double-click the icon:

<html>
<head>
<title>Letter Frequency</title>
<hta:application id="lcount" applicationname="Letter Frequency">
<script type="text/javascript">
function countFile(pathName) {
var tally=new Object();
var fso=new ActiveXObject("Scripting.FileSystemObject");
var f=fso_OpenTextFile(pathName,1,false); //readonly, do not create
while(!f.AtEndOfStream) {
var c=f.read(1);
tally[c]=(tally[c]|0)+1;
}
f.Close();
var res="<xmp>";
for(var uc=65;uc<91;uc++) {
res+=String.fromCharCode(uc)+": "
+(tally[String.fromCharCode(uc)]|0
+ tally[String.fromCharCode(uc+32)]|0)+"\n";
}
res+="</xmp>";
document.getElementById("results").innerHTML=res;
}
</script>
</head>
<body>
<form>
Choose a file:
<input type="file" name="path" size="60">
<input type="button" value="Count" onclick="countFile(this.form.path.value)">
</form>
<div id="results"></div>
</body>
</html>
 
J

Jim Davis

Lasse Reichstein Nielsen said:
In this case, yes, because you only use single characters as keys. But
there *is* one problem that will bite you eventually if you use
Javascript arrays as associative arrays: You can't use "length" as a
key.

Very good point to keep in mind.

Thanks!

Jim Davis
 
D

Dung Ping

I want to thank all for your help. I have saved all your messages, and
will study them very carefully. At this moment I still don't
understand many of the tags and procedures.

Thanks a million again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top