Anagrams: Difference between revisions

Content added Content deleted
m (added related tasks.)
m (→‎{{header|AppleScript}}: Handler modified to take an array of words as a parameter instead of specifically reading "unixdict.txt".)
Line 523: Line 523:
use scripting additions
use scripting additions


on largestAnagramGroups(listOfWords)
on anagramsTask()
script o
script o
property wordList : missing value
property wordList : listOfWords
property doctoredWords : {}
property doctoredWords : {}
property longestRanges : {}
property longestRanges : {}
Line 531: Line 531:
end script
end script
-- The words in "unixdict.txt" are in alphabetical order, one per line.
-- Some contain punctuation characters, so they're best extracted as 'paragraphs' rather than as 'words'.
set wordFile to ((path to desktop as text) & "www.rosettacode.org:unixdict.txt") as «class furl»
set o's wordList to paragraphs of (read wordFile as «class utf8»)
ignoring case
ignoring case
-- Build another list containing doctored versions of the same words with their characters lexically sorted.
-- Build another list containing doctored versions of the input words with their characters lexically sorted.
set astid to AppleScript's text item delimiters
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
set AppleScript's text item delimiters to ""
Line 542: Line 538:
set theseChars to thisWord's characters
set theseChars to thisWord's characters
-- A straight ascending in-place sort here.
-- A straight ascending in-place sort here.
tell sorter to sort(theseChars, 1, -1, {}) -- Params: (list, start index, end index, customisation spec.).
tell sorter to sort(theseChars, 1, -1, {}) -- Params: (list, start index, end index, customisation spec.).
set end of o's doctoredWords to theseChars as text
set end of o's doctoredWords to theseChars as text
end repeat
end repeat
Line 576: Line 572:
-- Get the group(s) of words occupying the same range(s) in the original-word list.
-- Get the group(s) of words occupying the same range(s) in the original-word list.
-- The stable parallel sort above will have kept each group's words in alphabetical order.
-- The stable parallel sort above will have kept each group's words in the same order with respect to each other.
repeat with thisRange in o's longestRanges
repeat with thisRange in o's longestRanges
set {i, j} to thisRange
set {i, j} to thisRange
Line 592: Line 588:
return o's output
return o's output
end largestAnagramGroups
end anagramsTask


-- The closing values of AppleScript 'run handler' variables not explicity declared local are
anagramsTask()</lang>
-- saved back to the script file afterwards — and "unixdict.txt" contains 25,104 words!
local wordFile, wordList
-- The words in "unixdict.txt" are arranged one per line in alphabetical order.
-- Some contain punctuation characters, so they're best extracted as 'paragraphs' rather than as 'words'.
set wordFile to ((path to desktop as text) & "www.rosettacode.org:unixdict.txt") as «class furl»
set wordList to paragraphs of (read wordFile as «class utf8»)
return largestAnagramGroups(wordList)</lang>


{{output}}
{{output}}