Re: Algorithms in Applescript
Re: Algorithms in Applescript
- Subject: Re: Algorithms in Applescript
- From: email@hidden
- Date: Wed, 25 Mar 2015 03:37:40 +0100
Hello.
Nothing helps me more than posting someone, to device tests. This version actually works. I have reworked it, to be in line with the c-char arrays. (first version worked with the tests on the page, but broke when I tried to find the difference between saturday and saturdax.
I shall not post unsolicited code for the future, that doesn’t work.
----
set dist to findLevenshteinDistance for "sunday" against "saturday"
to findLevenshteinDistance for s1 against s2
global v0, v1
script o
property l : s1
property m : s2
end script
if s1 = s2 then return 0
set ll to length of s1
set lm to length of s2
if ll = 0 then return lm
if lm = 0 then return ll
set v0 to {}
repeat with i from 1 to (lm + 1)
set end of v0 to (i - 1)
end repeat
set item -1 of v0 to 0
copy v0 to v1
repeat with i from 1 to ll
-- calculate v1 (current row distances) from the previous row v0
-- first element of v1 is A[i+1][0]
-- edit distance is delete (i+1) chars from s to match empty t
set item 1 of v1 to i
-- use formula to fill in the rest of the row
repeat with j from 1 to lm
if item i of o's l = item j of o's m then
set cost to 0
else
set cost to 1
end if
set item (j + 1) of v1 to min3 for ((item j of v1) + 1) against ((item (j + 1) of v0) + 1) by ((item j of v0) + cost)
end repeat
copy v1 to v0
end repeat
return item (lm + 1) of v1
end findLevenshteinDistance
to min3 for anInt against anOther by theThird
if anInt < anOther then
if theThird < anInt then
return theThird
else
return anInt
end if
else
if theThird < anOther then
return theThird
else
return anOther
end if
end if
end min3
-------
25. mars 2015 kl. 02:55 skrev email@hidden:
> Hello.
>
> *Much* earlier today, I gave the Levenshtein algorithm a spin. It seems to work alright, even though, I am a tad unsure if I didn’t get it right. what is at fault if there is any, is the last element of weights. (Due to the zero/1 difference with indexing of strings.)
> -------------------------------------
> set dist to findLevenshteinDistance for "sunday" against "saturday"
> to findLevenshteinDistance for s1 against s2
> script o
> property l : s1
> property m : s2
> end script
> if s1 = s2 then return 0
> set ll to length of s1
> set lm to length of s2
> if ll = 0 then return lm
> if lm = 0 then return ll
>
> set v0 to {}
>
> repeat with i from 1 to lm
> set end of v0 to (i - 1)
> end repeat
> copy v0 to v1
>
> repeat with i from 1 to ll
> -- calculate v1 (current row distances) from the previous row v0
>
> -- first element of v1 is A[i+1][0]
> -- edit distance is delete (i+1) chars from s to match empty t
> set item 1 of v1 to i
> -- use formula to fill in the rest of the row
> repeat with j from 2 to lm
> if item i of o's l = item j of o's m then
> set cost to 0
> else
> set cost to 1
> end if
> set item j of v1 to min3 for ((item (j - 1) of v1) + 1) against ((item j of v0) + 1) by ((item (j - 1) of v0) + cost)
> end repeat
> copy v1 to v0
> end repeat
> return item lm of v1
> end findLevenshteinDistance
>
> to min3 for anInt against anOther by theThird
> if anInt < anOther then
> if theThird < anInt then
> return theThird
> else
> return anInt
> end if
> else
> if theThird < anOther then
> return theThird
> else
> return anOther
> end if
> end if
> end min3
> -----------------------------------
>
> 25. mars 2015 kl. 02:25 skrev Stockly, Ed <email@hidden>:
>
>> I gave it some thought, and the idea of the Rosetta site is to show how
>> different languages approach the same tasks.
>>
>> While there are some tasks where the approaches would be identical, there
>> are others where the AppleScriptObjC approach would be radically
>> different. And it's probably better to have the non-AppleScriptObjC
>> appleScript approach distinguished from the appleScript approach.
>>
>>
>>
>> On 3/24/15, 6:23 PM, "Shane Stanley" <email@hidden> wrote:
>>
>>> On 25 Mar 2015, at 12:08 pm, Stockly, Ed <email@hidden> wrote:
>>>>
>>>> I noticed there wasn't a category for AppleScriptObjC
>>>
>>> I'm not sure that there should be. It's not really a different language
>>> -- it's still linguistic "pure" AppleScript. I suppose it could be
>>> described as a different implementation of AppleScript, but it's really
>>> just a feature of a newer version of AppleScript.
>>>
>>
>>
>> _______________________________________________
>> 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
>
_______________________________________________
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