• 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: A Lookup Function?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A Lookup Function?


  • Subject: Re: A Lookup Function?
  • From: Christopher Stone <email@hidden>
  • Date: Mon, 24 Jan 2011 14:54:00 -0600

On Jan 24, 2011, at 11:30, Marconi wrote:
For example, I'd enter "email@hidden" and it would parse each character and pop up a window with the text:

Mike Alpha Romeo Charlie Oscar November India at Mike Delta Charlie Charlie Lima X-ray X-ray Victor India dot Charlie Oscar Mike.
...

What's the most efficient way to store and initialize the table, and look up the values?
______________________________________________________________________

Hey There,

A very simple method would be something like this:

property alphaList : "ABCDEFGHIJKLMNOPQRSTUVWXYZ.-@0123456789"
property phoneticList : {"Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliet", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whiskey", "X-Ray", "Yankee", "Zulu", "DOT", "DASH", "AMPERSAND", "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}
property excludeItems : "" # exclusions if any

on phoneticLookup(someStringToLookup)
set transList to {}
set splitIt to characters of someStringToLookup
repeat with i from 1 to length of splitIt
set lookUpChar to item i of splitIt
if lookUpChar is not in excludeItems then
set offst to offset of lookUpChar in alphaList
set end of transList to item offst of phoneticList
else
set end of transList to lookUpChar
end if
end repeat
return transList
end phoneticLookup

set someStringToLookup to text returned of (display dialog "ENTER STRING TO CONVERT TO PHONETICS:" default answer "")
set phoneticLookupList to phoneticLookup(someStringToLookup)
choose from list phoneticLookupList with title ¬
"PHONETIC CONVERSION LIST" with prompt ¬
"PHONETIC CONVERSION LIST" multiple selections allowed true ¬
with empty selection allowed

--

For more complex look-ups I like to use the Satimage.osax's regex-powered 'find text': http://tinyurl.com/dc3soh

Even though overkill for this job it's very fast (see time at end).

property lookUpTable : "
A Alpha
B Bravo
C Charlie
D Delta
E Echo
F Foxtrot
G Golf
H Hotel
I India
J Juliet
K Kilo
L Lima
M Mike
N November
O Oscar
P Papa
Q Quebec
R Romeo
S Sierra
T Tango
U Uniform
V Victor
W Whiskey
X X-Ray
Y Yankee
Z Zulu
. DOT
- DASH
@ AMPERSAND
0 Zero
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
7 Seven
8 Eight
9 Nine"
######################################################################
on phoneticLookup(someStringToLookup)
set transList to {}
set splitIt to characters of someStringToLookup
repeat with i from 1 to length of splitIt
set lookUpChar to item i of splitIt
set lookUpCharRegex to "^" & lookUpChar & "\\t+(.+)"
set end of transList to regexFindCapture(lookUpCharRegex, "\\1", lookUpTable, false, true, false, false, true) of me
end repeat
return transList
end phoneticLookup
######################################################################
on regexFindCapture(fndStr, returnStr, dataSource, caseSensitive, regexFlag, wholeWord, allOccurrences, stringResult)
try
set findResult to find text fndStr ¬
in dataSource ¬
case sensitive caseSensitive ¬
regexp regexFlag ¬
whole word wholeWord ¬
using returnStr ¬
all occurrences allOccurrences ¬
string result stringResult
return findResult
on error # errMsg number errNum
return false
end try
end regexFindCapture
######################################################################

set someStringToLookup to text returned of (display dialog "ENTER STRING TO CONVERT TO PHONETICS:" default answer "")
set phoneticLookupList to phoneticLookup(someStringToLookup)
choose from list phoneticLookupList with title ¬
"PHONETIC CONVERSION LIST" with prompt ¬
"PHONETIC CONVERSION LIST" multiple selections allowed true ¬
with empty selection allowed

# NOTE: I've not done any real error-checking here.

1st Script's List-Lookup time:   0.014
2nd Script's Satimage.osax time: 0.020

--
Chris
 _______________________________________________
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: 
 >A Lookup Function? (From: Marconi <email@hidden>)

  • Prev by Date: Re: A Lookup Function?
  • Next by Date: Re: A Lookup Function?
  • Previous by thread: Re: A Lookup Function?
  • Next by thread: Re: A Lookup Function?
  • Index(es):
    • Date
    • Thread