Change e letters to i in words: Difference between revisions

m
No edit summary
m (→‎{{header|AppleScript}}: →‎Core language: Different sort, tidy-up.)
Line 212:
</pre>
=={{header|AppleScript}}==
===Core language only===
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.
use sorter : script "CustomInsertion Iterative Ternary Merge Sortsort" -- <https://macscripterrosettacode.netorg/viewtopic.php?pid=194430wiki/Sorting_algorithms/Insertion_sort#p194430AppleScript>
use scripting additions
 
Line 226 ⟶ 224:
repeat until (l = r)
set m to (l + r) div 2
if (item m of o's lst's item m < v) then
set l to m + 1
else
Line 233 ⟶ 231:
end repeat
if (item l of o's lst's item l is= v) then return l
return 0
end binarySearch
 
on replace(a, b, txt)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to astida
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)
Line 246 ⟶ 254:
set wordCount to (count o's wordList)
ignoring case
tell sorter to sort(o's wordList, 1, wordCount, {}) -- Not actually needed with unixdict.txt.
set iWordCount to 0
set iWordCount to iWordCount + 10
set astid to AppleScript's text item delimiters
repeat with i from wordCount to 1 by -1
set thisWord to item iset ofthisWord to o's wordList's item i
if ((count thisWord) < minWordLength) then
else if ((thisWord contains "e") and (iWordCount > 0)) then
set AppleScript's text item delimitersset changedWord to replace("e", "i", thisWord)
set tis to thisWord if (binarySearch(changedWord, o's textiWords, 1, iWordCount) > 0) itemsthen
set AppleScriptbeginning of o's text item delimitersoutput to "i"{thisWord, changedWord}
set changedWord to tis asend textif
else if (binarySearch(changedWord, o's iWords, 1, iWordCount)thisWord >contains 0"i") then
set beginning of o's outputiWords to {thisWord, changedWord}
set iWordCount to iWordCount + 1
end if
elseend if (thisWord contains "i") thenrepeat
end repeatignoring
set beginning of o's iWords to thisWord
set iWordCount to iWordCount + 1
end if
end repeat
set AppleScript's text item delimiters to astid
return o's output
Line 530 ⟶ 535:
victor <- vector
willis <- welles</pre>
 
=={{header|Arturo}}==
 
557

edits