Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,120 @@
#include <fstream>
#include <iostream>
#include <unordered_map>
#include <vector>
struct Textonym_Checker {
private:
int total;
int elements;
int textonyms;
int max_found;
std::vector<std::string> max_strings;
std::unordered_map<std::string, std::vector<std::string>> values;
int get_mapping(std::string &result, const std::string &input)
{
static std::unordered_map<char, char> mapping = {
{'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'}
};
result = input;
for (char &c : result) {
if (!isalnum(c)) return 0;
if (isalpha(c)) c = mapping[toupper(c)];
}
return 1;
}
public:
Textonym_Checker(void) : total(0), elements(0), textonyms(0), max_found(0) { }
~Textonym_Checker(void) { }
void add(const std::string &str) {
std::string mapping;
total += 1;
if (!get_mapping(mapping, str)) return;
const int num_strings = values[mapping].size();
textonyms += num_strings == 1 ? 1 : 0;
elements += 1;
if (num_strings > max_found) {
max_strings.clear();
max_strings.push_back(mapping);
max_found = num_strings;
}
else if (num_strings == max_found) {
max_strings.push_back(mapping);
}
values[mapping].push_back(str);
}
void results(const std::string &filename) {
std::cout << "Read " << total << " words from " << filename << "\n\n";
std::cout << "There are " << elements << " words in " << filename;
std::cout << " which can be represented by the digit key mapping.\n";
std::cout << "They require " << values.size() <<
" digit combinations to represent them.\n";
std::cout << textonyms << " digit combinations represent Textonyms.\n\n";
std::cout << "The numbers mapping to the most words map to ";
std::cout << max_found + 1 << " words each:\n";
for (auto it1 = max_strings.begin(); it1 != max_strings.end(); ++it1) {
std::cout << '\t' << *it1 << " maps to: ";
for (auto it2 = values[*it1].begin(); it2 != values[*it1].end(); ++it2) {
std::cout << *it2 << " ";
}
std::cout << "\n";
}
std::cout << '\n';
}
void match(const std::string &str) {
auto match = values.find(str);
if (match == values.end()) {
std::cout << "Key '" << str << "' not found\n";
}
else {
std::cout << "Key '" << str << "' matches: ";
for (auto it = values[str].begin(); it != values[str].end(); ++it)
std::cout << *it << " ";
std::cout << '\n';
}
}
};
int main(void)
{
std::string filename = "unixdict.txt";
std::ifstream input(filename);
Textonym_Checker tc;
if (input.is_open()) {
std::string line;
while (getline(input, line))
tc.add(line);
}
input.close();
tc.results(filename);
tc.match("001");
tc.match("228");
tc.match("27484247");
tc.match("7244967473642");
}

View file

@ -0,0 +1,27 @@
void main() {
import std.stdio, std.string, std.range, std.algorithm, std.ascii;
immutable src = "unixdict.txt";
const words = src.File.byLineCopy.map!strip.filter!(w => w.all!isAlpha).array;
immutable table = makeTrans("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
"2223334445556667777888999922233344455566677778889999");
string[][string] dials;
foreach (const word; words)
dials[word.translate(table)] ~= word;
auto textonyms = dials.byPair.filter!(p => p[1].length > 1).array;
writefln("There are %d words in %s which can be represented by the digit key mapping.", words.length, src);
writefln("They require %d digit combinations to represent them.", dials.length);
writefln("%d digit combinations represent Textonyms.", textonyms.length);
"\nTop 5 in ambiguity:".writeln;
foreach (p; textonyms.schwartzSort!(p => -p[1].length).take(5))
writefln(" %s => %-(%s %)", p[]);
"\nTop 5 in length:".writeln;
foreach (p; textonyms.schwartzSort!(p => -p[0].length).take(5))
writefln(" %s => %-(%s %)", p[]);
}

View file

