Count words, characters and lines.

Count words, characters and lines.

18
Basic UserBasic User
18

    Jun 22, 2011#1

    Mofi, or anyone:

    Can someone help me? What I want to do is to have text "bracketed" with delimiters, have the script select the text in the bracketed area, count the words, characters and lines within this area, deselect it, and put the status just below the bracketed area.

    For instance:

    // begin calculation
    this is some text, with two lines.

    This is a second line.
    // end calculation

    Total lines: 3
    Total characters (including eol): 62
    Total words: 12

    The above would count starting with the first "this" and ending with "second line." Counting eol (end of line) is AOK, as well as blank lines.

    A script, no a macro is what I would like to have.

    6,684586
    Grand MasterGrand Master
    6,684586

      Jun 22, 2011#2

      Here is the script which should to the job. You and anybody else can use this script as template how to do statistic analysis with a script in UltraEdit like Search - Word Count offers.

      Code: Select all

      if (UltraEdit.document.length > 0) {  // Is any file currently opened?
      
         // Define the environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         UltraEdit.activeDocument.hexOff();
         UltraEdit.perlReOn();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=true;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean") {
            UltraEdit.activeDocument.findReplace.searchInColumn=false;
         }
         // Move caret to top of the file.
         UltraEdit.activeDocument.top();
      
         // Determine once type of line termination.
         var sLineTerm = "\r\n";           // Default is DOS.
         if (typeof(UltraEdit.activeDocument.lineTerminator) == "number") {
            if (UltraEdit.activeDocument.lineTerminator == 2) sLineTerm = "\n";
            else if (UltraEdit.activeDocument.lineTerminator == 3) sLineTerm = "\r";
         }
         else {
            // This version of UE/UES does not offer line terminator property.
            UltraEdit.activeDocument.findReplace.regExp=false;
            if (!UltraEdit.activeDocument.findReplace.find(sLineTerm)) {
               sLineTerm = "\n";           // Not DOS, perhaps UNIX.
               if (!UltraEdit.activeDocument.findReplace.find(sLineTerm)) {
                  sLineTerm = "\r";        // Also not UNIX, perhaps MAC.
                  if (!UltraEdit.activeDocument.findReplace.find(sLineTerm)) {
                     sLineTerm = "\r\n";   // No line terminator, use DOS.
                  }
               }
            }
            UltraEdit.activeDocument.top();
         }
      
         // Find in the file with a Perl regular expression blocks to evaluate.
         UltraEdit.activeDocument.findReplace.regExp=true;
         while (UltraEdit.activeDocument.findReplace.find("(?s)// begin calculation.*// end calculation")) {
      
            // Remove from found and selected block the marker strings.
            var sBlock = UltraEdit.activeDocument.selection.replace(/\/\/ begin calculation.*(?:\r\n|\n|\r)|\/\/ end calculation/g,"");
            if (sBlock == "") continue;  // Ignore empty blocks.
      
            // Verify if at least 1 line terminator is found in the remaining block.
            var nLineCount = sBlock.indexOf(sLineTerm);
            if (nLineCount < 0) {        // If no line terminator found, the block
               nLineCount = 1;           // contains just a part of a line counted
            }                            // nevertheless as 1 line.
            else {
               // Block contains 1 or more lines. Split the block up into
               // an array of strings each containing an entire line.
               var asLines = sBlock.split(sLineTerm);
               nLineCount = asLines.length;
               // If the block ends with a line termination (last string is
               // empty), decrease the number of lines by 1 to get correct result.
               if (asLines[nLineCount-1] == "") nLineCount--;
            }
      
            // Replace all sequences of whitspace characters (spaces,
            // tabs, line terminators, form-feeds) by a single space.
            // This expression defines which string is interpreted as "word".
            var sText = sBlock.replace(/\s+/g," ");
      
            // Split the string of words into an array of strings each containing
            // one word. The number of strings is equal the number of words.
            var asWords = sText.split(" ");
            var nWordCount = asWords.length;
      
            // But if text ends with a space character (last string empty),
            // the count must be decreased by one to get correct word count.
            if (asWords[nWordCount-1] == "") nWordCount--;
            // Also if the text starts with a space character (first string
            // empty), the count must be decreased by one for correct count.
            if (asWords[0] == "") nWordCount--;
      
            // Write the result into the file below the still selected block.
            // Selection is discarded with moving the caret to end of line
            // even if the caret is already at end of the marker line.
            UltraEdit.activeDocument.key("END");
            UltraEdit.activeDocument.write(sLineTerm + sLineTerm + "Total lines: " +
               nLineCount + sLineTerm + "Total characters (including eol): " +
               sBlock.length + sLineTerm + "Total words: " + nWordCount);
         }
      }

      18
      Basic UserBasic User
      18

        Jun 22, 2011#3

        This is fantastic! Thanks Loads! :) :) :)

        I'm on another board and they said a post with 209 words was "too long" (it was a POLITICAL FORUM), but I had made other posts with 506 and 1646 words, and they didn't complain. Then when I randomly picked two other posts (not mine) and they had 855 and 3578 words.

        So, when I prepare my post offline, I will run this script against the post, so it can automatically puts in the count.

        This issue is with a non-profit (supposedly) organization (AARP), and they can't even follow their own TOS... in other words like a parent... "Do as I say, not as I do!"