473,395 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Can Javascript count letter frequency?

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
7 9639
"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


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

"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
"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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ntg85 | last post by:
Can you use variables in string.count or string.find? I tried, and IDLE gives me errors. Here's the code: while list: print letter = raw_input("What letter? ") string.lower(letter) guess =...
0
by: mpilman | last post by:
The following oracle query purports to output a word count for all words found in a table column. Is this possible in MYSQL? Mike =================================================== Measuring...
10
by: Phil Stanton | last post by:
I am trying to count the fields in a queryDef in an external database. If I run this in the actaal database I get Fields.count = 6 correctly Private Sub ObjectName_DblClick(Cancel As Integer) ...
5
by: not_a_commie | last post by:
So I have a motherboard with multiple CPU sockets. It seems that if I create a StopWatch on one thread and then call the Elapsed member from a different thread that sometimes I get a tick count...
1
by: wkerplunk | last post by:
Below is what I have build with several different languages. It works great but I need help, I am stuck. When you click on an item in the dropdown autocomplete div it does a mousedown function...
3
by: waynejr25 | last post by:
can anyone debug my program and get it to run. #include <fstream> #include <iostream> #include <string> #include <cstdlib> #include <map> using namespace std;
5
by: svd | last post by:
so i'm attempting to make a program that reads in a file and then counts the frequency of each letter of the alphabet in the file and then outputs the letter with the frequency into a different...
13
by: umpsumps | last post by:
Hello, Here is my code for a letter frequency counter. It seems bloated to me and any suggestions of what would be a better way (keep in my mind I'm a beginner) would be greatly appreciated.. ...
3
by: nigelesquire | last post by:
Please help! I'm trying to clone and delete multiple rows with JavaScript. I need two delete buttons that work...! I only have one for now, but it's not working properly, the output count is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.