@ -0,0 +1,48 @@
import Data.Maybe (isJust, isNothing, fromMaybe)
import Data.Char (toUpper)
import Data.List (sortBy, groupBy)
import Data.Function (on)
toKey :: Char -> Maybe Char
toKey ch
| ch < 'A' = Nothing
| ch < 'D' = Just '2'
| ch < 'G' = Just '3'
| ch < 'J' = Just '4'
| ch < 'M' = Just '5'
| ch < 'P' = Just '6'
| ch < 'T' = Just '7'
| ch < 'W' = Just '8'
| ch <= 'Z' = Just '9'
| otherwise = Nothing
toKeyString :: String -> Maybe String
toKeyString st =
let mch = map (toKey.toUpper) st
in if any isNothing mch then Nothing
else Just $ map (fromMaybe '!') mch
showTextonym :: [(String,String)] -> IO ()
showTextonym ts = do
let keyCode = fst $ head ts
putStrLn $ keyCode ++ " => " ++ concat [w ++ " " | (_,w) <- ts ]
main :: IO()
main = do
let src = "unixdict.txt"
contents <- readFile src
let wordList = lines contents
keyedList = [(key, word) | (Just key, word) <- filter (isJust.fst) $ zip (map toKeyString wordList) wordList]
groupedList = groupBy ((==) `on` fst) $ sortBy (compare `on` fst) keyedList
textonymList = filter ((>1) . length) groupedList
putStrLn $ "There are " ++ show (length keyedList) ++ " words in " ++ src ++ " which can be represented by the digit key mapping."
putStrLn $ "They require " ++ show (length groupedList) ++ " digit combinations to represent them."
putStrLn $ show (length textonymList) ++ " digit combinations represent Textonyms."
putStrLn ""
putStrLn "Top 5 in ambiguity:"
mapM_ showTextonym $ take 5 $ sortBy (flip compare `on` length) textonymList
putStrLn ""
putStrLn "Top 5 in length:"
mapM_ showTextonym $ take 5 $ sortBy (flip compare `on` (length.fst.head)) textonymList

View file

@ -0,0 +1,162 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Vector;
public class RTextonyms {
private static final Map<Character, Character> mapping;
private int total, elements, textonyms, max_found;
private String filename, mappingResult;
private Vector<String> max_strings;
private Map<String, Vector<String>> values;
static {
mapping = new HashMap<Character, Character>();
mapping.put('A', '2'); mapping.put('B', '2'); mapping.put('C', '2');
mapping.put('D', '3'); mapping.put('E', '3'); mapping.put('F', '3');
mapping.put('G', '4'); mapping.put('H', '4'); mapping.put('I', '4');
mapping.put('J', '5'); mapping.put('K', '5'); mapping.put('L', '5');
mapping.put('M', '6'); mapping.put('N', '6'); mapping.put('O', '6');
mapping.put('P', '7'); mapping.put('Q', '7'); mapping.put('R', '7'); mapping.put('S', '7');
mapping.put('T', '8'); mapping.put('U', '8'); mapping.put('V', '8');
mapping.put('W', '9'); mapping.put('X', '9'); mapping.put('Y', '9'); mapping.put('Z', '9');
}
public RTextonyms(String filename) {
this.filename = filename;
this.total = this.elements = this.textonyms = this.max_found = 0;
this.values = new HashMap<String, Vector<String>>();
this.max_strings = new Vector<String>();
return;
}
public void add(String line) {
String mapping = "";
total++;
if (!get_mapping(line)) {
return;
}
mapping = mappingResult;
if (values.get(mapping) == null) {
values.put(mapping, new Vector<String>());
}
int num_strings;
num_strings = values.get(mapping).size();
textonyms += num_strings == 1 ? 1 : 0;
elements++;
if (num_strings > max_found) {
max_strings.clear();
max_strings.add(mapping);
max_found = num_strings;
}
else if (num_strings == max_found) {
max_strings.add(mapping);
}
values.get(mapping).add(line);
return;
}
public void results() {
System.out.printf("Read %,d words from %s%n%n", total, filename);
System.out.printf("There are %,d words in %s which can be represented by the digit key mapping.%n", elements,
filename);
System.out.printf("They require %,d digit combinations to represent them.%n", values.size());
System.out.printf("%,d digit combinations represent Textonyms.%n", textonyms);
System.out.printf("The numbers mapping to the most words map to %,d words each:%n", max_found + 1);
for (String key : max_strings) {
System.out.printf("%16s maps to: %s%n", key, values.get(key).toString());
}
System.out.println();
return;
}
public void match(String key) {
Vector<String> match;
match = values.get(key);
if (match == null) {
System.out.printf("Key %s not found%n", key);
}
else {
System.out.printf("Key %s matches: %s%n", key, match.toString());
}
return;
}
private boolean get_mapping(String line) {
mappingResult = line;
StringBuilder mappingBuilder = new StringBuilder();
for (char cc : line.toCharArray()) {
if (Character.isAlphabetic(cc)) {
mappingBuilder.append(mapping.get(Character.toUpperCase(cc)));
}
else if (Character.isDigit(cc)) {
mappingBuilder.append(cc);
}
else {
return false;
}
}
mappingResult = mappingBuilder.toString();
return true;
}
public static void main(String[] args) {
String filename;
if (args.length > 0) {
filename = args[0];
}
else {
filename = "./unixdict.txt";
}
RTextonyms tc;
tc = new RTextonyms(filename);
Path fp = Paths.get(filename);
try (Scanner fs = new Scanner(fp, StandardCharsets.UTF_8.name())) {
while (fs.hasNextLine()) {
tc.add(fs.nextLine());
}
}
catch (IOException ex) {
ex.printStackTrace();
}
List<String> numbers = Arrays.asList(
"001", "228", "27484247", "7244967473642",
"."
);
tc.results();
for (String number : numbers) {
if (number.equals(".")) {
System.out.println();
}
else {
tc.match(number);
}
}
return;
}
}

