Textonyms: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 47:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">[Char = String] CH2NUM
L(chars) ‘abc def ghi jkl mno pqrs tuv wxyz’.split(‘ ’)
V num = L.index + 2
Line 94:
print(‘ #. maps to: #.’.format(num, wrds.join(‘, ’)))
 
interactiveconversions()</langsyntaxhighlight>
 
{{out}}
Line 123:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} Uses the Algol 68G specific "to upper" procedure.
<langsyntaxhighlight lang="algol68"># find textonyms in a list of words #
# use the associative array in the Associate array/iteration task #
PR read "aArray.a68" PR
Line 259:
 
FI
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 275:
=={{header|C}}==
{{libheader|GLib}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Line 449:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 473:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <fstream>
#include <iostream>
#include <unordered_map>
Line 590:
tc.match("27484247");
tc.match("7244967473642");
}</langsyntaxhighlight>
{{out}}
<pre>Read 25104 words from unixdict.txt
Line 611:
The [[Textonyms#Tcl|Tcl example]] counts all the words which share a digit sequence with another word. Like the other
examples, this considers a textonym to be a digit sequence which maps to more than one word.
<syntaxhighlight lang="clojure">
<lang Clojure>
(def table
{\a 2 \b 2 \c 2 \A 2 \B 2 \C 2
Line 636:
(count mapping) " digit combinations to represent them. "
(count textonyms) " digit combinations represent Textonyms.")))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 644:
=={{header|D}}==
{{trans|Raku}}
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.string, std.range, std.algorithm, std.ascii;
 
Line 670:
foreach (p; textonyms.schwartzSort!(p => -p[0].length).take(5))
writefln(" %s => %-(%s %)", p[]);
}</langsyntaxhighlight>
{{out}}
<pre>There are 24978 words in unixdict.txt which can be represented by the digit key mapping.
Line 695:
{{libheader| System.Character}}
{{Trans|C++}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Textonyms;
 
Line 896:
end.
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 916:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-07-03}}
<langsyntaxhighlight lang="factor">USING: assocs assocs.extras interpolate io io.encodings.utf8
io.files kernel literals math math.parser prettyprint sequences
unicode ;
Line 942:
${} digit combinations represent Textonyms.I] nl nl
 
"7325 -> " write words textonyms 7325 of .</langsyntaxhighlight>
{{out}}
<pre>
Line 957:
Like the [[Textonyms#Python|Phython example]],
the examples shown are the numbers that map to the most words.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,097:
fmt.Fprintln(w, "\t", k, "maps to:", strings.Join(v, ", "))
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,119:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Char (toUpper)
import Data.Function (on)
import Data.List (groupBy, sortBy)
Line 1,189:
++ fmap
showTextonym
(take 5 $ sortBy (flip compare `on` (length . fst . head)) textonymList)</langsyntaxhighlight>
{{out}}
<pre style="font-size:80%">There are 24978 words in unixdict.txt which can be represented by the digit key mapping.
Line 1,212:
Or, in terms of ''Data.Map'' and ''traverse'':
 
<langsyntaxhighlight lang="haskell">import Data.Function (on)
import Data.List (groupBy, maximum, sortBy, sortOn)
import qualified Data.Map as M
Line 1,301:
]
where
rjust n c = (drop . length) <*> (replicate n c <>)</langsyntaxhighlight>
{{Out}}
<pre>There are 24978 words in unixdict.txt which can be represented
Line 1,325:
=={{header|Io}}==
 
<langsyntaxhighlight Iolang="io">main := method(
setupLetterToDigitMapping
 
Line 1,428:
)
 
main</langsyntaxhighlight>
{{output}}
<pre>There are 24978 words in unixdict.txt which can be represented by the digit key mapping.
Line 1,465:
=={{header|J}}==
 
<langsyntaxhighlight Jlang="j">require'regex strings web/gethttp'
 
strip=:dyad define
Line 1,502:
reps=. {&digits@(letters&i.)&.> valid NB. reps is digit seq
reporttext report (#valid);y;(#~.reps);+/(1<#)/.~reps
)</langsyntaxhighlight>
 
Required example:
 
<langsyntaxhighlight Jlang="j"> keys textonymrpt 'http://rosettacode.org/wiki/Textonyms/wordlist'
There are 13085 words in http://rosettacode.org/wiki/Textonyms/wordlist which can be represented by the digit key mapping.
They require 11932 digit combinations to represent them.
661 digit combinations represent Textonyms.</langsyntaxhighlight>
 
In this example, the intermediate results in textonymrpt would look like this (just looking at the first 5 elements of the really big values:
 
<langsyntaxhighlight Jlang="j"> digits
22233344455566677778889999
letters
Line 1,524:
┌─┬──┬───┬───┬──┐
│2│22│222│226│22│
└─┴──┴───┴───┴──┘</langsyntaxhighlight>
 
Here's another example:
 
<langsyntaxhighlight Jlang="j"> keys textonymrpt 'http://www.puzzlers.org/pub/wordlists/unixdict.txt'
There are 24978 words in http://www.puzzlers.org/pub/wordlists/unixdict.txt which can be represnted by the digit key mapping.
They require 22903 digit combinations to represent them.
1473 digit combinations represent Textonyms.</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|c++}}
<langsyntaxhighlight lang="java">
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Line 1,698:
}
}
</syntaxhighlight>
</lang>
{{out|Output with "java RTextonyms ./unixdict.txt"}}
<pre>
Line 1,718:
=={{header|jq}}==
The following requires a version of jq with "gsub".
<langsyntaxhighlight lang="jq">def textonym_value:
gsub("a|b|c|A|B|C"; "2")
| gsub("d|e|f|D|E|F"; "3")
Line 1,757:
;
 
