Can Javascript count letter frequency?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dung Ping

    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

  • Jim Davis

    #2
    Re: Can Javascript count letter frequency?

    "Dung Ping" <dungping5@yaho o.com> wrote in message
    news:1125507863 .163588.291400@ z14g2000cwz.goo glegroups.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).

    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


    Comment

    • Martin Honnen

      #3
      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

      Comment

      • Jim Davis

        #4
        Re: Can Javascript count letter frequency?


        "Martin Honnen" <mahotrash@yaho o.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


        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: Can Javascript count letter frequency?

          "Jim Davis" <newsmonkey@vbo ston.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/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Lee

            #6
            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: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>

            Comment

            • Dung Ping

              #7
              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.

              Comment

              • Jim Davis

                #8
                Re: Can Javascript count letter frequency?

                "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
                news:d5ntopju.f sf@hotpop.com.. .[color=blue]
                > "Jim Davis" <newsmonkey@vbo ston.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


                Comment

                Working...