View file

@ -0,0 +1,22 @@
const tcode = (Regex=>Char)[r"A|B|C|Ä|Å|Á|Â|Ç" => '2',
r"D|E|F|È|Ê|É" => '3',
r"G|H|I|Í" => '4',
r"J|K|L" => '5',
r"M|N|O|Ó|Ö|Ô|Ñ" => '6',
r"P|Q|R|S" => '7',
r"T|U|V|Û|Ü" => '8',
r"W|X|Y|Z" => '9']
function tpad(str::IOStream)
tnym = (String=>Array{String,1})[]
for w in eachline(str)
w = chomp(w)
t = uppercase(w)
for (k,v) in tcode
t = replace(t, k, v)
end
t = replace(t, r"\D", '1')
tnym[t] = [get(tnym, t, String[]), w]
end
return tnym
end

View file

@ -0,0 +1,39 @@
dname = "/usr/share/dict/american-english"
DF = open(dname, "r")
tnym = tpad(DF)
close(DF)
println("The character to digit mapping is done according to")
println("these regular expressions (following uppercase conversion):")
for k in sort(collect(keys(tcode)), by=x->tcode[x])
println(" ", tcode[k], " -> ", k)
end
println("Unmatched non-digit characters are mapped to 1")
println()
print("There are ", sum(map(x->length(x), values(tnym))))
println(" words in ", dname)
println(" which can be represented by the digit key mapping.")
print("They require ", length(keys(tnym)))
println(" digit combinations to represent them.")
print(sum(map(x->length(x)>1, values(tnym))))
println(" digit combinations represent Textonyms.")
println()
println("The degeneracies of telephone key encodings are:")
println(" Words Encoded Number of codes")
dgen = zeros(maximum(map(x->length(x), values(tnym))))
for v in values(tnym)
dgen[length(v)] += 1
end
for (i, d) in enumerate(dgen)
println(@sprintf "%10d %15d" i d)
end
println()
dgen = length(dgen) - 2
println("Codes mapping to ", dgen, " or more words:")
for (k, v) in tnym
dgen <= length(v) || continue
println(@sprintf "%7s (%2d) %s" k length(v) join(v, ", "))
end

View file

