473,765 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9698
"Dung Ping" <du*******@yaho o.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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).

Conversationall y 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*******@yaho o.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********@vbo ston.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/rasterTriangleD OM.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:applicatio n id="lcount" applicationname ="Letter Frequency">
<script type="text/javascript">
function countFile(pathN ame) {
var tally=new Object();
var fso=new ActiveXObject(" Scripting.FileS ystemObject");
var f=fso.OpenTextF ile(pathName,1, false); //readonly, do not create
while(!f.AtEndO fStream) {
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.fro mCharCode(uc)+" : "
+(tally[String.fromChar Code(uc)]|0
+ tally[String.fromChar Code(uc+32)]|0)+"\n";
}
res+="</xmp>";
document.getEle mentById("resul ts").innerHTML= res;
}
</script>
</head>
<body>
<form>
Choose a file:
<input type="file" name="path" size="60">
<input type="button" value="Count" onclick="countF ile(this.form.p ath.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********@vbo ston.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
2043
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 = string.count(letter, list) if guess == -1: print "Try again!"
0
2740
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 of word frequency in text data SQL query: select Word, count(*) Frequency from (
10
3224
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) Dim MyDb As Database Dim MyQueryDef As QueryDef On Error GoTo ShowObject_Err
5
10855
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 that's a million miles away. My thinking is that I can subclass the StopWatch. Then when the Elapsed member is called, I can invoke it on the thread that the StopWatch class was created on. True? How? Or is there an easy way that I could scan...
1
3854
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 and send the item number to the xmlHttp and works awesome. Now I need to add an apply button next to it. so they can type in the complete number then hit apply and it does the same function. I cant get it to work with the script I have. The image...
3
7794
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
4540
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 file.. i didn't include the first part of the program because i'm pretty sure it's correct... i really have no clue what's wrong with this so any help would be great vector<int> occurrencesVec(26); int i; for(;;) { fin >> i; if...
13
3736
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.. def valsort(x): res = for key, value in x.items(): res.append((value, key))
3
3927
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 messing up. Problems:
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10156
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9951
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.