Jump to content

Textonyms: Difference between revisions

Added AppleScript and AppleScriptObjC solutions.
imported>Meerkut
m (→‎AWK: more reliable substring inclusion check)
(Added AppleScript and AppleScriptObjC solutions.)
Line 271:
3532876362374256472749 encodes electroencephalography
</pre>
 
=={{header|AppleScript}}==
===Vanilla===
<syntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- OS X 10.9 (Mavericks) or later.
-- https://rosettacode.org/wiki/Sorting_algorithms/Quicksort#Straightforward
use sorter : script "Quicksort"
use scripting additions
 
on textonyms(posixPath, query)
set digits to "23456789"
set keys to {"", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}
set {mv, LF} to {missing value, linefeed}
-- Check input.
try
set reporting to (query's class is not text)
if (not reporting) then
repeat with chr in query
if (chr is not in digits) then error "Invalid digit input"
end repeat
set digitCount to (count query)
end if
script o
property |words| : (do shell script ("cat " & posixPath))'s paragraphs
property combos : mv
end script
on error errMsg
display alert "Textonyms handler: parameter error" message ¬
errMsg as critical buttons {"Stop"} default button 1
error number -128
end try
ignoring case
-- Lose obvious no-hope words.
set alphabet to join(keys's rest, "")
repeat with i from 1 to (count o's |words|)
set wrd to o's |words|'s item i
if ((reporting) or (wrd's length = digitCount)) then
repeat with chr in wrd
if (chr is not in alphabet) then
set o's |words|'s item i to mv
exit repeat
end if
end repeat
else
set o's |words|'s item i to mv
end if
end repeat
set o's |words| to o's |words|'s every text
set wordCount to (count o's |words|)
-- Derive digit combinations from the rest.
set txt to join(o's |words|, LF)
repeat with d in digits
set d to d's contents
repeat with letter in keys's item d
set txt to replaceText(txt, letter's contents, d)
end repeat
end repeat
set o's combos to txt's paragraphs
end ignoring
-- Return the appropriate result
considering case -- Case insensitivity not needed with digits.
if (reporting) then
tell sorter to sort(o's combos, 1, wordCount)
set {previousCombo, comboCount, textonymCount, counting} to ¬
{"", wordCount, 0, true}
repeat with i from 1 to wordCount
set thisCombo to o's combos's item i
if (thisCombo = previousCombo) then
set comboCount to comboCount - 1
if (counting) then
set textonymCount to textonymCount + 1
set counting to false
end if
else
set previousCombo to thisCombo
set counting to true
end if
end repeat
set output to (wordCount as text) & " words in '" & ¬
(do shell script ("basename " & posixPath)) & ¬
"' can be represented by the digit key mapping." & ¬
(LF & comboCount & " digit combinations are required to represent them.") & ¬
(LF & textonymCount & " of the digit combinations represent Textonyms.")
else
set output to {}
repeat with i from 1 to wordCount
if (o's combos's item i = query) then set output's end to o's |words|'s item i
end repeat
if ((count output) = 1) then set output to {}
end if
end considering
return output
end textonyms
 
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
on replaceText(mainText, searchText, replacementText)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to searchText
set textItems to mainText's text items
set AppleScript's text item delimiters to replacementText
set mainText to textItems as text
set AppleScript's text item delimiters to astid
return mainText
end replaceText
 
on task()
set posixPath to "~/Desktop/www.rosettacode.org/unixdict.txt"
set report to textonyms(posixPath, missing value)
set output to {report, "", "Examples:"}
repeat with digitCombo in {"729", "723353", "25287876746242"}
set foundWords to textonyms(posixPath, digitCombo's contents)
set output's end to digitCombo & " --> {" & join(foundWords, ", ") & "}"
end repeat
return join(output, linefeed)
end task
 
task()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"24978 words in 'unixdict.txt' can be represented by the digit key mapping.
22903 digit combinations are required to represent them.
1473 of the digit combinations represent Textonyms.
 
Examples:
729 --> {paw, pax, pay, paz, raw, ray, saw, sax, say}
723353 --> {paddle, raffle, saddle}
25287876746242 --> {claustrophobia, claustrophobic}"</syntaxhighlight>
 
===AppleScriptObjC===
<syntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions
 
on textonyms(posixPath, query)
set digits to "23456789"
set keys to {"", "[abc]", "[def]", "[ghi]", "[jkl]", "[mno]", "[pqrs]", "[tuv]", "[wxyz]"}
set {mv, LF} to {missing value, linefeed}
-- Check input.
try
set reporting to (query's class is not text)
if (not reporting) then
repeat with chr in query
if (chr is not in digits) then error "Invalid digit input"
end repeat
set digitCount to (count query)
end if
set || to current application
set pathStr to (||'s NSString's stringWithString:(posixPath))'s ¬
stringByExpandingTildeInPath()
set {txt, err} to ||'s NSMutableString's stringWithContentsOfFile:(pathStr) ¬
usedEncoding:(mv) |error|:(reference)
if (err ≠ mv) then error (err's localizedDescription() as text)
on error errMsg
display alert "Textonyms handler: parameter error" message ¬
errMsg as critical buttons {"Stop"} default button 1
error number -128
end try
-- Lose obvious no-hope words.
set regex to ||'s NSRegularExpressionSearch
txt's replaceOccurrencesOfString:("\\R") withString:(LF) ¬
options:(regex) range:({0, txt's |length|()})
set |words| to txt's componentsSeparatedByString:(LF)
if ((reporting) or (digitCount > 9)) then
set predFormat to "(self MATCHES '(?i)[a-z]++')"
else
set predFormat to "(self MATCHES '(?i)[a-z]{" & digitCount & "}+')"
end if
set predicate to ||'s NSPredicate's predicateWithFormat:(predFormat)
set |words| to |words|'s filteredArrayUsingPredicate:(predicate)
set wordCount to |words|'s |count|()
-- Derive digit combinations from the rest.
set txt to (|words|'s componentsJoinedByString:(LF))'s mutableCopy()
set range to {0, txt's |length|()}
repeat with d in digits
(txt's replaceOccurrencesOfString:("(?i)" & keys's item d) withString:(d) ¬
options:(regex) range:(range))
end repeat
set combos to txt's componentsSeparatedByString:(LF)
-- Return the appropriate result.
if (reporting) then
set comboSet to ||'s NSSet's setWithArray:(combos)
set comboCount to comboSet's |count|()
set textonymSet to ||'s NSCountedSet's alloc()'s initWithArray:(combos)
textonymSet's minusSet:(comboSet)
set textonymCount to textonymSet's |count|()
set output to (wordCount as text) & " words in '" & ¬
(pathStr's lastPathComponent()) & ¬
"' can be represented by the digit key mapping." & ¬
(LF & comboCount & " digit combinations are required to represent them.") & ¬
(LF & textonymCount & " of the digit combinations represent Textonyms.")
else
set output to {}
set range to {0, wordCount}
set i to combos's indexOfObject:(query) inRange:(range)
repeat until (i > wordCount)
set output's end to (|words|'s objectAtIndex:(i)) as text
set range to {i + 1, wordCount - (i + 1)}
set i to combos's indexOfObject:(query) inRange:(range)
end repeat
if ((count output) = 1) then set output to {}
end if
return output
end textonyms
 
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
on task()
set posixPath to "~/Desktop/www.rosettacode.org/unixdict.txt"
set report to textonyms(posixPath, missing value)
set output to {report, "", "Examples:"}
repeat with digitCombo in {"729", "723353", "25287876746242"}
set foundWords to textonyms(posixPath, digitCombo's contents)
set output's end to digitCombo & " --> {" & join(foundWords, ", ") & "}"
end repeat
return join(output, linefeed)
end task
 
task()</syntaxhighlight>
 
{{output}}
Same as for the "vanilla" solution.
 
=={{header|Arturo}}==
557

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.