Re: Count word's occurences
Re: Count word's occurences
- Subject: Re: Count word's occurences
- From: Shane Stanley <email@hidden>
- Date: Mon, 08 Sep 2014 23:51:43 +1000
On 8 Sep 2014, at 10:56 pm, Shane Stanley <email@hidden> wrote:
On 8 Sep 2014, at 7:36 pm, Shane Stanley <email@hidden> wrote:
It's pretty easy to do with an ASObjC-based library in Mavericks or later -- there's a class just for the job:
Err, there's a class for an entirely different job.
Well it can do the job -- the key is to keep the values in a separate variable, and generate the counts from there, rather than from the TextWrangler document.
So the library would be this:
use framework "Foundation"
on makeSet() return current application's NSCountedSet's alloc()'s initWithCapacity:5000 end makeSet
on entryFrom:aString inSet:theSet theSet's addObject:aString return aString & space & (theSet's countForObject:aString) end entryFrom:inSet:
And each time you call the second handler, you'd get a string back for adding to the document. So for example:
-- build a list of words to test with tell application "TextWrangler" to set theWords to contents of words of text document 2 -- document full of words -- get a new set tell script "<script name>" to set theSet to makeSet() repeat with aWord in theWords -- get an entry tell script "<script name>" to set newText to (its entryFrom:aWord inSet:theSet) -- add it to the document tell application "TextWrangler" to make new line at end of text of front window with data newText end repeat
But the real time-consuming part is actually adding to the document. I did a comparison running the above, and then running it again but replacing the call to the library in the repeat loop with a simple 'set newText to "black 1"', and there was no meaningful difference in running time. -- Shane Stanley < email@hidden> < www.macosxautomation.com/applescript/apps/> |
_______________________________________________
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