Re: Count word's occurences
Re: Count word's occurences
- Subject: Re: Count word's occurences
- From: Nigel Garvey <email@hidden>
- Date: Tue, 16 Sep 2014 12:49:32 +0100
Deivy Petrescu wrote on Mon, 15 Sep 2014 15:55:16 -0400:
>set theDocName to "theDocName"
>set lista to {}
>set wordcount to {}
>tell application "TextWrangler"
> activate
> set myDocument to make new text document with properties
{name:theDocName}
> set show line numbers of myDocument to false
>end tell
>
>on idle
> global lista
> global wordcount
> tell application "JSON Helper" to set myFruit to fetch JSON from
mySource
> set lFruit to "|"& myFruit&"|" ---> added this line
> if myFruit is in lista then
> ---from here change myFruit to lFruit
> try
> wordcount as string
> on error e
> set e to text (get offset of "{" in e) thru (get offset of "}" in e)
of e
> end try
> set {wordcount, kount} to run script "set nue to " & e & "\n\t\tset
>nue's " & lFruit & " to nue's " & lFruit & " + 1\n\t\treturn {nue,nue's
>" & lFruit & " }"
> else
> set end of lista to lFruit
> set {wordcount, kount} to {wordcount & (run script "{" & lFruit &
":1}"), 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
Hi Deivy.
The line after 'else' should be 'set end of lista to myFruit'.
I think your idea of the script keeping its own record of how often each
fruit has appeared is a good one, but your implementation spends a fair
bit of time creating and switching between records and text by means of
recovery from deliberate errors and the use of 'run script' with text.
This can be avoided by keeping a text record anyway. The format I've
used for it below is:
"|<fruitname>|<number>|<fruitname>|<number>|etc.…|"
The use of bars as separators originated from their use in your record
labels, but they also happen to be 'words' in AppleScript, which is
convenient in this version of the script:
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
_______________________________________________
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