Anadromes: Difference between revisions

153 bytes removed ,  1 year ago
m
→‎{{header|AppleScript}}: Minor optimisation.
(added AWK)
m (→‎{{header|AppleScript}}: Minor optimisation.)
Line 153:
set theText to |⌘|'s class "NSString"'s stringWithContentsOfFile:(textFile's POSIX path) ¬
usedEncoding:(missing value) |error|:(missing value)
-- Lose paragraphs (one word per paragraph) withwhich have fewer than minLength characters.
set theText to theText's stringByReplacingOccurrencesOfString:("(?m)^.{0," & minLength - 1 & "}\\R") withString:("") ¬
options:(|⌘|'s NSRegularExpressionSearch) range:({0, theText's |length|()})
-- Get the remaining paragraphs as an array.
set wordArray to (theText's componentsSeparatedByCharactersInSet:(|⌘|'s class "NSCharacterSet"'s controlCharacterSet()))
-- Derive a list of reversed words.
Line 169:
end repeat
-- IdentifyGet anda sortlist of words that are in both the original and reversed groups.
set wordSet to |⌘|'s class "NSMutableSet"'s setWithArray:(wordArray)
set reversedWordSet to |⌘|'s class "NSSet"'s setWithArray:(o's wordList)
set filter to |⌘|'s class "NSPredicate"'s predicateWithFormat_("self IN %@", reversedWordSet)
tellset wordSeto's wordList to filterUsingPredicate(wordArray's filteredArrayUsingPredicate:(filter)) as list
set o's wordList to (wordSet's allObjects()'s sortedArrayUsingSelector:("localizedStandardCompare:")) as list
-- ConstructBuild the output line by line, omitting palindromes and already matched word pairs.
set output to {}
repeat with i from 1 to (count o's wordList)
Line 182 ⟶ 180:
set o's wordList's item i to missing value
set reversedWord to (thisWord's characters's reverse) as text
if ({reversedWord} is in o's wordList) then set output's end to thisWord & " <--> " & reversedWord
end repeat
set AppleScript's text item delimiters to linefeed
Line 211 ⟶ 209:
revotes <--> setover
sallets <--> stellas"</lang>
 
=={{header|AWK}}==
<lang AWK>
557

edits