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 only===
===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 "Custom Iterative Ternary Merge Sort" -- <https://macscripter.net/viewtopic.php?pid=194430#p194430>
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 (item m of o's lst < v) then
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 (item l of o's lst is v) then return l
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 astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to a
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, {}) -- Not actually needed with unixdict.txt.
tell sorter to sort(o's wordList, 1, wordCount) -- Not actually needed with unixdict.txt.
set iWordCount to 0
set iWordCount to 0
set astid to AppleScript's text item delimiters
repeat with i from wordCount to 1 by -1
repeat with i from wordCount to 1 by -1
set thisWord to item i of o's wordList
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 AppleScript's text item delimiters to "e"
set changedWord to replace("e", "i", thisWord)
set tis to thisWord's text items
if (binarySearch(changedWord, o's iWords, 1, iWordCount) > 0) then
set AppleScript's text item delimiters to "i"
set beginning of o's output to {thisWord, changedWord}
set changedWord to tis as text
end if
if (binarySearch(changedWord, o's iWords, 1, iWordCount) > 0) then
else if (thisWord contains "i") then
set beginning of o's output to {thisWord, changedWord}
set beginning of o's iWords to thisWord
set iWordCount to iWordCount + 1
end if
end if
else if (thisWord contains "i") then
end repeat
end ignoring
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
return o's output
Line 530: Line 535:
victor <- vector
victor <- vector
willis <- welles</pre>
willis <- welles</pre>

=={{header|Arturo}}==
=={{header|Arturo}}==