Words from neighbour ones: Difference between revisions

Added AppleScript.
(Added AppleScript.)
Line 25:
{{Template:Strings}}
<br><br>
 
=={{header|AppleScript}}==
===Core language===
<lang applescript>on task()
-- Since the task specifically involves unixdict.txt, this code's written in
-- the knowlege that the words are on individual lines and in dictionary order.
set dictPath to (path to desktop as text) & "unixdict.txt"
script o
property wordList : paragraphs of (read file dictPath as «class utf8»)
property matches : {}
end script
-- Zap words with fewer than 9 characters and work with what's left.
repeat with i from 1 to (count o's wordList)
if ((count item i of o's wordList) < 9) then set item i of o's wordList to missing value
end repeat
set o's wordList to o's wordList's every text
set wordListCount to (count o's wordList)
set previousNewWord to missing value
repeat with i from 1 to (wordListCount - 8)
set newWord to character 1 of item i of o's wordList
set j to (i - 1)
repeat with k from 2 to 9
set newWord to newWord & character k of item (j + k) of o's wordList
end repeat
-- Since wordList's known to be in dictionary order, a lot of time can be saved
-- by only checking the necessary few words ahead for a match instead of
-- using AppleScript's 'is in' or 'contains' commands, which check the entire list.
if (newWord is not previousNewWord) then
repeat with j from i to wordListCount
set thisWord to item j of o's wordList
if (newWord comes after thisWord) then
else
if (newWord is thisWord) then set end of o's matches to newWord
exit repeat
end if
end repeat
set previousNewWord to newWord
end if
end repeat
return o's matches
end task
 
task()</lang>
 
{{output}}
<lang applescript>{"applicate", "architect", "astronomy", "christine", "christoph", "committee", "composite", "constrict", "construct", "different", "extensive", "greenwood", "implement", "improvise", "intercept", "interpret", "interrupt", "philosoph", "prescript", "receptive", "telephone", "transcend", "transport", "transpose"}</lang>
 
===AppleScriptObjC===
Same output as above.
<lang applescript>use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions
 
on task()
set |⌘| to current application
set dictPath to (POSIX path of (path to desktop)) & "unixdict.txt"
set dictText to |⌘|'s class "NSString"'s stringWithContentsOfFile:(dictPath) ¬
usedEncoding:(missing value) |error|:(missing value)
set newlineSet to |⌘|'s class "NSCharacterSet"'s newlineCharacterSet()
set wordArray to dictText's componentsSeparatedByCharactersInSet:(newlineSet)
-- Lose words with fewer than 9 characters.
set filter to |⌘|'s class "NSPredicate"'s predicateWithFormat:("self MATCHES '.{9,}+'")
set relevantWords to wordArray's filteredArrayUsingPredicate:(filter)
-- Creating the new words is most easily and efficiently done with core AppleScript.
script o
property wordList : relevantWords as list
property newWords : {}
end script
repeat with i from 1 to ((count o's wordList) - 8)
set newWord to character 1 of item i of o's wordList
set j to (i - 1)
repeat with k from 2 to 9
set newWord to newWord & character k of item (j + k) of o's wordList
end repeat
set end of o's newWords to newWord
end repeat
-- But Foundation sets are good for filtering the results.
set matches to |⌘|'s class "NSMutableOrderedSet"'s orderedSetWithArray:(o's newWords)
tell matches to intersectSet:(|⌘|'s class "NSSet"'s setWithArray:(relevantWords))
return (matches's array()) as list
end task
 
task()</lang>
 
=={{header|AWK}}==
557

edits