Textonyms: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
(→OCaml: add) |
||
Line 46: | Line 46: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
<syntaxhighlight lang="11l">[Char = String] CH2NUM |
<syntaxhighlight lang="11l">[Char = String] CH2NUM |
||
L(chars) ‘abc def ghi jkl mno pqrs tuv wxyz’.split(‘ ’) |
L(chars) ‘abc def ghi jkl mno pqrs tuv wxyz’.split(‘ ’) |
||
Line 689: | Line 688: | ||
49376746242 => hydrophobia hydrophobic |
49376746242 => hydrophobia hydrophobic |
||
6388537663 => mettlesome nettlesome</pre> |
6388537663 => mettlesome nettlesome</pre> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
{{libheader| System.SysUtils}} |
{{libheader| System.SysUtils}} |
||
Line 914: | Line 914: | ||
Key "7244967473642" matches: schizophrenia schizophrenic |
Key "7244967473642" matches: schizophrenia schizophrenic |
||
</pre> |
</pre> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2020-07-03}} |
{{works with|Factor|0.99 2020-07-03}} |
||
Line 1,324: | Line 1,325: | ||
=={{header|Io}}== |
=={{header|Io}}== |
||
<syntaxhighlight lang="io">main := method( |
<syntaxhighlight lang="io">main := method( |
||
setupLetterToDigitMapping |
setupLetterToDigitMapping |
||
Line 1,464: | Line 1,464: | ||
=={{header|J}}== |
=={{header|J}}== |
||
<syntaxhighlight lang="j">require'regex strings web/gethttp' |
<syntaxhighlight lang="j">require'regex strings web/gethttp' |
||
Line 2,213: | Line 2,212: | ||
10 2668368466 contention, convention |
10 2668368466 contention, convention |
||
10 6388537663 mettlesome, nettlesome</pre> |
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}}== |
=={{header|Perl}}== |
||
Line 2,495: | Line 2,546: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
This version allows digits to be used (since you can usually enter them through an SMS-style keypad). |
This version allows digits to be used (since you can usually enter them through an SMS-style keypad). |
||
Line 3,041: | Line 3,091: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
<syntaxhighlight lang="tcl">set keymap { |
<syntaxhighlight lang="tcl">set keymap { |
||
2 -> ABC |
2 -> ABC |