• 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: Deivy Petrescu <email@hidden>
  • Date: Tue, 23 Sep 2014 17:55:53 -0400

On Sep 16, 2014, at 07:49 , Nigel Garvey <email@hidden> wrote:

> 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

Nigel,
I’ve been having some email problems…. Sorry for this delay.

Firstly, you are right about  'set end of lista to myFruit’.

Secondly I like your idea of a record that it is not a record. That is really good.
I was thinking inside the box.
I believe that we can do without the text item delimiters, but  I loved the way you thought!

I have a question, why is it that you are  prepending the text, that is adding it to the front ?
I ran some tests and for large text appending was a bit (very slightly ) faster.

Deivy Petrescu
email@hidden


 _______________________________________________
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


References: 
 >Re: Count word's occurences (From: Nigel Garvey <email@hidden>)

  • Prev by Date: Re: Search Contacts for phone number
  • 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