explore</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">$ jq -R -r -c -s -f textonyms.jq textonyms.txt
There are 13085 words in the Textonyms/wordlist word list that can be represented by the digit-key mapping.
They require 11932 digit combinations to represent them.
Line 1,767:
The longest Textonyms in the word list have length 11:
26456746242 maps to: ["Anglophobia","Anglophobic"]
24636272673 maps to: ["CinemaScope","Cinemascope"]</langsyntaxhighlight>
 
=={{header|Julia}}==
This solution uses an <tt>aspell</tt> dictionary on the local machine as its word source. The character to number mapping is done via regular expressions and Julia's <tt>replace</tt> function. Because this list contains accented characters, the matching expressions were expanded to include such characters. Words are case sensitive, but the mapping is not, so for example both "Homer" and "homer" are included in the tabulation, each coded as "46637".
'''Function'''
<langsyntaxhighlight Julialang="julia">using Printf
 
const tcode = (Regex=>Char)[r"A|B|C|Ä|Å|Á|Â|Ç" => '2',
Line 1,796:
return tnym
end
</syntaxhighlight>
</lang>
 
'''Main'''
<syntaxhighlight lang="julia">
<lang Julia>
dname = "/usr/share/dict/american-english"
DF = open(dname, "r")
Line 1,839:
println(@sprintf "%7s (%2d) %s" k length(v) join(v, ", "))
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,885:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.4-3
 
import java.io.File
Line 1,947:
fun main(args: Array<String>) {
processList()
}</langsyntaxhighlight>
 
{{out}}
Line 1,981:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">-- Global variables
http = require("socket.http")
keys = {"VOICEMAIL", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}
Line 2,035:
 
-- Main procedure
showReport(textonyms(http.request(dictFile)))</langsyntaxhighlight>
{{out}}
<pre>There are 24983 words in http://www.puzzlers.org/pub/wordlists/unixdict.txt
Line 2,043:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[Numerify,rls]
rls={"A"->2,"B"->2,"C"->2,"D"->3,"E"->3,"F"->3,"G"->4,"H"->4,"I"->4,"J"->5,"K"->5,"L"->5,"M"->6,"N"->6,"O"->6,"P"->7,"Q"->7,"R"->7,"S"->7,"T"->8,"U"->8,"V"->8,"W"->9,"X"->9,"Y"->9,"Z"->9};
Numerify[s_String]:=Characters[ToUpperCase[s]]/.rls
Line 2,058:
KeyValueMap[List,TakeLargestBy[grouped,Length,1]]//Grid
Print["5 longest words with textonyms:"]
List@@@Normal[ReverseSortBy[grouped,First/*Length][[;;5]]]//Grid</langsyntaxhighlight>
{{out}}
<pre>Number of words from Textonyms/wordlist are: 71125
Line 2,073:
=={{header|MiniScript}}==
This solution assumes the Mini Micro environment (providing the listUtil and mapUtil modules, as well as the englishWords.txt file).
<langsyntaxhighlight MiniScriptlang="miniscript">import "listUtil"
import "mapUtil"
 
Line 2,121:
print num + " -> " + numToWords.get(num)
end if
end while</langsyntaxhighlight>
{{out}}
<pre>There are 64664 words in englishWords.txt which can be represented
Line 2,137:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils, strformat, strutils, tables
 
const
Line 2,183:
echo &"""{l[0].len:4} {l[0]:>14} {l[1].join(", ")}"""
 
processList(WordList)</langsyntaxhighlight>
 
{{out}}
Line 2,215:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">my $src = 'unixdict.txt';
 
# filter word-file for valid input, transform to low-case
Line 2,231:
print "There are @{[scalar @words]} words in '$src' which can be represented by the digit key mapping.
They require @{[scalar @dials]} digit combinations to represent them.
@{[scalar @textonyms]} digit combinations represent Textonyms.";</langsyntaxhighlight>
{{out}}
<pre>There are 24978 words in 'unixdict.txt' which can be represented by the digit key mapping.
Line 2,238:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">digit</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">255</span><span style="color: #0000FF;">)</span>
Line 2,313:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %s encodes %s\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">words</span><span style="color: #0000FF;">[</span><span style="color: #000000;">long_idx</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{Out}}
<small>(my unixdict.txt seems to have grown by 4 entries sometime in the past couple of years...)</small>
Line 2,329:
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
$url = "http://www.puzzlers.org/pub/wordlists/unixdict.txt"
$file = "$env:TEMP\unixdict.txt"
Line 2,375:
 
Remove-Item -Path $file -Force -ErrorAction SilentlyContinue
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,405:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from collections import defaultdict
import urllib.request
 
Line 2,466:
print(" %s maps to: %s" % (num, ', '.join(wrds)))
 
interactiveconversions()</langsyntaxhighlight>
 
{{out}}
Line 2,500:
<code>unixdict.txt</code> has words like <q>2nd</q> which would not be valid using letters only, but is textable.
 
<langsyntaxhighlight lang="racket">#lang racket
(module+ test (require tests/eli-tester))
(module+ test
Line 2,580:
 
(module+ main
(report-on-file "data/unixdict.txt"))</langsyntaxhighlight>
 
{{out}}
Line 2,614:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>my $src = 'unixdict.txt';
 
my @words = slurp($src).lines.grep(/ ^ <alpha>+ $ /);
Line 2,635:
 
say "\nTop 5 in length:";
say " ",$_ for @textonyms.sort(-*.key.chars)[^5];</langsyntaxhighlight>
{{out}}
<pre>There are 24978 words in unixdict.txt which can be represented by the digit key mapping.
Line 2,658:
Extra code was added detect and display a count illegal words &nbsp; (words not representable by the ''key digits''), &nbsp; and
<br>also duplicate words in the dictionary.
<langsyntaxhighlight lang="rexx">/*REXX program counts and displays the number of textonyms that are in a dictionary file*/
parse arg iFID . /*obtain optional fileID from the C.L. */
if iFID=='' | iFID=="," then iFID='UNIXDICT.TXT' /*Not specified? Then use the default.*/
Line 2,713:
commas: parse arg _; do jc=length(_)-3 to 1 by -3; _=insert(',', _, jc); end; return _
tell: arg ##; say 'There are ' right(commas(##), L)' ' arg(2).; return /*commatize #*/
s: if arg(1)==1 then return ''; return "s" /*a simple pluralizer.*/</langsyntaxhighlight>
 
{{out|output|text=&nbsp; when using the default input file:}}
Line 2,746:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">
CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
NUMS = "22233344455566677778889999" * 2
Line 2,758:
 
puts "\n25287876746242: #{textonyms["25287876746242"].join(", ")}"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,769:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">use std::collections::HashMap;
use std::fs::File;
use std::io::{self, BufRead};
Line 2,858:
Err(error) => eprintln!("{}: {}", args[1], error),
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,883:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">var words = ARGF.grep(/^[[:alpha:]]+\z/);
 
var dials = words.group_by {
Line 2,902:
 
say "\nTop 5 in length:";
say textonyms.sort_by { |k,_| -k.len }.first(5).join("\n");</langsyntaxhighlight>
{{out}}
<pre>
Line 2,926:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
func textCharacter(_ ch: Character) -> Character? {
Line 3,017:
} catch {
print(error.localizedDescription)
}</langsyntaxhighlight>
 
{{out}}
Line 3,042:
=={{header|Tcl}}==
 
<langsyntaxhighlight Tcllang="tcl">set keymap {
2 -> ABC
3 -> DEF
Line 3,109:
}
 
puts [main $keymap $url]</langsyntaxhighlight>
 
{{out}}
Line 3,123:
 
=={{header|VBScript}}==
<langsyntaxhighlight lang="vb">Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInFile = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) &_
"\unixdict.txt",1)
Line 3,169:
objMoreThanOneWord.Count & " digit combinations represent Textonyms."
 
objInFile.Close</langsyntaxhighlight>
{{out}}
<pre>There are 24978 words in "unixdict.txt" which can be represented by the digit key mapping.
Line 3,180:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "io" for File
import "/str" for Char, Str
import "/sort" for Sort
Line 3,229:
System.print("Length Textonym Words")
System.print("====== ============== =====")
for (l in longest.take(6)) Fmt.print(f, l.key.count, l.key, l.value)</langsyntaxhighlight>
 
{{out}}
Line 3,265:
{{trans|Python}}
Like the Python example, this solution uses the Unix Dictionary, rather than the textonyms word list as I don't want to parse the HTML.
<langsyntaxhighlight lang="zkl">URL:="http://www.puzzlers.org/pub/wordlists/unixdict.txt";
var ZC=Import("zklCurl");
var keypad=Dictionary(
Line 3,294:
foreach k,v in (wcnt.filter('wrap([(k,v)]){ v.len()==maxWordPerNum })){
println(" %s is the textonym of: %s".fmt(k,v.concat(", ")));
}</langsyntaxhighlight>
{{out}}
<pre>
10,339

edits