Change e letters to i in words: Difference between revisions
Content added Content deleted
(→AppleScript :: Functionally: Added a functionally composed variant, defining the result as an intersection of sets.) |
|||
Line 305: | Line 305: | ||
task(6)</lang> |
task(6)</lang> |
||
===Functional=== |
|||
Assuming a local copy of unixdict.txt (on the macOS Desktop), using the Foundation libraries, |
|||
and defining the list of twins as an intersection of sets. |
|||
<lang applescript>use framework "Foundation" |
|||
----- DICTIONARY WORDS TWINNED BY (E -> I) REPLACEMENT --- |
|||
-- ieTwins :: String -> String |
|||
on ieTwins(s) |
|||
-- Listing of dictionary words twinned by (e -> i) replacement |
|||
set ca to current application |
|||
set shortWords to filteredLines("5 < length", s) |
|||
set eWords to shortWords's ¬ |
|||
filteredArrayUsingPredicate:(containsChar("e")) |
|||
set possibles to ((eWords's ¬ |
|||
componentsJoinedByString:(linefeed))'s ¬ |
|||
stringByReplacingOccurrencesOfString:("e") ¬ |
|||
withString:("i"))'s ¬ |
|||
componentsSeparatedByString:(linefeed) |
|||
set possibleSet to ca's NSMutableSet's setWithArray:(possibles) |
|||
set lexicon to ca's NSSet's ¬ |
|||
setWithArray:(shortWords's filteredArrayUsingPredicate:(containsChar("i"))) |
|||
possibleSet's intersectSet:(lexicon) |
|||
-- Dictionary of possible words and their sources |
|||
set dict to ca's NSDictionary's dictionaryWithObjects:eWords forKeys:possibles |
|||
-- Listing of candidate words which are found in the dictionary |
|||
-- (twinned with their sources) |
|||
script pair |
|||
on |λ|(k) |
|||
((dict's objectForKey:(k)) as string) & " -> " & k |
|||
end |λ| |
|||
end script |
|||
unlines(map(pair, ((possibleSet's allObjects())'s ¬ |
|||
sortedArrayUsingSelector:"compare:") as list)) |
|||
end ieTwins |
|||
--------------------------- TEST ------------------------- |
|||
on run |
|||
ieTwins(readFile("~/Desktop/unixdict.txt")) |
|||
end run |
|||
------------------------- GENERIC ------------------------ |
|||
-- containsChar :: Char -> NSPredicate |
|||
on containsChar(c) |
|||
tell current application |
|||
its (NSPredicate's predicateWithFormat:("self contains '" & c & "'")) |
|||
end tell |
|||
end containsChar |
|||
-- filteredLines :: String -> NString -> [a] |
|||
on filteredLines(predicateString, s) |
|||
-- A list of lines filtered by an NSPredicate string |
|||
tell current application |
|||
set predicate to its (NSPredicate's predicateWithFormat:predicateString) |
|||
set array to its (NSArray's ¬ |
|||
arrayWithArray:(s's componentsSeparatedByString:("\n"))) |
|||
end tell |
|||
(array's filteredArrayUsingPredicate:(predicate)) |
|||
end filteredLines |
|||
-- map :: (a -> b) -> [a] -> [b] |
|||
on map(f, xs) |
|||
-- The list obtained by applying f |
|||
-- to each element of xs. |
|||
tell mReturn(f) |
|||
set lng to length of xs |
|||
set lst to {} |
|||
repeat with i from 1 to lng |
|||
set end of lst to |λ|(item i of xs, i, xs) |
|||
end repeat |
|||
return lst |
|||
end tell |
|||
end map |
|||
-- mReturn :: First-class m => (a -> b) -> m (a -> b) |
|||
on mReturn(f) |
|||
-- 2nd class handler function lifted into 1st class script wrapper. |
|||
if script is class of f then |
|||
f |
|||
else |
|||
script |
|||
property |λ| : f |
|||
end script |
|||
end if |
|||
end mReturn |
|||
-- readFile :: FilePath -> IO NSString |
|||
on readFile(strPath) |
|||
set ca to current application |
|||
set e to reference |
|||
set {s, e} to (ca's NSString's ¬ |
|||
stringWithContentsOfFile:((ca's NSString's ¬ |
|||
stringWithString:strPath)'s ¬ |
|||
stringByStandardizingPath) ¬ |
|||
encoding:(ca's NSUTF8StringEncoding) |error|:(e)) |
|||
if missing value is e then |
|||
s |
|||
else |
|||
(localizedDescription of e) as string |
|||
end if |
|||
end readFile |
|||
-- unlines :: [String] -> String |
|||
on unlines(xs) |
|||
-- A single string formed by the intercalation |
|||
-- of a list of strings with the newline character. |
|||
set {dlm, my text item delimiters} to ¬ |
|||
{my text item delimiters, linefeed} |
|||
set s to xs as text |
|||
set my text item delimiters to dlm |
|||
s |
|||
end unlines</lang> |
|||
{{Out}} |
|||
<pre>analyses -> analysis |
|||
atlantes -> atlantis |
|||
bellow -> billow |
|||
breton -> briton |
|||
clench -> clinch |
|||
convect -> convict |
|||
crises -> crisis |
|||
diagnoses -> diagnosis |
|||
frances -> francis |
|||
galatea -> galatia |
|||
harden -> hardin |
|||
heckman -> hickman |
|||
enfant -> infant |
|||
inflect -> inflict |
|||
inequity -> iniquity |
|||
enquiry -> inquiry |
|||
jacobean -> jacobian |
|||
marten -> martin |
|||
module -> moduli |
|||
pegging -> pigging |
|||
psychoses -> psychosis |
|||
rabbet -> rabbit |
|||
sterling -> stirling |
|||
synopses -> synopsis |
|||
vector -> victor |
|||
welles -> willis</pre> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |