Change e letters to i in words: Difference between revisions
Content added Content deleted
No edit summary |
m (→{{header|AppleScript}}: →Core language: Different sort, tidy-up.) |
||
Line 212: | Line 212: | ||
</pre> |
</pre> |
||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
===Core language |
===Core language=== |
||
Because of the huge size of the original word list and the number of changed words to check, it's nearly 100 times as fast to ensure the list is sorted and to use a binary search handler as it is to use the language's built-in '''is in''' command! (1.17 seconds instead of 110 on my current machine.) A further, lesser but interesting optimisation is to work through the sorted list in reverse, storing possible "i" word candidates encountered before getting to any "e" words from which they can be derived. Changed "e" words then only need to be checked against this smaller collection. |
|||
<syntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- Mac OS X 10.9 (Mavericks) or later. |
<syntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- Mac OS X 10.9 (Mavericks) or later. |
||
use sorter : script " |
use sorter : script "Insertion sort" -- <https://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#AppleScript> |
||
use scripting additions |
use scripting additions |
||
Line 226: | Line 224: | ||
repeat until (l = r) |
repeat until (l = r) |
||
set m to (l + r) div 2 |
set m to (l + r) div 2 |
||
if ( |
if (o's lst's item m < v) then |
||
set l to m + 1 |
set l to m + 1 |
||
else |
else |
||
Line 233: | Line 231: | ||
end repeat |
end repeat |
||
if ( |
if (o's lst's item l = v) then return l |
||
return 0 |
return 0 |
||
end binarySearch |
end binarySearch |
||
on replace(a, b, txt) |
|||
⚫ | |||
⚫ | |||
set txt to txt's text items |
|||
set AppleScript's text item delimiters to b |
|||
set txt to txt as text |
|||
set AppleScript's text item delimiters to astid |
|||
return txt |
|||
end replace |
|||
on task(minWordLength) |
on task(minWordLength) |
||
Line 246: | Line 254: | ||
set wordCount to (count o's wordList) |
set wordCount to (count o's wordList) |
||
ignoring case |
|||
tell sorter to sort(o's wordList, 1, wordCount |
tell sorter to sort(o's wordList, 1, wordCount) -- Not actually needed with unixdict.txt. |
||
⚫ | |||
⚫ | |||
repeat with i from wordCount to 1 by -1 |
repeat with i from wordCount to 1 by -1 |
||
set thisWord to o's wordList's item i |
|||
if ((count thisWord) < minWordLength) then |
if ((count thisWord) < minWordLength) then |
||
else if ((thisWord contains "e") and (iWordCount > 0)) then |
else if ((thisWord contains "e") and (iWordCount > 0)) then |
||
set changedWord to replace("e", "i", thisWord) |
|||
if (binarySearch(changedWord, o's iWords, 1, iWordCount) > 0) then |
|||
set |
set beginning of o's output to {thisWord, changedWord} |
||
end if |
|||
if ( |
else if (thisWord contains "i") then |
||
set beginning of o's |
set beginning of o's iWords to thisWord |
||
set iWordCount to iWordCount + 1 |
|||
end if |
end if |
||
end repeat |
|||
⚫ | |||
set beginning of o's iWords to thisWord |
|||
⚫ | |||
end if |
|||
⚫ | |||
⚫ | |||
return o's output |
return o's output |
||
Line 530: | Line 535: | ||
victor <- vector |
victor <- vector |
||
willis <- welles</pre> |
willis <- welles</pre> |
||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||