Connecting Tech Pros Worldwide Forums | Help | Site Map

Can Javascript count letter frequency?

Dung Ping
Guest
 
Posts: n/a
#1: Aug 31 '05
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


Jim Davis
Guest
 
Posts: n/a
#2: Aug 31 '05

re: Can Javascript count letter frequency?


"Dung Ping" <dungping5@yahoo.com> wrote in message
news:1125507863.163588.291400@z14g2000cwz.googlegr oups.com...[color=blue]
> Such as:[/color]

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


Martin Honnen
Guest
 
Posts: n/a
#3: Aug 31 '05

re: Can Javascript count letter frequency?




Jim Davis wrote:
[color=blue]
> +) Create a hash table (new Array()) to store the counts. The keys of this
> array will be the characters in question.[/color]

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/
Jim Davis
Guest
 
Posts: n/a
#4: Aug 31 '05

re: Can Javascript count letter frequency?



"Martin Honnen" <mahotrash@yahoo.de> wrote in message
news:4315fcd8$0$2103$9b4e6d93@newsread2.arcor-online.net...[color=blue]
>
>
> Jim Davis wrote:
>[color=green]
>> +) Create a hash table (new Array()) to store the counts. The keys of
>> this array will be the characters in question.[/color]
>
> Why do you want to use
> new Array()
> and not
> new Object()
> if you want to have keys or properties which are characters?[/color]

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


Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#5: Aug 31 '05

re: Can Javascript count letter frequency?


"Jim Davis" <newsmonkey@vboston.com> writes:
[color=blue]
> 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).[/color]

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 - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Lee
Guest
 
Posts: n/a
#6: Aug 31 '05

re: Can Javascript count letter frequency?


Dung Ping said:[color=blue]
>
>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.[/color]

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>

Jim Davis
Guest
 
Posts: n/a
#7: Aug 31 '05

re: Can Javascript count letter frequency?


"Lasse Reichstein Nielsen" <lrn@hotpop.com> wrote in message
news:d5ntopju.fsf@hotpop.com...[color=blue]
> "Jim Davis" <newsmonkey@vboston.com> writes:
>[color=green]
>> 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).[/color]
>
> 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.[/color]

Very good point to keep in mind.

Thanks!

Jim Davis


Dung Ping
Guest
 
Posts: n/a
#8: Aug 31 '05

re: Can Javascript count letter frequency?


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.

Closed Thread