Skip to content

Instantly share code, notes, and snippets.

@avar
Created March 17, 2012 20:26
Show Gist options
  • Save avar/2064945 to your computer and use it in GitHub Desktop.
Save avar/2064945 to your computer and use it in GitHub Desktop.
Figure out how much of a given piece of text (on stdin) you type where with Dvorak/QWERTY
#!/usr/bin/env perl
use strict;
use Data::Dumper;
use List::Util qw(sum);
binmode STDIN, ":utf8";
binmode STDOUT, ":utf8";
my $text = do { local $/; scalar <> };
my %layout_keys = (
qwerty => {
number_row => [ 0..9 ],
top_row_left => [ qw/ q w e r t / ],
top_row_right => [ qw/ y u i o p / ],
home_row_left => [ qw/ a s d f g / ],
home_row_right => [ qw/ h j k l / ],
bottom_row_left => [ qw/ z x c v / ],
bottom_row_right => [ qw/ b n m , . / ],
space_bar => [ ' ' ],
},
dvorak => {
number_row => [ 0..9 ],
top_row_left => [ qw/ ' , . p y / ],
top_row_right => [ qw/ f g c r l / ],
home_row_left => [ qw/ a o e u i / ],
home_row_right => [ qw/ d h t n s / ],
bottom_row_left => [ qw/ ; q j k / ],
bottom_row_right => [ qw/ x b m w / ],
space_bar => [ ' ' ],
}
);
my %reverse_lookup;
for my $layout (keys %layout_keys) {
for my $category (keys %{ $layout_keys{ $layout } }) {
for my $key (@{ $layout_keys{ $layout }->{ $category } }) {
die "Duplicate <$layout> entry for key <$key>" if exists $reverse_lookup{$layout}->{$key};
$reverse_lookup{$layout}->{$key} = $category;
}
}
}
my %statistics;
my %unclassified;
for my $c (map { lc } split //, $text) {
for my $layout (keys %layout_keys) {
my $category;
unless ($category = $reverse_lookup{$layout}->{$c}) {
$category = "N/A";
$unclassified{$layout}{$c}++;
}
$statistics{$layout}->{$category}++;
}
}
# Spew out statistics
printf "For a text of %d characters this is the breakdown by layouts:\n", length $text;
for my $layout (keys %layout_keys) {
printf " $layout:\n";
for my $category (sort { $statistics{$layout}->{$b} <=> $statistics{$layout}->{$a} } keys %{ $statistics{$layout} }) {
printf " %18s\t%d\n", $category, $statistics{$layout}->{$category};
}
printf " (we couldn't classify %d keys spread over %d keystrokes)\n",
scalar(keys %{ $unclassified{$layout} }),
sum(values %{ $unclassified{$layout} });
}
=head1 RESULTS
Here's a breakdown of a copy/paste of the intro paragraph of the
featured article of the day for Wikipedia in various languages
=head2 ENGLISH
For a text of 1379 characters this is the breakdown by layouts:
dvorak:
home_row_left 416
home_row_right 348
space_bar 219
top_row_right 193
top_row_left 64
bottom_row_right 57
number_row 44
N/A 23
bottom_row_left 15
(we couldn't classify 10 keys spread over 23 keystrokes)
qwerty:
top_row_left 332
home_row_left 241
top_row_right 220
space_bar 219
bottom_row_right 132
home_row_right 114
bottom_row_left 59
number_row 44
N/A 18
(we couldn't classify 10 keys spread over 18 keystrokes)
=head2 SPANISH
For a text of 1994 characters this is the breakdown by layouts:
dvorak:
home_row_left 699
home_row_right 424
top_row_right 317
space_bar 287
N/A 85
top_row_left 82
bottom_row_right 70
bottom_row_left 16
number_row 14
(we couldn't classify 13 keys spread over 85 keystrokes)
qwerty:
home_row_left 435
top_row_left 390
top_row_right 377
space_bar 287
bottom_row_right 199
bottom_row_left 122
home_row_right 110
N/A 60
number_row 14
(we couldn't classify 12 keys spread over 60 keystrokes)
=head2 GERMAN
For a text of 873 characters this is the breakdown by layouts:
dvorak:
home_row_left 258
home_row_right 238
top_row_right 134
space_bar 98
N/A 53
bottom_row_right 39
top_row_left 21
number_row 20
bottom_row_left 12
(we couldn't classify 11 keys spread over 53 keystrokes)
qwerty:
top_row_left 224
home_row_left 162
top_row_right 118
bottom_row_right 105
space_bar 98
home_row_right 72
N/A 39
bottom_row_left 35
number_row 20
(we couldn't classify 9 keys spread over 39 keystrokes)
=head2 ICELANDIC
For a text of 724 characters this is the breakdown by layouts:
dvorak:
home_row_left 165
top_row_right 128
home_row_right 110
space_bar 107
N/A 106
bottom_row_left 34
number_row 26
top_row_left 26
bottom_row_right 22
(we couldn't classify 12 keys spread over 106 keystrokes)
qwerty:
home_row_left 151
space_bar 107
top_row_right 95
top_row_left 94
N/A 84
bottom_row_right 77
home_row_right 68
number_row 26
bottom_row_left 22
(we couldn't classify 11 keys spread over 84 keystrokes)
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment