Textonyms: Difference between revisions

18,691 bytes added ,  3 months ago
m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(9 intermediate revisions by 4 users not shown)
Line 46:
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">[Char = String] CH2NUM
L(chars) ‘abc def ghi jkl mno pqrs tuv wxyz’.split(‘ ’)
Line 271 ⟶ 270:
Encodings with this length are:
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}}==
 
<syntaxhighlight lang="arturo">words: read.lines relative "unixdict.txt" | select => [match? & {/^[a-z]+$/}]
 
nums: "22233344455566677778889999"
 
phone: $ => [
join map &'c -> nums\[sub to :integer c 97]
]
 
textonyms: #[]
tcount: 0
 
loop words 'w [
p: phone w
if? key? textonyms p [
textonyms\[p]: textonyms\[p] ++ w
if 2 = size textonyms\[p] -> 'tcount + 1
]
else -> textonyms\[p]: @[w]
]
 
print ~{
There are |size words| words in unixdict.txt which can be represented by the digit key mapping.
They require |size keys textonyms| digit combinations to represent them.
|tcount| digit combinations represent Textonyms.
 
7325 -> |textonyms\["7325"]|
}</syntaxhighlight>
 
{{out}}
 
<pre>There are 24978 words in unixdict.txt which can be represented by the digit key mapping.
They require 22903 digit combinations to represent them.
1473 digit combinations represent Textonyms.
 
7325 -> [peak peal peck real reck seal]</pre>
 
=={{header|AWK}}==
{{works with|gawk|5.1.0 }}
<syntaxhighlight lang="awk">
#!/usr/bin/env -S gawk -E
 
BEGIN { # user's configuration area
 
KEYMAP="2 abc 3 def 4 ghi 5 jkl 6 mno 7 pqrs 8 tuv 9 wxyz"
FNAME="/usr/share/dict/american-english" # 0.5 MB; 102775 words;
 
#KEYMAP="2 αβγά 3 δεζέ 4 ηθιήίϊΐ 5 κλμ 6 νξοό 7 πρσς 8 τυφύϋΰ 9 χψωώ"
#FNAME="/usr/share/dict/greek" # 19.5MB; 828808 words;
 
# where generated data will be written,
# or comment out a line if you don’t need it.
EXPORT_TXN="/tmp/textonyms"
EXPORT_ALL="/tmp/phonewords"
EXPORT_BAD="/tmp/invalidwords" #also the line ‘BUFF_ERRW = BUFF_...’
}
BEGIN { # main
delete ARGV; ARGC=1 # do not accept command line arguments
delete XEK # reserve id for use only as a hash table
delete TXN # reserve id ...
AZ="" # generated Alphabet
EE=0 # invalid word Counter
KK=0 # valid word Counter
TT=0 # textonym groups in the table TXN
BUFF_ERRW="" # invalid word buffer
TOTAL=1 # enum
COUNT=2 # enum
 
STDERR="/dev/stderr"
OLD_RS=RS
OLD_FS=FS
processFile()
generateReport()
userQuery()
}
function processFile( ii,jj,nn,errW,ss,aKey,aGroup,qqq){
$0=KEYMAP
AZ=" "
for (ii=1; ii<=NF; ii=ii+2) {
aKey=$ii; aGroup=$(ii+1)
nn=split(aGroup, qqq, //)
for (jj=1; jj<=nn; jj++) {ss=qqq[jj]; XEK[ss]=aKey; AZ = AZ ss " " }
}
AZ = AZ " "
######################
RS="^$" #
FS="[\n\t ]+" #
######################
if ((getline <FNAME) <= 0) {
printf "unexpected EOF or error: ‘%s’ %s\n",FNAME,ERRNO >STDERR
exit 1
} else printf "total words in the file ‘%s’: %s\n", FNAME,NF
for (ii=1; ii<=NF; ii++) {
errW=0
ss=tolower($ii)
nn=split(ss, qqq, //)
nmb=""
for (jj=1; jj<=nn; jj++) {
lchr=qqq[jj]
if (index(AZ," "lchr" ")>0) { nmb = nmb XEK[lchr] }
else {
EE++
errW=1
BUFF_ERRW = BUFF_ERRW $ii "\n"
break
}
}
if (errW) { continue }
T9=TXN[nmb][TOTAL]
if (index(T9" "," "ss" ")==0) {
TXN[nmb][TOTAL] = T9 " " ss
TXN[nmb][COUNT]++
}
KK++
}
}
function generateReport( elm){
for (elm in TXN) { if (TXN[elm][COUNT]>1) { TT++ } }
printf "valid words: %9s\n", KK
printf "invalid words: %9s\n", EE
printf "table indices for valid words: %9s\n", length(TXN)
printf "textonym groups in the table: %9s\n", TT
exportData()
close(EXPORT_BAD); close(EXPORT_TXN); close(EXPORT_ALL)
}
function exportData( elm){
if (EXPORT_BAD != "") print BUFF_ERRW >EXPORT_BAD
 
if (EXPORT_TXN != "" && EXPORT_ALL != "") {
printf "%s\n",
"number-of-textonyms\tword's-length\tkeys\tlist-of-textonyms" >EXPORT_ALL
printf "%s\n",
"number-of-textonyms\tword's-length\tkeys\tlist-of-textonyms" >EXPORT_TXN
for (elm in TXN) {
printf "%s\t%s\t%s\t%s\n",
TXN[elm][COUNT], length(elm), elm, TXN[elm][TOTAL] >EXPORT_ALL
if (TXN[elm][COUNT]>1) {
printf "%s\t%s\t%s\t%s\n",
TXN[elm][COUNT], length(elm), elm, TXN[elm][TOTAL] >EXPORT_TXN
}
}
return ## return ## return ## return ##
} else if (EXPORT_ALL != "") {
printf "%s\n",
"number-of-textonyms\tword's-length\tkeys\tlist-of-textonyms" >EXPORT_ALL
for (elm in TXN) {
printf "%s\t%s\t%s\t%s\n",
TXN[elm][COUNT], length(elm), elm, TXN[elm][TOTAL] >EXPORT_ALL
}
}
else if (EXPORT_TXN != "") {
printf "%s\n",
"number-of-textonyms\tword's-length\tkeys\tlist-of-textonyms" >EXPORT_TXN
for (elm in TXN) {
if (TXN[elm][COUNT]>1) {
printf "%s\t%s\t%s\t%s\n",
TXN[elm][COUNT], length(elm), elm, TXN[elm][TOTAL] >EXPORT_TXN
}
}
}
}
function userQuery( userasks,ss,ss1,nn,key,words){
printf "txn>> "
RS=OLD_RS
FS=OLD_FS
while ((getline ) > 0) {
userasks=$1
if (NF==0){ printf "txn>> ", ""; continue }
else if (userasks ~ /^-e|--ex|--exit$/) { exit }
else if (userasks ~ /^[0-9]+$/) {
nn=TXN[userasks][COUNT]+0
words=TXN[userasks][TOTAL]
if (nn == 0) { printf "%s -> %s\n", userasks,"no matching words" }
else { printf "%s -> (%s) %s\n", userasks,nn,words }
}
else {
ss=tolower(userasks)
if ((key=keySeq_orElse_zero(ss))>0) {
ss1=(index((TXN[key][TOTAL]" ") , " "ss" ")>0) ?
", and the word is in" : ", but the word is not in"
printf "%s -> %s; the key is%s in the table%s\n", ss,key,
((key in TXN) ?"":" not"),ss1
}
else {
printf "%s -> not a valid word for the alphabet:\n%s\n", userasks,AZ
}
}
printf "txn>> "
}
printf "\n"
}
function keySeq_orElse_zero(aWord, qqq,lchr,nn,jj,buf){
nn=split(aWord, qqq, //)
for (jj=1; jj<=nn; jj++) {
lchr=qqq[jj]
if (index(AZ," "lchr" ")>0) { buf = buf XEK[lchr] } else { return 0 }
}
return buf
}
</syntaxhighlight>
 
{{out}}
<pre>
# Run, assuming the code is in the txn.awk
$ LANG=en_US.UTF-8 ./txn.awk
total words in the file ‘/usr/share/dict/american-english’: 102775
valid words: 73318
invalid words: 29457
table indices for valid words: 65817
textonym groups in the table: 4670
txn>> cafe
cafe -> 2233; the key is in the table, but the word is not in
txn>> 2233
2233 -> (3) abed aced bade
txn>> café
café -> not a valid word for the alphabet:
a b c d e f g h i j k l m n o p q r s t u v w x y z
txn>> --exit
$
$
$ egrep 'café' "/tmp/invalidwords"
café
café's
cafés
$
$ sort -n -b -k 1 "/tmp/textonyms" | tail -n 7
8 6 782537 quaker pucker quakes rubles stakes staler stales sucker
9 3 269 amy bmw cox coy any bow box boy cow
9 4 2273 case acre bard bare barf base cape card care
9 4 7277 parr sars paps pars pass raps rasp saps sass
9 4 7867 pump puns rump rums runs stop sump sums suns
9 5 46637 homer goner goods goofs homes hones hoods hoofs inner
12 5 22737 acres bards barer bares barfs baser bases caper capes cards cares cases
$
$
$ sort -n -b -k 2 "/tmp/phonewords" | tail -n 5
1 20 86242722837478422559 uncharacteristically
1 21 353287636237425647267 electroencephalograms
1 21 353287636237425647274 electroencephalograph
1 22 2686837738658846627437 counterrevolutionaries
1 22 3532876362374256472747 electroencephalographs
$
</pre>
 
Line 689 ⟶ 1,175:
49376746242 => hydrophobia hydrophobic
6388537663 => mettlesome nettlesome</pre>
 
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
Line 914 ⟶ 1,401:
Key "7244967473642" matches: schizophrenia schizophrenic
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2020-07-03}}
Line 1,324 ⟶ 1,812:
 
=={{header|Io}}==
 
<syntaxhighlight lang="io">main := method(
setupLetterToDigitMapping
Line 1,464 ⟶ 1,951:
 
=={{header|J}}==
 
<syntaxhighlight lang="j">require'regex strings web/gethttp'
 
Line 2,213 ⟶ 2,699:
10 2668368466 contention, convention
10 6388537663 mettlesome, nettlesome</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">module IntMap = Map.Make(Int)
 
let seq_lines ch =
let rec repeat () =
match input_line ch with
| s -> Seq.Cons (s, repeat)
| exception End_of_file -> Nil
in repeat
 
(* simply use bijective numeration in base 8 for keys *)
 
let key_of_char = function
| 'a' .. 'c' -> Some 1
| 'd' .. 'f' -> Some 2
| 'g' .. 'i' -> Some 3
| 'j' .. 'l' -> Some 4
| 'm' .. 'o' -> Some 5
| 'p' .. 's' -> Some 6
| 't' .. 'v' -> Some 7
| 'w' .. 'z' -> Some 8
| _ -> None
 
let keys_of_word =
let next k c =
Option.bind (key_of_char c) (fun d -> Option.map (fun k -> k lsl 3 + d) k)
in String.fold_left next (Some 0)
 
let update m k =
IntMap.update k (function Some n -> Some (succ n) | None -> Some 1) m
 
let map_from ch =
seq_lines ch |> Seq.filter_map keys_of_word |> Seq.fold_left update IntMap.empty
 
let count _ n (words, keys, txtns) =
words + n, succ keys, if n > 1 then succ txtns else txtns
 
let show src (words, keys, txtns) = Printf.printf "\
There are %u words in %s which can be represented by the digit key mapping.\n\
They require %u digit combinations to represent them.\n\
%u digit combinations represent Textonyms.\n" words src keys txtns
 
let () =
show "stdin" (IntMap.fold count (map_from stdin) (0, 0, 0))</syntaxhighlight>
{{out}}
<pre>
There are 24978 words in stdin which can be represented by the digit key mapping.
They require 22903 digit combinations to represent them.
1473 digit combinations represent Textonyms.
</pre>
...when being feeded with unixdict.txt
 
=={{header|Perl}}==
Line 2,495 ⟶ 3,033:
 
=={{header|Racket}}==
 
This version allows digits to be used (since you can usually enter them through an SMS-style keypad).
 
Line 3,041 ⟶ 3,578:
 
=={{header|Tcl}}==
 
<syntaxhighlight lang="tcl">set keymap {
2 -> ABC
Line 3,180 ⟶ 3,716:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "./str" for Char, Str
import "./sort" for Sort
import "./fmt" for Fmt
 
var wordList = "unixdict.txt"
9,476

edits