Re: Count word's occurences
Re: Count word's occurences
- Subject: Re: Count word's occurences
- From: Nigel Garvey <email@hidden>
- Date: Wed, 24 Sep 2014 21:04:02 +0100
Deivy Petrescu wrote on Tue, 23 Sep 2014 17:55:53 -0400:
>On Sep 16, 2014, at 07:49 , Nigel Garvey
><email@hidden> wrote:
>> set theDocName to "theDocName"
>> set wordcount to "|" -- Now text. Initialise to the bar at the end.
>> tell application "TextWrangler"
>> activate
>> set myDocument to make new text window with properties
>>{name:theDocName}
>> set show line numbers of myDocument to false
>> end tell
>>
>> on idle
>> global wordcount
>> tell application "JSON Helper" to set myFruit to fetch JSON from
>>mySource
>> set lFruit to "|" & myFruit & "|" ---> added this line
>> if (lFruit is in wordcount) then
>> set astid to AppleScript's text item delimiters
>> set AppleScript's text item delimiters to lFruit
>> set ti2 to text item 2 of wordcount
>> set AppleScript's text item delimiters to astid
>> set ti1 to text 1 thru (-1 - (count ti2)) of wordcount
>> set kount to (word 1 of ti2) + 1
>> set wordcount to ti1 & kount & text from word 2 to -1 of ti2
>> else
>> -- Append new fruits to the beginning of the wordcount text.
>> set {wordcount, kount} to {lFruit & "1" & wordcount, 1}
>> end if
>> ---from here on back to myFruit
>> tell application "TextWrangler"
>> tell text of front text window
>> make new line at end with data (myFruit & space & kount)
>> select insertion point -1
>> end tell
>> end tell
>>
>> return 0.5
>> end idle
>>
>>
>> NG
>
>Nigel,
>I have a question, why is it that you are prepending the text, that is
>adding it to the front ?
Hi Deivy.
It's just something which arose from the way the script developed. :)
I was originally thinking in terms of just your barred labels and the
corresponding values. So each time a new fruit came up, the script would
append the barred name and "1" to the end of 'wordcount' with no bar at
the end. But this caused the script to error when the fruit at the end
came up again because there was then no word 2 in 'ti2'. The fix which
occurred to me was to have a permanent bar at the end of 'wordcount' to
act as word 2 when needed and to prepend "|<new fruit>|1" pairs instead
of postpending them.
But now you mention it, once one thinks of 'wordcount' as having bars at
both ends as well as between the labels and values, the same structure
can be obtained by postpending unbarred labels and barred "1"s instead
of prepending barred labels and unbarred "1"s. So …
> -- Append new fruits to the beginning of the wordcount text.
> set {wordcount, kount} to {lFruit & "1" & wordcount, 1}
… becomes …
-- Append new fruits to the end of the wordcount text.
set {wordcount, kount} to {wordcount & (myFruit & "|1|"), 1}
>I ran some tests and for large text appending was a bit (very slightly )
>faster.
I can't think why that should be so here. The amount of concatenation
should be exactly the same with the parenthesis I've used above. If the
parentheses are omitted and 'wordcount' already quite long, I'd expect
postpending to take slightly _longer_ than prepending. Say 'wordcount'
contains 100 characters and 'myFruit' is 'apple' (5 characters), then in
theory, at least:
wordcount & (myFruit & "|1|")
--> 3 characters concatenated to 5, the resulting 8 characters then concatenated to 100. Total new text involved = 8 + 108 characters.
wordcount & myFruit & "|1|"
--> 5 characters concatenated to 100, three characters then concatenated to the resulting 105. Total new text involved = 105 + 108 characters.
But I must admit I've not actually got round to timing the difference.
:)
NG
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