By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,504 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,504 IT Pros & Developers. It's quick & easy.

Can Javascript count letter frequency?

P: n/a
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

Aug 31 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Dung Ping" <du*******@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Such as:


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
Aug 31 '05 #2

P: n/a


Jim Davis wrote:
+) 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?

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 31 '05 #3

P: n/a

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:43**********************@newsread2.arcor-online.net...


Jim Davis wrote:
+) 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?


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
Aug 31 '05 #4

P: n/a
"Jim Davis" <ne********@vboston.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 31 '05 #5

P: n/a
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>

Aug 31 '05 #6

P: n/a
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.

Aug 31 '05 #7

P: n/a
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:d5**********@hotpop.com...
"Jim Davis" <ne********@vboston.com> writes:
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.


Very good point to keep in mind.

Thanks!

Jim Davis
Aug 31 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.