working word counter

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

    #1

    working word counter

    After getting some help working through my bugs, I have what seems to
    be a robust, working word counter script. I post it here to benefit
    others that might want this in the future and so that if I ever lose
    my copy I can come back here to find it :) Some other scripts that I
    used for inspiration failed when confronted with whitespace before the
    string or miscalculated when encountering linefeeds and other
    non-space spaces, so I made mine better. Definition of words for this
    exercise is contiguous groups of characters separated by whitespace.
    Maybe it will even be useful to somebody besides me.

    Cheers!
    --David

    <html>
    <!--Generated 02/12/04-->
    <!--modified 02/15/04-->
    <!--name of this file: "wordCounter.ht ml" -->

    <head>
    <title>Word Counter</title>
    <SCRIPT LANGUAGE="JavaS cript">

    function trim(data)
    {
    while (/\s/.test(data.char At(0)))
    {data=data.subs tring(1,data.le ngth);}
    while (/\s/.test(data.char At(data.length-1)))
    {data=data.subs tring(0,(data.l ength-1));}
    return data;
    }

    function countWords()
    {
    var input, wordList, output;
    input=document. forms[0].elements[0].value;
    input=trim(inpu t);
    wordList=input. split(/\s+/g);
    output=wordList .length;
    document.forms[0].elements[0].focus();
    return output;
    }

    </SCRIPT>
    </head>

    <body bgcolor="#66666 6"
    text="#00ff00"
    link="#ffffcc"
    vlink="#ffffcc" >

    Enter or paste text into the box below and press <b>count</b> to see
    the total number of words.
    <br>
    <br>
    <form onSubmit="retur n false">
    <center>
    <textarea rows="30" cols="100"></textarea>
    <br>
    <br>
    <button onClick="alert( 'total words = ' + countWords())"
    class="button"> count</button>
    </center>
    </form>

    </body>
    </html>
  • Grant Wagner

    #2
    Re: working word counter

    David wrote:
    [color=blue]
    > After getting some help working through my bugs, I have what seems to
    > be a robust, working word counter script. I post it here to benefit
    > others that might want this in the future and so that if I ever lose
    > my copy I can come back here to find it :) Some other scripts that I
    > used for inspiration failed when confronted with whitespace before the
    > string or miscalculated when encountering linefeeds and other
    > non-space spaces, so I made mine better. Definition of words for this
    > exercise is contiguous groups of characters separated by whitespace.
    > Maybe it will even be useful to somebody besides me.
    >
    > Cheers!
    > --David
    >
    > function trim(data)
    > {
    > while (/\s/.test(data.char At(0)))
    > {data=data.subs tring(1,data.le ngth);}
    > while (/\s/.test(data.char At(data.length-1)))
    > {data=data.subs tring(0,(data.l ength-1));}
    > return data;
    > }[/color]

    trim() would be better implemented as:

    String.prototyp e.LTrim = new Function("retur n this.replace(/^\\s+/,'')")
    String.prototyp e.RTrim = new Function("retur n this.replace(/\\s+$/,'')")
    String.prototyp e.Trim = new Function("retur n
    this.replace(/^\\s+|\\s+$/g,'')")

    as shown in the FAQ at <url: http://jibbering.com/faq/#FAQ4_16 />
    [color=blue]
    > function countWords()
    > {
    > var input, wordList, output;
    > input=document. forms[0].elements[0].value;
    > input=trim(inpu t);[/color]

    Using the Trim() method outlined above, this would become:

    input = input.Trim();

    --
    | Grant Wagner <gwagner@agrico reunited.com>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *


    * Internet Explorer DOM Reference available at:
    *
    http://msdn.microsoft.com/workshop/a...ence_entry.asp

    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 7 / Mozilla
    * http://www.mozilla.org/docs/web-deve...upgrade_2.html


    Comment

    • Dr John Stockton

      #3
      Re: working word counter

      JRS: In article <6769ced8.04021 52351.59478cc2@ posting.google. com>, seen
      in news:comp.lang. javascript, David <david.lindsey@ mindspring.com>
      posted at Sun, 15 Feb 2004 23:51:19 :-[color=blue]
      >After getting some help working through my bugs, I have what seems to
      >be a robust, working word counter script.[/color]
      [color=blue]
      ><!--Generated 02/12/04-->[/color]

      this is an international newsgroup; please use internationally-
      comprehensible notation. The above, in most countries, is 2nd December
      2004. The correct form is 2004-02-12; or put the month as Feb and use
      YYYY.
      [color=blue]
      >input=trim(inp ut);
      >wordList=input .split(/\s+/g);
      >output=wordLis t.length;[/color]

      What purpose does trim serve here? and /g?

      I find
      Q = X.replace(/\S+/g, 'a').replace(/\s+/g, '').length
      to be significantly faster than
      Q = X.split(/\s+/).length
      for strings with many words. The latter creates many Objects.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      Working...