This is the final solution for my experiment to find the most optimal keyboard layout for use by one-finger or cursor+click movement:
V D R N G F Q _ A E I O U _ K H T S L B J X C W M Y P Z
The vowels in the second row would be enlarged to cover the width of the entire row.
The primary goal of the KType project is to improve communication for people with disabilities. I am developing an iPad app that presents the users with an on-screen keyboard with large keys that can be re-arranged easily for comfort and efficiency. The application makes communication easier by predicting what the user will type next. I analyzed how people talk on Twitter to create KType's prediction engine. Improving prediction is just one way to reduce the effort needed to communicate. Another way is to minimize the amount of movement required to type.
The computer keyboard layout we are all familiar with is called QWERTY based on the first six letters in the top row. It was designed by C. L. Sholes in 1878 for using a typewriter with two hands and the letters were arranged in a special, non-alphabetical order to improve the speed of typing by minimizing jamming of the typewriter. Since computer keyboards don't jam, many people have tried to re-arrange the keyboard layout to improve typing efficiency; a layout popular among computer programmers is called Dvorak. But all of these layouts are optimized for users with two hands, ten fingers.
Potential users of KType might use head-tracking and blink-action to type. Existing keyboard layouts are inefficient for typing with a single finger or cursor+click movement. KType users could certainly benefit from a keyboard layout that requires the least amount of movement when typing with just one finger. With that goal in mind, I sought to design such a layout - the Optimus Keyboard Layout.
In addition to Optimus, KType will also provide QWERTY, DVORAK, ABCDEF keyboard layouts to users who are already accustomed to a particular layout and do not wish to switch.
The 26 letters of the English alphabet can be rearranged into infinite ways on a flat surface. You can put all the keys in a straight line, arrange the consonants in a circle with the vowels in the middle, or break them into rows like regular computer keyboards. How can we measure if one keyboard layout is better than any other? To answer that question, I used a simple formula to calculate the score for each keyboard layout:
Keyboard Score = (Distance between a key and every other key) * (Frequency of that key-pair being used in regular typing)
The distance is relatively easy to calculate. Keys next to each other and above/below each other have a distance of one. Keys diagonal to each other have a distance of square-root of 2. If the keys are in a grid, it is trivial to compute the distances using the Pythagoras Theorem. Python even has a math.hypot function to make this easy.
The frequency for each combination of letters comes from my recent analysis of Twitter data that resulted in this letter-pair frequency table. I analyzed 9 million Tweets and obtained a list of the most common words and phrases used by over a hundred thousand users. Based on the frequency of use of each word and phrase, it was straightforward to calculate how often do two letters follow each other in common usage. Not surprisingly, the letters 'T' and 'H' are typed in a row most often, followed by 'I' and 'N', then 'E' and 'R'.
Imagine just three keys 'A', 'C', and 'T' arranged next to each other like [A][C][T]:
Distance between A and C 1 Frequency of typing 'AC' or 'CA' x 200 = 200 (for A, C) Distance between A and T 2 Frequency of typing 'AT' or 'TA' x 500 = 1000 (for A, T) Distance between C and T 1 Frequency of typing 'CT' or 'TC' x 100 = 100 (for C, T) --------------------------------------------------------------------------- Total Keyboard Score = 1300 (for A, C, T)
Lower keyboard scores mean the finger has to travel a shorter distance to type the most common words and phrases. To improve a keyboard layout, we just need to rearrange the letters such that letters most commonly typed in a row (like 'T' and 'H' or 'I' and 'N') are next to each other. In the above example, 'A' and 'T' are furthest apart at a distance of 2 and also typed the most often i.e. frequency of 500. We could easily re-arrange this keyboard to reduce the score as follows:
Re-arrange the three keys 'A', 'C', and 'T' to [C][A][T]:
Distance between A and C 1 Frequency of typing 'AC' or 'CA' x 200 = 200 (for A, C) Distance between A and T 1 Frequency of typing 'AT' or 'TA' x 500 = 500 (for A, T) Distance between C and T 2 Frequency of typing 'CT' or 'TC' x 100 = 200 (for C, T) --------------------------------------------------------------------------- Total Keyboard Score = 900 (for C, A, T)
Re-arranging the keys reduced the total keyboard score without any change in the frequency of typing different letter combinations. This 30% improvement in score means the same text can be typed faster and require less movement. For users who find it difficult to move their hands, this could be a remarkable improvement.
By this metric, the new Optimus keyboard should be significantly better than QWERTY, DVORAK, and alternatives.
To make it even possible to design an optimal keyboard, I had to place some restrictions on the layout:
These restrictions mean that I could be ignoring a more efficient keyboard layout (possibly circular) but given the fact that almost every keyboard in existence is close to a grid (3 rows of 10 keys) and the iPad has a rectangular screen, a grid-like layout will work for my users. The closer the grid is to a square, the lower the score is. I prefer 7×4 (28 key slots) grid because it is closer to square than 9×3 (27 key slots) and only has two blanks as compared to 6×5 (30 key slots) grid with 4 blanks. Additionally, the KType interface is suited quite well for 4 rows of letters.
The 26 alphabets could be arranged into a grid of 7×4 in over 10^29 ways, into a grid of 6×5 in over 10^32 ways. Planet Earth has only 10^50 atoms so given the current state of technology and my dwindling savings account, it is impossible to simulate every possible keyboard layout and calculate its score to find Optimus. In computer science terms, this is problem is in NP.
After realizing that I was trying to find a perfect solution to an uncomputable problem, I decided to put my four years of college to use. I dug into complexity theory articles and papers to see if someone has solved this exact problem but that effort was fruitless. I turned to reddit/r/compsci subreddit and rephrased my problem into more understandable terms: How would you seat 26 people in a big room of cubicles such that people who like each other a lot end up sitting close to each other? While I did not get an exact answer (and neither did I expect one), I got a lot of leads: genetic optimization, dynamic programming, distance preserving embedding graphs, facilities problem, k-means clustering, and simulated annealing.
I was very interested in using genetic algorithms to evolve a better keyboard from two or more good keyboards but found it impossible to combine two keyboard layouts into a new one. How would one combine the QWERTY and DVORAK layouts to create a new generation in one-pass anyway? I had similar issues in applying k-means clustering, graph layouts etc. to my problem. I don't mean it is impossible to use these methods, I just couldn't find a logical, simple way to apply these solutions to my problem. After fiddling with tons of algorithms, I found that simulated annealing works pretty nicely for this particular problem.
Update 2011-02-28, 1:25pm: Redditor deong mentioned that this problem is called Quadratic assignment problem, which is one of the fundamental combinatorial optimization problems in the branch of optimization or operations research in mathematics, from the category of the facilities location problems.
I tried three different methods to find the most optimal keyboard. I started with the simplest algorithm I could think of:
Step 1: Select a grid size: 7x4 (or 10x3, 9x3, 6x5) Step 2: Randomly put all the alphabets into the grid Step 3: Calculate the score Step 4: Swap two or more keys and recalculate score Step 5: If score in Step 4 is 'progressively better' than Step 3, keep the new layout Step 6: Repeat Step 4 about 100,000 times
Each time the above program was run, I would get a new keyboard layout with the lowest possible score that random swapping could achieve. I ran the script 10,000 times for 7×4 grid and here are the three best layouts:
Rank #1, Score: 130575410797
_ J Y M L D Z Q B U O A N G X P R E T I C _ V F S H W K
Rank #2, Score: 130676148330
_ K W H S F V X C I T E R P Q G N A O U B _ Z D L M Y J
Rank #3, Score: 130683308691
V F S H W K _ P R E T I C Z B U O A N G Q J Y M L D X _
Notice that the keys 'E', 'T', 'H', are always adjacent to each other and generally towards the center. Similarly 'I', 'N', 'A' are adjacent and close to the center. In practical terms, all three of these keyboards are equally good but the problem with them is that I cannot definitively tell if they are optimal or not. They just look so random!
Step 1: Get an optimized keyboard using Random Grid method Step 2: For each key in the keyboard, store its position in the grid and the keyboard score Step 3: Repeat Step 1 about 10,000 times Step 4: Find the key with the best position among all layouts and place it in that position Step 5: Repeat Step 4 for all keys i.e. find best open position for each key Step 6: Print the keyboard with best position for each key
I wrote this algorithm with the idea that if keyboards with letter 'E' in the second row, middle column most often get low scores then there is a good chance that 'E' belongs in the second row, middle column in the Optimus keyboard. It is also possible that keyboards with the letter 'T' in second row, middle column also get very low scores but not as low as 'E' in the same position. In that case we pick the next open position that 'T' has low scores in, and so on for each letter. I ran the script hundreds of times but not one keyboard scored better than any of the Random Grid keyboards, even though we combined optimized keyboard generated via the Random Grid algorithm. Applying 100,000 generations of randomized swapping to the final heat-map keyboard helped, but it was no different than starting with a completely random grid. So in the end, I abandoned this approach.
Step 1: Set grid size to: 7x4 Step 2: Put '_AEIOU_' in the second row, randomly put consonants into rows 1, 3, and 4 Step 3: Calculate the score Step 4: Swap two or more keys in rows 1, 3, or 4 and recalculate score Step 5: If score in Step 4 is 'progressively better' than Step 3, keep the new layout Step 6: Repeat Step 4 about 100,000 times
This method is almost identical to Random Grid with the exception that vowels are fixed in the second row. The idea behind this is three-fold:
I ran the above algorithm 100,000 times and was pleasantly surprised at the result. Every single time the best keyboard was the following:
Rank #1, Score: 135242359724
V D R N G F Q _ A E I O U _ K H T S L B J X C W M Y P Z
I also saw the following two layouts popup many times, notice how similar they are to the above layout:
Rank #2, Score: 135298507794 V D R N G F Q _ A E I O U _ C H T S L P J X K W M Y B Z
Rank #3, Score: 135428576171 V D R N G F Q _ A E I O U _ K C H T S M J X B W L Y P Z
I verified my code to ensure that I was indeed going through thousands of random keyboards and not the same random layout over and over again. Yet the exact same layout out of a possible combination of 10^19 (21-factorial permutations) kept showing up as the best result over and over again. Either I was constantly hitting the local minima or I had found Optimus. I wasn't sure. Time to graph it up.
If you look at Step 5 of Random Grid and Fixed Vowel Row algorithms, you'll see I only keep a keyboard generated from swapping two or more keys if the new score is 'progressively better' than the previous score. Whether a new keyboard is considered progressively better or not directly affects how a random keyboard mutates over generations to achieve a lower score. The problem with iteratively generating improved keyboards from an initial random layout is getting stuck in local minima. Local minima is the state where after a number of generations, randomly swapping 2-6 keys over and over will not help lower the score of the keyboard. This doesn't mean we have achieved the best keyboard layout possible, but rather an entirely different layout would be required if we were to get any lower. This is similar to playing Sudoku where after getting the first 2-3 numbers fit, we find it impossible to solve the rest of the puzzle, because even though these 2-3 numbers fit a small section of the puzzle, they are not the correct values and thus do not align with the final solution.
One sure way of getting stuck in the local minima is to set the definition of 'progressively better' to mean strictly better: Only accept a new keyboard if its score is lower than the previous score. A good way to avoid getting stuck in local minima is to accept scores slightly worse than the previous score, in the hope that future generations will lower the scores fast enough to achieve global minima eventually. For us, global minima is the lowest possible score achievable by a keyboard, which by definition is the Optimus Keyboard Layout that we seek.
I ran the Fixed-Vowel Row script with different values for 'progressively better':
The following graph shows the keyboard score over 1000 generations for each value of SA. Each of the five curves was generated by taking the lowest score for each generation from 1000 independent runs of the Fixed Vowel Row algorithm.
The horizontal axis is time i.e. the generation of each keyboard layout and vertical axis is the scaled-down keyboard score (100 = worst, 0 = best). Notice that the values for SA=1.001 are lower than those of SA=1 between generations 244 and 460. Both the lines approach 0 as time goes on. The graph for SA=1 gradually goes down to 0, without ever rising. The graph for SA=1.001 rises a few times and then falls down further towards zero. This is a visualization of the algorithm escaping the local minima. In the download section, you will find two Excel files in the zip file with more data showing the effect of the various SA values.
After examining these graphs I realized that if there is a completely different global minima, I'm not seeing any evidence for it. It appears to me that indeed the Fixed Vowel Row layout with score of 135242359724 is the most optimal layout I could come up with. I feel comfortable in saying that I have found Optimus.
Optimus is only useful if it is significantly better than the existing keyboard layouts. In designing Optimus, I made two types of changes:
As I mentioned above, the closer a grid is to a square, the lower the score will be. Consequently, a random 7×4 keyboard should have a lower score than a random 10×3 or 9×3 keyboard layout. In an example above I showed that re-arranging keys without changing grid size can significantly lower scores too.
To make direct comparisons with QWERTY/DVORAK possible, I also designed a 10×3 Random Grid keyboard:
Score: 234828339277 - QWERTY
Q W E R T Y U I O P A S D F G H J K L _ _ Z X C V B N M _ _
Score: 285170559534 - DVORAK
_ _ P Y F G C R L _ A O E U I D H T N S _ Q J K X B M W V Z
Score: 228186373801 - ABCDEF
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 _
Score: 142812598543 - Random Grid 10x3
X V W H E R M B J _ Z C D A T O U Y P _ _ K G N I S L F Q _
Score: 130575410797 - Random Grid 7x4
_ J Y M L D Z Q B U O A N G X P R E T I C _ V F S H W K
Score: 135242359724 - Fixed Vowel Row - Optimus
V D R N G F Q _ A E I O U _ K H T S L B J X C W M Y P Z
The Random Grid 10×3 layout is 40% better than QWERTY and 50% better than DVORAK. Surprisingly, the simple ABCDEF is better than both QWERTY and DVORAK when it comes to typing regular English words with one-finger but it is still no match for Random Grid 10×3. The absolute best layout is indeed Random Grid 7×4 but it is only marginally better than the best Fixed Vowel Row layout - Optimus.
I will be using this Optimus layout in the KType iPad application. The Optimus Keyboard Layout and all the other layouts I designed as part of KType are released under Creative Commons Attribution 3.0 Unported license. You are free to use these layouts in your products and projects freely. Feel free to contact me if you wish to discuss customizing these layouts to better suit your application.
Download: ktype-design-optimus.zip (480 kb).
The file contains the following letter-pair frequency table, Python source code to generate the keyboard layouts using various algorithms, resulting keyboard layouts, and analysis of simulated annealing effects.
Letter 1 | Letter 2 | Frequency |
---|---|---|
H | T | 2218229634 |
I | N | 1922554842 |
E | R | 1913206038 |
O | T | 1651959574 |
E | H | 1601011001 |
I | T | 1487255208 |
N | O | 1479860227 |
A | N | 1265459126 |
A | T | 1260648745 |
E | T | 1258725651 |
S | T | 1185232584 |
E | N | 1171608838 |
O | U | 1126932155 |
E | S | 1107565465 |
O | R | 1092265913 |
A | H | 1022280805 |
I | S | 960195483 |
E | M | 918278103 |
G | N | 909733510 |
A | L | 839446797 |
F | O | 823487673 |
E | V | 801947989 |
A | E | 797897656 |
A | R | 796846722 |
N | T | 794178903 |
E | L | 789272000 |
D | E | 783266966 |
A | S | 773980698 |
I | L | 759404506 |
O | W | 751046502 |
L | O | 734816471 |
M | O | 698163757 |
D | N | 678634049 |
O | S | 648145027 |
D | O | 643315365 |
O | Y | 633596100 |
A | D | 616725699 |
H | I | 609242985 |
T | U | 596760119 |
H | O | 586086517 |
B | E | 575192703 |
E | W | 572812083 |
A | M | 572266367 |
A | C | 565338092 |
G | O | 559686660 |
D | I | 549517237 |
A | W | 537247331 |
I | M | 521586389 |
S | U | 507334794 |
A | Y | 502393165 |
I | R | 486246571 |
I | W | 450687179 |
E | K | 428882955 |
R | T | 421211358 |
E | I | 407799929 |
R | U | 407397030 |
C | E | 407351197 |
C | O | 403087197 |
E | G | 382764462 |
G | I | 378602448 |
H | W | 368500309 |
I | K | 360033022 |
E | P | 354495878 |
C | I | 346569568 |
A | B | 335507570 |
F | I | 328914371 |
C | H | 323540014 |
M | Y | 316866458 |
I | O | 309551788 |
H | S | 305508336 |
B | O | 303004287 |
A | I | 298145659 |
E | Y | 296447167 |
O | P | 295404717 |
E | F | 290971651 |
K | N | 280835232 |
A | V | 279795408 |
E | O | 262514383 |
A | P | 261998245 |
A | G | 255356395 |
N | U | 254152765 |
L | U | 254113954 |
C | K | 248424708 |
I | V | 247549005 |
L | Y | 243996318 |
T | W | 242491399 |
D | L | 240391532 |
P | U | 237599233 |
G | H | 232906631 |
T | Y | 222888273 |
R | S | 216653503 |
A | K | 211980140 |
B | U | 211979402 |
N | S | 211776426 |
A | F | 193824730 |
O | V | 192962716 |
C | T | 188939364 |
R | Y | 188502168 |
D | T | 188402546 |
C | U | 186361403 |
D | R | 185088727 |
L | P | 184104242 |
G | U | 178630271 |
F | R | 178527639 |
K | O | 174733777 |
F | T | 171291497 |
L | T | 161219491 |
I | P | 161111324 |
S | Y | 156142943 |
G | R | 153700734 |
J | U | 152286241 |
L | S | 151520422 |
G | T | 149316256 |
P | R | 143296724 |
B | L | 138601267 |
N | Y | 138246852 |
D | S | 134751885 |
M | U | 132378859 |
M | T | 127343629 |
F | U | 125993561 |
D | Y | 121347072 |
A | U | 119556041 |
K | S | 119326917 |
I | Y | 113795309 |
C | N | 113761460 |
D | U | 113668234 |
S | W | 111895723 |
P | S | 111450787 |
E | U | 110905976 |
M | S | 109566195 |
K | R | 103770406 |
C | R | 101493632 |
B | Y | 101445176 |
B | I | 101204181 |
N | R | 100055146 |
G | S | 98748538 |
L | R | 97710011 |
B | R | 95417380 |
E | X | 89960199 |
N | W | 87655828 |
H | Y | 85299649 |
A | O | 79710648 |
F | Y | 77693099 |
M | N | 77076264 |
C | S | 76403037 |
L | N | 76372421 |
C | L | 75154679 |
H | U | 74147418 |
B | T | 69144648 |
F | L | 69083270 |
M | P | 68044740 |
M | R | 63155285 |
P | T | 62406896 |
W | Y | 60259788 |
R | W | 59306342 |
L | M | 58902094 |
H | R | 58246149 |
K | L | 58134151 |
G | L | 55875832 |
F | S | 55770806 |
K | T | 55634609 |
P | Y | 55528363 |
J | O | 53893753 |
D | M | 50827500 |
B | S | 49601932 |
L | W | 48340561 |
I | U | 47184174 |
H | P | 46870843 |
G | M | 46807175 |
U | Y | 45139207 |
F | N | 44243974 |
B | D | 42611310 |
D | W | 42008116 |
B | M | 41059508 |
D | H | 40179641 |
U | W | 39316423 |
H | N | 38696521 |
I | Z | 38554786 |
G | Y | 38354464 |
T | X | 36283799 |
Q | U | 35324874 |
D | G | 32666173 |
F | M | 32426682 |
C | Y | 31746087 |
A | Z | 31563140 |
D | F | 30409074 |
H | M | 29847179 |
E | J | 28237181 |
F | G | 28104043 |
K | U | 27621187 |
M | W | 27257997 |
A | J | 25554812 |
K | Y | 25484182 |
H | L | 23549529 |
B | N | 23242247 |
E | Z | 22969777 |
R | V | 22195816 |
I | J | 21744779 |
G | W | 19462601 |
C | D | 19309807 |
F | K | 17509243 |
D | P | 17429475 |
F | W | 17333850 |
C | X | 16852097 |
N | V | 16627871 |
I | X | 16517246 |
P | W | 15371993 |
F | H | 15319896 |
Y | Z | 15225352 |
J | N | 14670487 |
N | P | 14354461 |
A | X | 14148250 |
F | P | 14133734 |
K | W | 13722663 |
P | X | 13701927 |
D | V | 11802046 |
B | G | 11632065 |
B | W | 11303828 |
T | V | 11233279 |
J | S | 10902445 |
O | X | 10533830 |
U | Z | 10333128 |
C | F | 9356896 |
G | P | 8574159 |
D | K | 8570548 |
S | V | 8520590 |
U | V | 8506988 |
B | K | 8472017 |
K | M | 8335180 |
C | M | 7916579 |
B | H | 7886184 |
J | T | 7765266 |
H | K | 7180195 |
D | J | 7161828 |
B | F | 6484840 |
O | Z | 6475488 |
E | Q | 6240834 |
L | V | 6235558 |
B | C | 6163482 |
C | W | 6027788 |
B | P | 6008353 |
G | K | 5464448 |
J | Y | 5153882 |
J | M | 4982654 |
C | P | 4754842 |
V | Y | 4660821 |
C | G | 4065364 |
J | L | 3738552 |
K | P | 3637084 |
J | R | 3527477 |
Q | S | 3172981 |
X | Y | 3111515 |
A | Q | 2840475 |
H | X | 2737927 |
I | Q | 2443563 |
N | X | 2323584 |
M | V | 2300484 |
U | X | 2103527 |
J | K | 1830467 |
L | Z | 1783769 |
T | Z | 1728101 |
M | X | 1668414 |
J | W | 1639389 |
H | V | 1541979 |
V | W | 1430812 |
B | X | 1253260 |
H | J | 1234409 |
F | J | 1215778 |
F | X | 1201958 |
B | J | 1134450 |
Q | T | 1107381 |
B | V | 992734 |
H | Q | 988915 |
N | Z | 982866 |
B | Q | 957192 |
F | V | 931569 |
G | Z | 914489 |
C | V | 878834 |
J | P | 858104 |
P | V | 847409 |
G | J | 841536 |
N | Q | 812960 |
W | X | 787716 |
C | Q | 775845 |
O | Q | 770656 |
D | Q | 765843 |
S | X | 741104 |
Q | R | 704695 |
G | V | 665842 |
Q | Y | 654502 |
Q | W | 497820 |
R | X | 494117 |
S | Z | 487502 |
H | Z | 487323 |
D | X | 473416 |
L | X | 394765 |
M | Z | 378236 |
L | Q | 360847 |
W | Z | 356981 |
C | J | 338473 |
D | Z | 311653 |
C | Z | 256072 |
G | X | 163601 |
K | V | 152321 |
G | Q | 145178 |
R | Z | 143610 |
F | Q | 123009 |
V | Z | 118481 |
B | Z | 106094 |
F | Z | 103771 |
K | Q | 97577 |
M | Q | 91807 |
V | X | 86300 |
K | Z | 69338 |
J | V | 56091 |
P | Z | 51385 |
P | Q | 50243 |
Q | V | 35036 |
J | Q | 30206 |
Q | X | 28116 |
J | X | 16176 |
J | Z | 5151 |
Q | Z | 3030 |