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:
use scripting additions
 
on largestAnagramGroups(listOfWords)
on anagramsTask()
script o
property wordList : missing valuelistOfWords
property doctoredWords : {}
property longestRanges : {}
Line 531:
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
-- Build another list containing doctored versions of the sameinput words with their characters lexically sorted.
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
Line 542 ⟶ 538:
set theseChars to thisWord's characters
-- A straight ascending in-place sort here.
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
end repeat
Line 576 ⟶ 572:
-- 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 alphabeticalthe same order with respect to each other.
repeat with thisRange in o's longestRanges
set {i, j} to thisRange
Line 592 ⟶ 588:
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 in alphabetical order,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 o's wordList to paragraphs of (read wordFile as «class utf8»)
return largestAnagramGroups(wordList)</lang>
 
{{output}}