3 Replies Latest reply: Aug 3, 2009 7:41 AM by Dave Saunders RSS

    Word Count - ignore numbers and symbols

    littlemookie Community Member

      Hi Guys,

       

      can anyone tell me how to ignore numbers and symbols by adjusting the script below, i.e., just counting letters in a whole document.


      myDoc = app.activeDocument

       

      h = 0

       

      for ( var j = 0; myDoc.stories.length > j; j++) {

       

      h = myDoc.stories[j].words.length + h

       

      }

       

      alert("You have " + h + " words in this document" )

       

      Cheers,

       

      Samuel

        • 1. Re: Word Count - ignore numbers and symbols
          Dave Saunders Community Member

          The script you need will be more complex. You're either going to have to look at each word before counting it or perhaps take the text of each story into a JavaScript string, use RegExp to eliminate words you want to ignore from the string and then count the remaining words.

           

          By the way, the script you're using won't count words in tables or footnotes.

           

          Dave

          • 2. Re: Word Count - ignore numbers and symbols
            pkahrel Community Member

            In CS3 and later you can use this:

             

            app.findGrepPreferences = null;
            app.findGrepPreferences.findWhat = "[\\u\\l][-\\u\\l]+";
            found = app.documents[0].findGrep ();
            alert ("You have " + found.length + " words in this document.")

             

            A "word" is defined here as any string of upper- and lower-case letters and hyphens. Maybe that's too strict, but you can tweak the GREP to suit your needs.

             

            Peter

            • 3. Re: Word Count - ignore numbers and symbols
              Dave Saunders Community Member

              Sometimes my mind gets into a state where it overlooks the fact that you can use GREP to just find things -- I think of it as a feature that changes text. D'oh. Peter's approach is far superior to what I blathered on about.

               

              Dave