5 Replies Latest reply on Dec 11, 2012 7:17 PM by Jo_2005

    Count the total number of specific words on each page

    Jo_2005 Level 1

      I have a script which counts the number of times the word "Manager" appears on each page of a report.

       

      This is what is reported back into the console after the script is run:

       

      Count No of Managers 1 Page Number 1

      Count No of Managers 2 Page Number 1

      Count No of Managers 3 Page Number 1

      Count No of Managers 4 Page Number 1

      Count No of Managers 5 Page Number 1

      Count No of Managers 1 Page Number 2

      Count No of Managers 2 Page Number 2

      Count No of Managers 3 Page Number 2

       

      I need the console to report back with only the total number of times the word manager appears on each page (once) and not iterate through each one.

       

      Can any one please advise how this can be achieved?

       

      Script as follows:

       

      var numpages = this.numPages;

      for (var k=0; k < numpages; k++)
      {
      {
      var numWords = this.getPageNumWords(k);

      var count = 0;

      for (var i=0; i<numWords; i++)
      {
      var ckWord = this.getPageNthWord(k, i, true);
      var q = this.getPageNthWordQuads(k, i);
      // Convert quads in default user space to rotated
      // User space used by Links.
      m = (new Matrix2D).fromRotated(this,0);
      mInv = m.invert()
      r = mInv.transform(q)
      r=r.toString()
      r = r.split(",");
      if ( ckWord === "Manager")
      {
      count++;
      console.println("Count No of Managers " +  count + " Page Number " + (k+1));
      }
      }
      }
      }

        • 1. Re: Count the total number of specific words on each page
          George_Johnson MVP & Adobe Community Professional

          You just have to move the statement outside of the inner loop:

           

          var numpages = this.numPages;

           

          for (var k = 0; k < numpages; k++) {

           

              var numWords = this.getPageNumWords(k);

              var count = 0;

           

              for (var i = 0; i < numWords; i++) {

                  var ckWord = this.getPageNthWord(k, i, true);

                  var q = this.getPageNthWordQuads(k, i);

                  if (ckWord === "Manager") {

                      count++;

                  }

              }

           

              console.println("Count No of Managers " +  count + " Page Number " + (k+1));

           

          }

          • 2. Re: Count the total number of specific words on each page
            Jo_2005 Level 1

            Thank you once again for your assistance George, your help has been much appreciated.

             

            By moving the statement outside of the inner loop the total count of the word "manager" appears once in the console (perfect!)

             

            Console reports back as follows:

             

            Count No of Managers 5 Page Number 1

            Count No of Managers 3 Page Number 2

            • 3. Re: Count the total number of specific words on each page
              Jo_2005 Level 1

              The script requires some further modification.

               

              I need an ongoing tally of the number of times the word manager appears in the report by adding up progressive totals for each page.

               

              Currently the console reports back:

               

              Count No of Managers 5 Page Number 1

              Count No of Managers 3 Page Number 2

               

              I need to to revise the count for page 2

               

              eg Count No of Managers 8 Page Number 2

               

              Can you please advise how I can modify the script for an ongoing tally for each page?

               

              thank you very much.

              • 4. Re: Count the total number of specific words on each page
                George_Johnson MVP & Adobe Community Professional

                Just initialize the count variable before you enter the first loop:

                 

                var numpages = this.numPages;

                var count = 0;

                 

                for (var k = 0; k < numpages; k++) {

                 

                    var numWords = this.getPageNumWords(k);

                 

                    for (var i = 0; i < numWords; i++) {

                 

                        var ckWord = this.getPageNthWord(k, i, true);

                        var q = this.getPageNthWordQuads(k, i);

                 

                        if (ckWord === "Manager") {

                            count++;

                        }

                    }

                    console.println("Count No of Managers " +  count + " Page Number " + (k+1));

                }

                • 5. Re: Count the total number of specific words on each page
                  Jo_2005 Level 1

                  Thank you very much for your help

                   

                  By initializing the count before the loop the number of words are tallied progressively.

                   

                  Console Reports Back:

                   

                  Count No of Managers 5 Page Number 1

                  Count No of Managers 8 Page Number 2

                  Count No of Managers 11 Page Number 3