• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Count word's occurences
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Count word's occurences


  • Subject: Re: Count word's occurences
  • From: Nigel Garvey <email@hidden>
  • Date: Mon, 08 Sep 2014 11:58:37 +0100

Guido Tangorra wrote on Mon, 08 Sep 2014 09:53:03 +0200:

>I'm generating a text document in TextWrangler, a sort of name list,
>dinamically with AS.
[snip]
>I need to print the occurrences of every word in this document too.
>So I would something like:
>
>*-----------*
>*apple 1*
>*grape 1*
>*banana 1*
>*ananas 1*
>*orange 1*
>*apple 2*
>*apple 3*
>*orange 2*
>*--------------*
[code snipped]
> I'm wondering if there's a better or a more efficient way to do the
same.

If you're entering the text into TextWrangler as it's generated, there
appears to be very little speed difference between using your vanilla
approach to examine what's in the document so far …

  #Word Counter
  on findLr(the_string, charTF)
    set AppleScript's text item delimiters to charTF --use of the searched string to separate bits
    set charNbr to ((count text items of the_string) - 1) --count those bits, minus 1
    if (charNbr is -1) then set charNbr to 0 -- catch when the_string is empty
    set AppleScript's text item delimiters to ""
    return charNbr
  end findLr

  on appendToTW(txt)
    tell application "TextWrangler" to set textSoFar to text of front window
    set n to findLr(textSoFar, txt) + 1
    tell application "TextWrangler" to make new line at end of text of front window with data (txt & space & n)
  end appendToTW

… and using TextWrangler's own facilities:

  on appendToTW(txt)
    tell application "TextWrangler"
      tell text of front window
        tell (find (txt & " [0-9]+") options {search mode:grep, backwards:true, match words:true})
          if (found) then
            set n to (word -1 of found text) + 1
          else
            set n to "1"
          end if
        end tell
        make new line at end with data (txt & space & n)
      end tell
    end tell
  end appendToTW

The latter, though, seems to be slightly faster when the text gets very
long.

NG

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden


  • Follow-Ups:
    • Re: Count word's occurences
      • From: "koenig.yvan" <email@hidden>
  • Prev by Date: Re: Count word's occurences
  • Next by Date: Re: Count word's occurences
  • Previous by thread: Re: Count word's occurences
  • Next by thread: Re: Count word's occurences
  • Index(es):
    • Date
    • Thread