@ -0,0 +1,81 @@
#lang racket
(module+ test (require tests/eli-tester))
(module+ test
(test
(map char->sms-digit (string->list "ABCDEFGHIJKLMNOPQRSTUVWXYZ."))
=> (list 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 7 8 8 8 9 9 9 9 #f)))
(define char->sms-digit
(match-lambda
[(? char-lower-case? (app char-upcase C)) (char->sms-digit C)]
;; Digits, too, can be entered on a text pad!
[(? char-numeric? (app char->integer c)) (- c (char->integer #\0))]
[(or #\A #\B #\C) 2]
[(or #\D #\E #\F) 3]
[(or #\G #\H #\I) 4]
[(or #\J #\K #\L) 5]
[(or #\M #\N #\O) 6]
[(or #\P #\Q #\R #\S) 7]
[(or #\T #\U #\V) 8]
[(or #\W #\X #\Y #\Z) 9]
[_ #f]))
(module+ test
(test
(word->textonym "criticisms") => 2748424767
(word->textonym "Briticisms") => 2748424767
(= (word->textonym "Briticisms") (word->textonym "criticisms"))))
(define (word->textonym w)
(for/fold ((n 0)) ((s (sequence-map char->sms-digit (in-string w))) #:final (not s))
(and s (+ (* n 10) s))))
(module+ test
(test
((cons-uniquely 'a) null) => '(a)
((cons-uniquely 'a) '(b)) => '(a b)
((cons-uniquely 'a) '(a b c)) => '(a b c)))
(define ((cons-uniquely a) d)
(if (member a d) d (cons a d)))
(module+ test
(test
(with-input-from-string "criticisms" port->textonym#) =>
(values 1 (hash 2748424767 '("criticisms")))
(with-input-from-string "criticisms\nBriticisms" port->textonym#) =>
(values 2 (hash 2748424767 '("Briticisms" "criticisms")))
(with-input-from-string "oh-no!-dashes" port->textonym#) =>
(values 0 (hash))))
(define (port->textonym#)
(for/fold
((n 0) (t# (hash)))
((w (in-port read-line)))
(define s (word->textonym w))
(if s
(values (+ n 1) (hash-update t# s (cons-uniquely w) null))
(values n t#))))
(define (report-on-file f-name)
(define-values (n-words textonym#) (with-input-from-file f-name port->textonym#))
(define n-textonyms (for/sum ((v (in-hash-values textonym#)) #:when (> (length v) 1)) 1))
(printf "--- report on ~s ends ---~%" f-name)
(printf
#<<EOS
There are ~a words in ~s which can be represented by the digit key mapping.
They require ~a digit combinations to represent them.
~a digit combinations represent Textonyms.
EOS
n-words f-name (hash-count textonym#) n-textonyms)
;; Show all the 6+ textonyms
(newline)
(for (((k v) (in-hash textonym#)) #:when (>= (length v) 6)) (printf "~a -> ~s~%" k v))
(printf "--- report on ~s ends ---~%" f-name))
(module+ main
(report-on-file "data/unixdict.txt"))

View file

@ -0,0 +1,68 @@
set keymap {
2 -> ABC
3 -> DEF
4 -> GHI
5 -> JKL
6 -> MNO
7 -> PQRS
8 -> TUV
9 -> WXYZ
}
set url http://www.puzzlers.org/pub/wordlists/unixdict.txt
set report {
There are %1$s words in %2$s which can be represented by the digit key mapping.
They require %3$s digit combinations to represent them.
%4$s digit combinations represent Textonyms.
A %5$s-letter textonym which has %6$s combinations is %7$s:
%8$s
}
package require http
proc geturl {url} {
try {
set tok [http::geturl $url]
return [http::data $tok]
} finally {
http::cleanup $tok
}
}
proc main {keymap url} {
foreach {digit -> letters} $keymap {
foreach l [split $letters ""] {
dict set strmap $l $digit
}
}
set doc [geturl $url]
foreach word [split $doc \n] {
if {![string is alpha -strict $word]} continue
dict lappend words [string map $strmap [string toupper $word]] $word
}
set ncombos [dict size $words]
set nwords 0
set ntextos 0
set nmax 0
set dmax ""
dict for {d ws} $words {
puts [list $d $ws]
set n [llength $ws]
incr nwords $n
if {$n > 1} {
incr ntextos $n
}
if {$n >= $nmax && [string length $d] > [string length $dmax]} {
set nmax $n
set dmax $d
}
}
set maxwords [dict get $words $dmax]
set lenmax [llength $maxwords]
format $::report $nwords $url $ncombos $ntextos $lenmax $nmax $dmax $maxwords
}
puts [main $keymap $url]